2012-08-28 18:44:51 +02:00
|
|
|
from django.contrib.auth import authenticate, login
|
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
from django.http import HttpResponse, HttpResponseRedirect
|
|
|
|
from django.shortcuts import render_to_response
|
|
|
|
from django.template import RequestContext
|
|
|
|
from django.shortcuts import render
|
2012-08-28 23:41:04 +02:00
|
|
|
from django.utils.timezone import utc
|
2012-08-28 18:44:51 +02:00
|
|
|
|
|
|
|
from django.contrib.auth.models import User
|
2012-08-30 19:56:15 +02:00
|
|
|
from zephyr.models import Zephyr, UserProfile, ZephyrClass, Subscription, \
|
2012-09-04 23:20:21 +02:00
|
|
|
Recipient, filter_by_subscriptions, get_display_recipient, get_huddle
|
2012-08-28 18:44:51 +02:00
|
|
|
from zephyr.forms import RegistrationForm
|
|
|
|
|
2012-08-28 22:56:21 +02:00
|
|
|
import tornado.web
|
|
|
|
from zephyr.decorator import asynchronous
|
|
|
|
|
2012-08-28 18:44:51 +02:00
|
|
|
import datetime
|
|
|
|
import simplejson
|
2012-09-05 01:11:25 +02:00
|
|
|
import socket
|
2012-08-28 18:44:51 +02:00
|
|
|
|
|
|
|
def register(request):
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = RegistrationForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
username = request.POST['username']
|
|
|
|
password = request.POST['password']
|
|
|
|
u = User.objects.create_user(username=username, password=password)
|
|
|
|
u.save()
|
|
|
|
user = authenticate(username=username, password=password)
|
|
|
|
login(request, user)
|
|
|
|
return HttpResponseRedirect(reverse('zephyr.views.home'))
|
|
|
|
else:
|
|
|
|
form = RegistrationForm()
|
|
|
|
|
|
|
|
return render(request, 'zephyr/register.html', {
|
|
|
|
'form': form,
|
|
|
|
})
|
|
|
|
|
|
|
|
def accounts_home(request):
|
|
|
|
return render_to_response('zephyr/accounts_home.html',
|
2012-09-04 23:21:30 +02:00
|
|
|
context_instance=RequestContext(request))
|
|
|
|
|
2012-08-28 18:44:51 +02:00
|
|
|
def home(request):
|
|
|
|
if not request.user.is_authenticated():
|
|
|
|
return HttpResponseRedirect('accounts/home/')
|
|
|
|
|
2012-08-28 22:56:21 +02:00
|
|
|
zephyrs = filter_by_subscriptions(Zephyr.objects.all(), request.user)
|
2012-08-28 21:27:42 +02:00
|
|
|
|
2012-08-28 18:44:51 +02:00
|
|
|
user = request.user
|
|
|
|
user_profile = UserProfile.objects.get(user=user)
|
2012-08-28 21:27:42 +02:00
|
|
|
if user_profile.pointer == -1 and zephyrs:
|
2012-08-28 18:44:51 +02:00
|
|
|
user_profile.pointer = min([zephyr.id for zephyr in zephyrs])
|
|
|
|
user_profile.save()
|
2012-08-30 19:56:15 +02:00
|
|
|
zephyr_json = simplejson.dumps([zephyr.to_dict() for zephyr in zephyrs])
|
2012-09-04 20:31:23 +02:00
|
|
|
|
|
|
|
personals = filter_by_subscriptions(Zephyr.objects.filter(
|
|
|
|
recipient__type="personal").all(), request.user)
|
|
|
|
people = simplejson.dumps(list(
|
2012-09-05 17:41:53 +02:00
|
|
|
set(get_display_recipient(zephyr.recipient) for zephyr in personals)))
|
2012-09-04 20:31:23 +02:00
|
|
|
|
|
|
|
publics = filter_by_subscriptions(Zephyr.objects.filter(
|
|
|
|
recipient__type="class").all(), request.user)
|
2012-09-05 22:15:38 +02:00
|
|
|
|
|
|
|
subscriptions = Subscription.objects.filter(userprofile_id=user_profile, active=True)
|
|
|
|
classes = simplejson.dumps([get_display_recipient(sub.recipient_id) for sub in subscriptions
|
|
|
|
if sub.recipient_id.type == "class"])
|
|
|
|
|
2012-09-05 17:41:53 +02:00
|
|
|
instances = simplejson.dumps(list(
|
|
|
|
set(zephyr.instance for zephyr in publics)))
|
2012-09-04 20:31:23 +02:00
|
|
|
|
2012-08-30 19:56:15 +02:00
|
|
|
return render_to_response('zephyr/index.html',
|
|
|
|
{'zephyr_json' : zephyr_json,
|
2012-09-04 20:31:23 +02:00
|
|
|
'user_profile': user_profile,
|
|
|
|
'people' : people,
|
|
|
|
'classes' : classes,
|
|
|
|
'instances' : instances},
|
2012-08-28 18:44:51 +02:00
|
|
|
context_instance=RequestContext(request))
|
|
|
|
|
2012-09-04 17:39:12 +02:00
|
|
|
@login_required
|
2012-08-28 18:44:51 +02:00
|
|
|
def update(request):
|
|
|
|
if not request.POST:
|
|
|
|
# Do something
|
|
|
|
pass
|
|
|
|
user = request.user
|
|
|
|
user_profile = UserProfile.objects.get(user=user)
|
|
|
|
if request.POST.get('pointer'):
|
|
|
|
user_profile.pointer = request.POST.get("pointer")
|
|
|
|
user_profile.save()
|
2012-08-28 22:46:02 +02:00
|
|
|
return HttpResponse(simplejson.dumps({}), mimetype='application/json')
|
2012-08-28 18:44:51 +02:00
|
|
|
|
2012-08-28 22:56:21 +02:00
|
|
|
@asynchronous
|
|
|
|
def get_updates_longpoll(request, handler):
|
2012-08-28 18:44:51 +02:00
|
|
|
if not request.POST:
|
2012-08-28 22:56:21 +02:00
|
|
|
# TODO: Do something
|
2012-08-28 18:44:51 +02:00
|
|
|
pass
|
2012-09-04 23:21:30 +02:00
|
|
|
|
2012-08-28 23:32:05 +02:00
|
|
|
last_received = request.POST.get('last_received')
|
2012-08-28 22:56:21 +02:00
|
|
|
if not last_received:
|
|
|
|
# TODO: return error?
|
|
|
|
pass
|
|
|
|
|
|
|
|
user = request.user
|
|
|
|
user_profile = UserProfile.objects.get(user=user)
|
|
|
|
|
|
|
|
def on_receive(zephyrs):
|
|
|
|
if handler.request.connection.stream.closed():
|
|
|
|
return
|
|
|
|
try:
|
2012-08-30 19:56:15 +02:00
|
|
|
handler.finish({'zephyrs': [zephyr.to_dict() for zephyr in zephyrs]})
|
2012-08-28 22:56:21 +02:00
|
|
|
except socket.error, e:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# We need to replace this abstraction with the message list
|
|
|
|
user_profile.add_callback(handler.async_callback(on_receive), last_received)
|
2012-08-28 18:44:51 +02:00
|
|
|
|
|
|
|
@login_required
|
|
|
|
def zephyr(request):
|
2012-09-04 23:43:56 +02:00
|
|
|
if not request.POST:
|
|
|
|
# TODO: Do something
|
|
|
|
pass
|
2012-08-28 18:44:51 +02:00
|
|
|
|
2012-09-04 23:43:56 +02:00
|
|
|
zephyr_type = request.POST["type"]
|
|
|
|
if zephyr_type == 'class':
|
|
|
|
class_name = request.POST['class']
|
|
|
|
if ZephyrClass.objects.filter(name=class_name):
|
|
|
|
my_class = ZephyrClass.objects.get(name=class_name)
|
|
|
|
else:
|
|
|
|
my_class = ZephyrClass()
|
|
|
|
my_class.name = class_name
|
|
|
|
my_class.save()
|
|
|
|
recipient = Recipient.objects.get(type_id=my_class.id, type="class")
|
|
|
|
elif zephyr_type == "personal":
|
2012-09-04 23:20:21 +02:00
|
|
|
recipient_data = request.POST['recipient']
|
|
|
|
if ',' in recipient_data:
|
2012-09-05 17:19:03 +02:00
|
|
|
# This is actually a huddle message, which shares the
|
|
|
|
# "personal" zephyr sending form
|
2012-09-04 23:20:21 +02:00
|
|
|
recipients = [r.strip() for r in recipient_data.split(',')]
|
2012-09-05 17:19:03 +02:00
|
|
|
# Ignore any blank recipients
|
2012-09-05 17:14:12 +02:00
|
|
|
recipients = [r for r in recipients if r]
|
2012-09-04 23:20:21 +02:00
|
|
|
recipient_ids = [UserProfile.objects.get(user=User.objects.get(username=r)).id
|
|
|
|
for r in recipients]
|
2012-09-05 17:32:05 +02:00
|
|
|
# Make sure the sender is included in the huddle
|
2012-09-04 23:20:21 +02:00
|
|
|
recipient_ids.append(UserProfile.objects.get(user=request.user).id)
|
|
|
|
huddle = get_huddle(recipient_ids)
|
|
|
|
recipient = Recipient.objects.get(type_id=huddle.pk, type="huddle")
|
|
|
|
else:
|
2012-09-05 17:19:03 +02:00
|
|
|
# This is actually a personal message
|
2012-09-04 23:20:21 +02:00
|
|
|
if User.objects.filter(username=recipient_data):
|
|
|
|
recipient_user = User.objects.get(username=recipient_data)
|
|
|
|
recipient_user_profile = UserProfile.objects.get(user=recipient_user)
|
|
|
|
recipient = Recipient.objects.get(type_id=recipient_user_profile.id, type="personal")
|
2012-09-04 23:43:56 +02:00
|
|
|
else:
|
|
|
|
# Do something smarter here
|
|
|
|
raise
|
2012-08-28 21:27:42 +02:00
|
|
|
|
2012-08-28 18:44:51 +02:00
|
|
|
new_zephyr = Zephyr()
|
|
|
|
new_zephyr.sender = UserProfile.objects.get(user=request.user)
|
|
|
|
new_zephyr.content = request.POST['new_zephyr']
|
2012-08-28 21:27:42 +02:00
|
|
|
new_zephyr.recipient = recipient
|
2012-09-04 23:43:56 +02:00
|
|
|
if zephyr_type == "class":
|
|
|
|
new_zephyr.instance = request.POST['instance']
|
2012-08-28 23:08:38 +02:00
|
|
|
new_zephyr.pub_date = datetime.datetime.utcnow().replace(tzinfo=utc)
|
2012-08-28 18:44:51 +02:00
|
|
|
new_zephyr.save()
|
2012-08-28 21:27:42 +02:00
|
|
|
|
2012-08-30 22:03:20 +02:00
|
|
|
return HttpResponse('')
|
2012-08-30 18:04:35 +02:00
|
|
|
|
|
|
|
@login_required
|
|
|
|
def subscriptions(request):
|
|
|
|
userprofile = UserProfile.objects.get(user=request.user)
|
|
|
|
subscriptions = Subscription.objects.filter(userprofile_id=userprofile, active=True)
|
|
|
|
# For now, don't display the subscription for your ability to receive personals.
|
2012-09-05 21:32:06 +02:00
|
|
|
sub_names = [get_display_recipient(sub.recipient_id) for sub in subscriptions
|
|
|
|
if sub.recipient_id.type == "class"]
|
2012-08-30 18:04:35 +02:00
|
|
|
|
|
|
|
return render_to_response('zephyr/subscriptions.html',
|
|
|
|
{'subscriptions': sub_names, 'user_profile': userprofile},
|
|
|
|
context_instance=RequestContext(request))
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def manage_subscriptions(request):
|
|
|
|
if not request.POST:
|
|
|
|
# Do something reasonable.
|
|
|
|
return
|
|
|
|
user_profile = UserProfile.objects.get(user=request.user)
|
|
|
|
|
|
|
|
unsubs = request.POST.getlist('subscription')
|
|
|
|
for sub_name in unsubs:
|
|
|
|
zephyr_class = ZephyrClass.objects.get(name=sub_name)
|
2012-09-04 22:22:06 +02:00
|
|
|
recipient = Recipient.objects.get(type_id=zephyr_class.id, type="class")
|
2012-08-30 18:04:35 +02:00
|
|
|
subscription = Subscription.objects.get(
|
2012-08-30 20:00:04 +02:00
|
|
|
userprofile_id=user_profile.id, recipient_id=recipient)
|
2012-08-30 18:04:35 +02:00
|
|
|
subscription.active = False
|
|
|
|
subscription.save()
|
|
|
|
|
|
|
|
return HttpResponseRedirect(reverse('zephyr.views.subscriptions'))
|
2012-08-30 20:00:04 +02:00
|
|
|
|
|
|
|
@login_required
|
|
|
|
def add_subscriptions(request):
|
|
|
|
if not request.POST:
|
|
|
|
# Do something reasonable.
|
|
|
|
return
|
|
|
|
user_profile = UserProfile.objects.get(user=request.user)
|
|
|
|
|
|
|
|
new_subs = request.POST.get('new_subscriptions')
|
|
|
|
if not new_subs:
|
|
|
|
return HttpResponseRedirect(reverse('zephyr.views.subscriptions'))
|
|
|
|
|
|
|
|
for sub_name in new_subs.split(","):
|
|
|
|
zephyr_class = ZephyrClass.objects.filter(name=sub_name)
|
|
|
|
if zephyr_class:
|
|
|
|
zephyr_class = zephyr_class[0]
|
|
|
|
else:
|
2012-08-30 20:18:59 +02:00
|
|
|
zephyr_class = ZephyrClass(name=sub_name)
|
2012-08-30 20:00:04 +02:00
|
|
|
zephyr_class.save()
|
|
|
|
|
2012-09-04 22:22:06 +02:00
|
|
|
recipient = Recipient.objects.filter(type_id=zephyr_class.pk, type="class")
|
2012-08-30 20:35:55 +02:00
|
|
|
if recipient:
|
|
|
|
recipient = recipient[0]
|
|
|
|
else:
|
2012-09-04 22:22:06 +02:00
|
|
|
recipient = Recipient(type_id=zephyr_class.pk, type="class")
|
2012-08-30 20:00:04 +02:00
|
|
|
recipient.save()
|
|
|
|
|
2012-08-30 20:18:59 +02:00
|
|
|
subscription = Subscription.objects.filter(userprofile_id=user_profile,
|
2012-08-30 20:35:55 +02:00
|
|
|
recipient_id=recipient)
|
2012-08-30 20:00:04 +02:00
|
|
|
if subscription:
|
|
|
|
subscription = subscription[0]
|
|
|
|
subscription.active = True
|
|
|
|
subscription.save()
|
|
|
|
else:
|
2012-08-30 20:18:59 +02:00
|
|
|
new_subscription = Subscription(userprofile_id=user_profile,
|
|
|
|
recipient_id=recipient)
|
2012-08-30 20:00:04 +02:00
|
|
|
new_subscription.save()
|
|
|
|
|
|
|
|
return HttpResponseRedirect(reverse('zephyr.views.subscriptions'))
|