2013-06-18 22:49:01 +02:00
|
|
|
|
|
|
|
from django.shortcuts import render, get_object_or_404
|
|
|
|
from django.http import HttpResponse
|
|
|
|
|
|
|
|
from models import Event, EventParticipation
|
|
|
|
from musicians.models import Musician
|
|
|
|
|
|
|
|
from serializers import ParticipationSerializer
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
|
|
|
|
from rest_framework.decorators import api_view
|
|
|
|
from rest_framework.response import Response
|
|
|
|
from rest_framework import status
|
2013-06-30 11:01:12 +02:00
|
|
|
from django.forms.models import ModelForm
|
|
|
|
|
2013-06-18 22:49:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------- API ---------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-30 11:01:12 +02:00
|
|
|
|
2013-06-18 22:49:01 +02:00
|
|
|
@api_view( ['GET', 'PUT'] )
|
2013-06-30 11:01:12 +02:00
|
|
|
def event_participation_detail( request, username = None, eventId = None ):
|
|
|
|
# TODO Permissions!
|
2013-06-18 22:49:01 +02:00
|
|
|
try:
|
2013-06-30 11:01:12 +02:00
|
|
|
participationQs = EventParticipation.objects.filter( event__date__gte = datetime.date.today() )
|
|
|
|
if username:
|
|
|
|
participationQs = EventParticipation.objects.filter( musician__user__username = username )
|
2013-06-18 22:49:01 +02:00
|
|
|
if eventId:
|
|
|
|
participationQs = participationQs.filter( event__pk = eventId )
|
|
|
|
|
|
|
|
except EventParticipation.DoesNotExist:
|
|
|
|
return HttpResponse( status=404)
|
|
|
|
|
|
|
|
|
|
|
|
if request.method == 'GET':
|
|
|
|
serializer = ParticipationSerializer( participationQs )
|
|
|
|
return Response( serializer.data )
|
|
|
|
|
|
|
|
elif request.method == 'PUT':
|
|
|
|
serializer = ParticipationSerializer ( participationQs, data = request.DATA, many=True )
|
|
|
|
if serializer.is_valid():
|
|
|
|
serializer.save()
|
|
|
|
return Response( serializer.data )
|
|
|
|
else:
|
|
|
|
return Response( status = status.HTTP_400_BAD_REQUEST )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-30 11:01:12 +02:00
|
|
|
|
2013-06-18 22:49:01 +02:00
|
|
|
# ------------------------------------ Normal Views ----------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-30 11:01:12 +02:00
|
|
|
def eventplanning_view( request ):
|
|
|
|
"""
|
|
|
|
View for a specific user, to edit his events
|
|
|
|
"""
|
2013-06-18 22:49:01 +02:00
|
|
|
# All events in the future sorted by date
|
|
|
|
all_future_events = list ( Event.objects.filter( date__gte = datetime.date.today() ) )
|
|
|
|
|
|
|
|
|
|
|
|
musician = get_object_or_404( Musician, user=request.user )
|
|
|
|
|
|
|
|
for e in all_future_events:
|
|
|
|
e.participation = EventParticipation.objects.get( event = e, musician = musician )
|
|
|
|
|
|
|
|
context = { 'events' : all_future_events }
|
2013-06-30 11:01:12 +02:00
|
|
|
return render ( request, 'eventplanner/eventplanning_view.html', context )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def events_grid( request ):
|
|
|
|
|
|
|
|
musicians = Musician.objects.all()
|
|
|
|
musicianNames = [ m.user.username for m in musicians ]
|
|
|
|
|
|
|
|
|
|
|
|
all_future_events = list ( Event.objects.filter( date__gte = datetime.date.today() ) )
|
|
|
|
|
|
|
|
for e in all_future_events:
|
|
|
|
e.participation = [ EventParticipation.objects.get( event = e, musician = m ) for m in musicians ]
|
|
|
|
|
|
|
|
# Sort the participations in a dict
|
|
|
|
#for p in participationQs:
|
|
|
|
# username = p.musician.user.username
|
|
|
|
# eventId = p.event.pk
|
|
|
|
# if not eventId in grid:
|
|
|
|
# grid[eventId] = {}
|
|
|
|
#
|
|
|
|
# grid[eventId][username] = p
|
|
|
|
|
|
|
|
|
|
|
|
context = { 'events': all_future_events,
|
|
|
|
'musicianNames' : musicianNames }
|
|
|
|
return render ( request, 'eventplanner/events_grid.html', context )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from django.views.generic.edit import UpdateView
|
|
|
|
|
|
|
|
|
|
|
|
class EventForm( ModelForm ):
|
|
|
|
class Meta:
|
|
|
|
model = Event
|
|
|
|
fields= ['title', 'date','time','type']
|
|
|
|
|
|
|
|
class EventUpdate( UpdateView ):
|
|
|
|
form_class = EventForm
|
|
|
|
model = Event
|
|
|
|
template_name_suffix = "_update_form"
|
|
|
|
success_url = '/events/grid'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|