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
|
|
|
|
|
|
|
|
from django.contrib.auth.models import User
|
2012-08-28 21:27:42 +02:00
|
|
|
from zephyr.models import Zephyr, UserProfile, ZephyrClass, Recipient
|
2012-08-28 18:44:51 +02:00
|
|
|
from zephyr.forms import RegistrationForm
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
import simplejson
|
|
|
|
|
|
|
|
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',
|
|
|
|
context_instance=RequestContext(request))
|
|
|
|
|
|
|
|
def home(request):
|
|
|
|
if not request.user.is_authenticated():
|
|
|
|
return HttpResponseRedirect('accounts/home/')
|
|
|
|
|
|
|
|
zephyrs = Zephyr.objects.all()
|
2012-08-28 21:27:42 +02:00
|
|
|
for zephyr in zephyrs:
|
|
|
|
# This is gross, but we'll probably be denormalizing this soon anyway.
|
|
|
|
zephyr_class = ZephyrClass.objects.get(pk=zephyr.recipient.user_or_class)
|
|
|
|
zephyr.zephyr_class = zephyr_class.name
|
|
|
|
|
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()
|
|
|
|
return render_to_response('zephyr/index.html', {'zephyrs': zephyrs, 'user_profile': user_profile},
|
|
|
|
context_instance=RequestContext(request))
|
|
|
|
|
|
|
|
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()
|
|
|
|
return HttpResponse(simplejson.dumps({}), mimetype='application/javascript')
|
|
|
|
|
|
|
|
def get_updates(request):
|
|
|
|
if not request.POST:
|
|
|
|
# Do something
|
|
|
|
pass
|
|
|
|
pointer = request.POST.get('pointer')
|
|
|
|
new_zephyrs = Zephyr.objects.filter(id__gt=pointer)
|
|
|
|
new_zephyr_list = []
|
|
|
|
for zephyr in new_zephyrs:
|
2012-08-28 21:27:42 +02:00
|
|
|
zephyr_class = ZephyrClass.objects.get(pk=zephyr.recipient.user_or_class)
|
2012-08-28 18:44:51 +02:00
|
|
|
new_zephyr_list.append({"id": zephyr.id,
|
|
|
|
"sender": zephyr.sender.user.username,
|
2012-08-28 21:27:42 +02:00
|
|
|
"zephyr_class": zephyr_class.name,
|
2012-08-28 18:44:51 +02:00
|
|
|
"instance": zephyr.instance,
|
|
|
|
"content": zephyr.content
|
|
|
|
})
|
|
|
|
|
|
|
|
return HttpResponse(simplejson.dumps(new_zephyr_list),
|
|
|
|
mimetype='application/javascript')
|
|
|
|
|
2012-08-28 21:27:42 +02:00
|
|
|
@login_required
|
|
|
|
def personal_zephyr(request):
|
|
|
|
username = request.POST['username']
|
|
|
|
if User.objects.filter(username=username):
|
|
|
|
user = User.objects.get(username=username)
|
|
|
|
else:
|
|
|
|
# Do something reasonable.
|
|
|
|
pass
|
|
|
|
|
|
|
|
new_zephyr = Zephyr()
|
|
|
|
new_zephyr.sender = UserProfile.objects.get(user=request.user)
|
|
|
|
new_zephyr.content = request.POST['new_zephyr']
|
|
|
|
new_zephyr.zephyr_class = username
|
|
|
|
new_zephyr.instance = request.POST['instance']
|
|
|
|
new_zephyr.pub_date = datetime.datetime.utcnow()
|
|
|
|
new_zephyr.save()
|
|
|
|
return HttpResponseRedirect(reverse('zephyr.views.home'))
|
|
|
|
|
2012-08-28 18:44:51 +02:00
|
|
|
@login_required
|
|
|
|
def zephyr(request):
|
|
|
|
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()
|
|
|
|
|
2012-08-28 21:27:42 +02:00
|
|
|
recipient = Recipient()
|
|
|
|
recipient.user_or_class = my_class.pk
|
|
|
|
recipient.type = "class"
|
|
|
|
recipient.save()
|
|
|
|
|
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-08-28 18:44:51 +02:00
|
|
|
new_zephyr.instance = request.POST['instance']
|
|
|
|
new_zephyr.pub_date = datetime.datetime.utcnow()
|
|
|
|
new_zephyr.save()
|
2012-08-28 21:27:42 +02:00
|
|
|
|
2012-08-28 18:44:51 +02:00
|
|
|
return HttpResponseRedirect(reverse('zephyr.views.home'))
|