# 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 from django.contrib.auth.decorators import login_required from eventplanner.models import Event, EventParticipation from musicians.models import Musician from datetime import datetime from datetime import timedelta from django.contrib.auth.models import User @login_required def home_view(request): context = dict() # Event participation for slider text if EventParticipation.isMember( request.user ): context['hasParticipationSetForAllEvents'] = EventParticipation.hasUserSetParticipationForAllEvents( request.user) # Countdown countdown = dict() events = Event.objects.filter( date__gte = datetime.now() ).order_by('date')[:3] if ( len(events) > 0 ): i = 0 nextEvent = events[0] while nextEvent.displaydatetime < datetime.now(): if len(events ) <= i: return else: i += 1 nextEvent = events[i] if EventParticipation.isMember( request.user ): part = EventParticipation.objects.filter( user = request.user ).filter( event = nextEvent ) countdown['participation'] = part[0].status eventTime = nextEvent.displaydatetime countdown['event'] = nextEvent countdown['epoch'] = int( (eventTime - datetime.now() ).total_seconds() * 1000 ) context['countdown'] = countdown # Route to concert nextConcerts = Event.objects.filter( date__gte = datetime.now(), type = 'Conc' ).order_by('date')[:1] if len( nextConcerts) > 0 : nextConcert = nextConcerts[0] routeInfo = dict() musician = Musician.objects.get( user = request.user ); routeInfo['origin'] = musician.street + ", " + str( musician.zip_code ) + " " + musician.city 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 return render( request, 'website/mainpage.html', context ) def logout_view(request): logout( request ) return redirect( login_view ) def userlistForAutocompletion(request): result = [ u.username for u in User.objects.all() ] return HttpResponse( simplejson.dumps(result), mimetype='application/json' ) def login_view( request ): if request.method == 'POST': # If the form has been submitted... raiseFirstLetter = lambda s: s[:1].upper() + s[1:] if s else '' username = raiseFirstLetter( request.POST['username'] ) password = request.POST['password'] user = authenticate( username=username, password=password ) result = dict() result['err'] = "" if user is not None: if user.is_active: if not request.POST.get('remember', None): # Expire in one year request.session.set_expiry( timedelta( weeks=52 ) ) else: # Expire on browser close request.session.set_expiry( 0 ) login(request, user) result['redirect'] = "/" print ( "Setting Redirect" ) if 'next' in request.POST : result['redirect'] = request.POST["next"] print ( "Using " + request.POST["next"] ) 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: # 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 } ) import django.contrib.auth.views from django.core.urlresolvers import reverse def change_password( request ): template_name = "website/change_password.html" return django.contrib.auth.views.password_change(request, template_name, post_change_redirect= "/" )