88 lines
2.7 KiB
Python
88 lines
2.7 KiB
Python
|
|
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
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
# ---------------------------------------- API ---------------------------------------------------------
|
|
|
|
@csrf_exempt
|
|
@api_view( ['GET'] )
|
|
def event_participation_list( request ):
|
|
"""
|
|
API for participation
|
|
"""
|
|
if request.method == 'GET':
|
|
event_participations = EventParticipation.objects.filter( event__date__gte = datetime.date.today() )
|
|
serializer = ParticipationSerializer( event_participations, many=True)
|
|
return Response( serializer.data )
|
|
|
|
elif request.method == 'POST':
|
|
serializer = ParticipationSerializer( data=request.DATA )
|
|
if serializer.is_valid():
|
|
print serializer.data
|
|
serializer.save()
|
|
return Response( serializer.data, status=status.HTTP_201_CREATED )
|
|
else:
|
|
return Response( serializer.errors, status = status.HTTP_400_BAD_REQUEST )
|
|
|
|
|
|
@csrf_exempt
|
|
@api_view( ['GET', 'PUT'] )
|
|
def event_participation_detail( request, username, eventId = None ):
|
|
try:
|
|
participationQs = EventParticipation.objects.filter( musician__user__username = username )
|
|
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 )
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------ Normal Views ----------------------------------------------------
|
|
|
|
|
|
|
|
|
|
def events_view( request ):
|
|
# 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 }
|
|
return render ( request, 'eventplanner/event_view.html', context )
|