63 lines
2.2 KiB
Python
63 lines
2.2 KiB
Python
# Create your views here.
|
|
|
|
from django.shortcuts import render, redirect
|
|
from django.contrib.auth import authenticate, login, logout
|
|
|
|
from django.core.urlresolvers import reverse
|
|
from django.http import HttpResponse
|
|
|
|
from django.utils import simplejson
|
|
|
|
|
|
from eventplanner.models import Event, EventParticipation
|
|
|
|
from datetime import datetime
|
|
|
|
def home_view(request):
|
|
events = Event.objects.filter( date__gte = datetime.now() ).order_by('date')[:1]
|
|
context = dict()
|
|
|
|
|
|
part = EventParticipation.objects.filter( musician__user = request.user ).filter( event = events[0] )
|
|
|
|
eventTime = datetime( events[0].date.year, events[0].date.month, events[0].date.day,
|
|
events[0].displaytime.hour, events[0].displaytime.minute )
|
|
|
|
context['hasParticipationSetForAllEvents'] = EventParticipation.hasUserSetParticipationForAllEvents( request.user)
|
|
context['event'] = events[0]
|
|
context['epoch'] = int( (eventTime - datetime.now() ).total_seconds() * 1000 )
|
|
context['participation'] = part[0].status
|
|
return render( request, 'website/mainpage.html', context )
|
|
|
|
|
|
def logout_view(request):
|
|
logout( request )
|
|
return redirect( login_view )
|
|
|
|
|
|
def login_view(request):
|
|
if request.method == 'POST': # If the form has been submitted...
|
|
username = request.POST['username']
|
|
password = request.POST['password']
|
|
user = authenticate(username=username, password=password)
|
|
result = dict()
|
|
result['err'] = ""
|
|
result['redirect'] = ""
|
|
if user is not None:
|
|
if user.is_active:
|
|
if not request.POST.get('remember', None):
|
|
request.session.set_expiry( 0 )
|
|
login(request, user)
|
|
result['redirect'] = reverse( home_view )
|
|
else:
|
|
result['err'] = "Dein Account wurde deaktiviert."
|
|
# Return a 'disabled account' error message
|
|
else:
|
|
result['err'] = "Falscher Benutzername oder falsches Kennwort."
|
|
|
|
return HttpResponse( simplejson.dumps(result), mimetype='application/json' )
|
|
|
|
else:
|
|
# TODO check if already logged in --------------------!!!!
|
|
return render( request, 'website/login.html' )
|