2013-06-15 21:50:12 +02:00
|
|
|
# Create your views here.
|
|
|
|
|
|
|
|
from django.shortcuts import render, redirect
|
|
|
|
from django.contrib.auth import authenticate, login, logout
|
|
|
|
|
|
|
|
from django.http import HttpResponse
|
|
|
|
|
|
|
|
from django.utils import simplejson
|
2013-09-22 11:11:48 +02:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2013-06-15 21:50:12 +02:00
|
|
|
|
|
|
|
|
2013-09-15 14:48:40 +02:00
|
|
|
from eventplanner.models import Event, EventParticipation
|
2013-09-22 17:53:18 +02:00
|
|
|
from musicians.models import Musician
|
2013-09-15 14:48:40 +02:00
|
|
|
|
|
|
|
from datetime import datetime
|
2013-10-03 11:27:06 +02:00
|
|
|
from datetime import timedelta
|
2013-09-28 18:57:19 +02:00
|
|
|
from django.contrib.auth.models import User
|
2013-09-15 14:48:40 +02:00
|
|
|
|
2013-09-26 21:56:32 +02:00
|
|
|
|
2013-09-22 11:11:48 +02:00
|
|
|
@login_required
|
2013-06-15 21:50:12 +02:00
|
|
|
def home_view(request):
|
2013-09-15 14:48:40 +02:00
|
|
|
context = dict()
|
2013-09-22 11:11:48 +02:00
|
|
|
|
2013-09-26 21:56:32 +02:00
|
|
|
# Event participation for slider text
|
2013-10-14 19:46:53 +02:00
|
|
|
if EventParticipation.isMember( request.user ):
|
|
|
|
context['hasParticipationSetForAllEvents'] = EventParticipation.hasUserSetParticipationForAllEvents( request.user)
|
2013-09-26 21:56:32 +02:00
|
|
|
|
|
|
|
# Countdown
|
|
|
|
countdown = dict()
|
2013-11-03 13:07:30 +01:00
|
|
|
events = Event.objects.filter( date__gte = datetime.now() ).order_by('date')[:3]
|
|
|
|
|
2013-09-22 11:11:48 +02:00
|
|
|
if ( len(events) > 0 ):
|
2013-11-03 13:07:30 +01:00
|
|
|
i = 0
|
2013-09-22 11:11:48 +02:00
|
|
|
nextEvent = events[0]
|
2013-11-03 13:07:30 +01:00
|
|
|
while datetime.combine( nextEvent.date, nextEvent.time ) < datetime.now():
|
|
|
|
if len(events ) <= i:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
i += 1
|
|
|
|
nextEvent = events[i]
|
2013-10-14 19:46:53 +02:00
|
|
|
|
|
|
|
if EventParticipation.isMember( request.user ):
|
|
|
|
part = EventParticipation.objects.filter( user = request.user ).filter( event = nextEvent )
|
|
|
|
countdown['participation'] = part[0].status
|
2013-09-22 11:11:48 +02:00
|
|
|
|
2013-11-03 13:07:30 +01:00
|
|
|
eventTime = datetime( nextEvent.date.year, nextEvent.date.month, nextEvent.date.day,
|
|
|
|
nextEvent.displaytime.hour, nextEvent.displaytime.minute )
|
2013-09-22 11:11:48 +02:00
|
|
|
|
2013-11-03 13:07:30 +01:00
|
|
|
countdown['event'] = nextEvent
|
2013-09-26 21:56:32 +02:00
|
|
|
countdown['epoch'] = int( (eventTime - datetime.now() ).total_seconds() * 1000 )
|
2013-09-22 11:11:48 +02:00
|
|
|
|
2013-09-26 21:56:32 +02:00
|
|
|
context['countdown'] = countdown
|
2013-09-22 17:53:18 +02:00
|
|
|
|
|
|
|
|
2013-09-26 21:56:32 +02:00
|
|
|
# Route to concert
|
2013-09-22 17:53:18 +02:00
|
|
|
nextConcerts = Event.objects.filter( date__gte = datetime.now(), type = 'Conc' ).order_by('date')[:1]
|
|
|
|
if len( nextConcerts) > 0 :
|
|
|
|
nextConcert = nextConcerts[0]
|
|
|
|
|
|
|
|
routeInfo = dict()
|
|
|
|
|
2013-09-26 21:56:32 +02:00
|
|
|
musician = Musician.objects.get( user = request.user );
|
|
|
|
routeInfo['origin'] = musician.street + ", " + str( musician.zip_code ) + " " + musician.city
|
2013-09-22 17:53:18 +02:00
|
|
|
|
|
|
|
if nextConcert.map_location:
|
|
|
|
# map_location has format "lat,longitute,zoomlevel"
|
|
|
|
routeInfo['destination'] = ",".join( nextConcert.map_location.split(",")[:2] )
|
|
|
|
else:
|
|
|
|
routeInfo['destination'] = nextConcert.location
|
|
|
|
|
|
|
|
context['routeInfo'] = routeInfo
|
|
|
|
context['nextConcert'] = nextConcert
|
|
|
|
context['hasNextConcertInfo'] = True
|
|
|
|
else:
|
|
|
|
context['hasNextConcertInfo'] = False
|
2013-09-22 11:11:48 +02:00
|
|
|
|
2013-09-26 21:56:32 +02:00
|
|
|
|
2013-09-15 14:48:40 +02:00
|
|
|
return render( request, 'website/mainpage.html', context )
|
2013-06-15 21:50:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
def logout_view(request):
|
|
|
|
logout( request )
|
|
|
|
return redirect( login_view )
|
|
|
|
|
|
|
|
|
2013-09-28 18:57:19 +02:00
|
|
|
def userlistForAutocompletion(request):
|
|
|
|
result = [ u.username for u in User.objects.all() ]
|
|
|
|
return HttpResponse( simplejson.dumps(result), mimetype='application/json' )
|
|
|
|
|
|
|
|
|
2013-09-26 08:31:19 +02:00
|
|
|
def login_view( request ):
|
2013-06-15 21:50:12 +02:00
|
|
|
if request.method == 'POST': # If the form has been submitted...
|
2013-09-28 18:57:19 +02:00
|
|
|
raiseFirstLetter = lambda s: s[:1].upper() + s[1:] if s else ''
|
|
|
|
username = raiseFirstLetter( request.POST['username'] )
|
2013-06-15 21:50:12 +02:00
|
|
|
password = request.POST['password']
|
2013-09-28 18:57:19 +02:00
|
|
|
user = authenticate( username=username, password=password )
|
2013-06-15 21:50:12 +02:00
|
|
|
result = dict()
|
2013-09-26 08:31:19 +02:00
|
|
|
result['err'] = ""
|
2013-06-15 21:50:12 +02:00
|
|
|
if user is not None:
|
|
|
|
if user.is_active:
|
|
|
|
if not request.POST.get('remember', None):
|
2013-10-03 11:27:06 +02:00
|
|
|
# Expire in one year
|
|
|
|
request.session.set_expiry( timedelta( weeks=52 ) )
|
|
|
|
else:
|
|
|
|
# Expire on browser close
|
2013-06-15 21:50:12 +02:00
|
|
|
request.session.set_expiry( 0 )
|
2013-10-03 11:27:06 +02:00
|
|
|
|
2013-06-15 21:50:12 +02:00
|
|
|
login(request, user)
|
2013-09-22 11:11:48 +02:00
|
|
|
result['redirect'] = "/"
|
2013-09-26 08:31:19 +02:00
|
|
|
print ( "Setting Redirect" )
|
|
|
|
if 'next' in request.POST :
|
|
|
|
result['redirect'] = request.POST["next"]
|
|
|
|
print ( "Using " + request.POST["next"] )
|
2013-06-15 21:50:12 +02:00
|
|
|
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:
|
2013-09-26 08:31:19 +02:00
|
|
|
# Check if user already logged in
|
|
|
|
if request.user.is_authenticated():
|
|
|
|
return redirect( "/")
|
|
|
|
|
|
|
|
if 'next' in request.GET:
|
|
|
|
nextPage = request.GET['next']
|
|
|
|
else:
|
|
|
|
nextPage = "/"
|
|
|
|
return render( request, 'website/login.html', { 'next' : nextPage } )
|
2013-10-03 11:27:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import django.contrib.auth.views
|
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
|
|
|
|
def change_password( request ):
|
|
|
|
template_name = "website/change_password.html"
|
2013-10-14 19:46:53 +02:00
|
|
|
return django.contrib.auth.views.password_change(request, template_name, post_change_redirect= "/" )
|
2013-10-03 11:27:06 +02:00
|
|
|
|