2013-06-18 22:49:01 +02:00
|
|
|
|
2013-09-26 21:56:32 +02:00
|
|
|
from django.shortcuts import render, redirect
|
2013-06-18 22:49:01 +02:00
|
|
|
from django.http import HttpResponse
|
2013-09-13 21:04:59 +02:00
|
|
|
from django.forms.models import ModelForm
|
2013-09-15 12:29:56 +02:00
|
|
|
from django.forms import TextInput
|
2013-06-18 22:49:01 +02:00
|
|
|
|
|
|
|
from models import Event, EventParticipation
|
2013-09-26 21:56:32 +02:00
|
|
|
|
2013-06-18 22:49:01 +02:00
|
|
|
|
|
|
|
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 16:55:00 +02:00
|
|
|
|
2013-06-18 22:49:01 +02:00
|
|
|
|
2013-09-13 21:04:59 +02:00
|
|
|
from crispy_forms.helper import FormHelper
|
|
|
|
from crispy_forms.layout import Submit
|
2013-06-18 22:49:01 +02:00
|
|
|
|
|
|
|
|
2013-06-30 16:55:00 +02:00
|
|
|
# ---------------------------------------- API ---------------------------------------------------------
|
2013-06-18 22:49:01 +02:00
|
|
|
|
2013-06-30 11:01:12 +02:00
|
|
|
|
2013-06-18 22:49:01 +02:00
|
|
|
@api_view( ['GET', 'PUT'] )
|
2013-06-30 16:55:00 +02:00
|
|
|
def event_api( request, username = None, eventId = None ):
|
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:
|
2013-09-26 21:56:32 +02:00
|
|
|
participationQs = EventParticipation.objects.filter( user__username = username )
|
2013-06-18 22:49:01 +02:00
|
|
|
if eventId:
|
2013-10-14 19:46:53 +02:00
|
|
|
participationQs = participationQs.filter( event__pk = eventId )
|
2013-06-18 22:49:01 +02:00
|
|
|
except EventParticipation.DoesNotExist:
|
2013-06-30 16:55:00 +02:00
|
|
|
return HttpResponse( status=404 )
|
2013-10-14 19:46:53 +02:00
|
|
|
|
|
|
|
|
2013-06-18 22:49:01 +02:00
|
|
|
if request.method == 'GET':
|
|
|
|
serializer = ParticipationSerializer( participationQs )
|
|
|
|
return Response( serializer.data )
|
|
|
|
|
|
|
|
elif request.method == 'PUT':
|
2013-10-14 19:46:53 +02:00
|
|
|
print "Request data" + str ( request.DATA )
|
2013-06-18 22:49:01 +02:00
|
|
|
serializer = ParticipationSerializer ( participationQs, data = request.DATA, many=True )
|
|
|
|
if serializer.is_valid():
|
2013-06-30 16:55:00 +02:00
|
|
|
for serializedObject in serializer.object:
|
2013-10-14 19:46:53 +02:00
|
|
|
if not EventParticipation.isMember( request.user ):
|
|
|
|
return Response( status = status.HTTP_403_FORBIDDEN )
|
2013-09-26 21:56:32 +02:00
|
|
|
if serializedObject.user != request.user:
|
2013-10-14 19:46:53 +02:00
|
|
|
if not EventParticipation.isAdmin( request.user ):
|
2013-06-30 16:55:00 +02:00
|
|
|
return Response( status = status.HTTP_403_FORBIDDEN )
|
|
|
|
|
2013-10-14 19:46:53 +02:00
|
|
|
print serializer.data
|
2013-06-18 22:49:01 +02:00
|
|
|
serializer.save()
|
|
|
|
return Response( serializer.data )
|
|
|
|
else:
|
2013-10-14 19:46:53 +02:00
|
|
|
print "In Bad request" + str(serializer.errors)
|
2013-06-18 22:49:01 +02:00
|
|
|
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 16:55:00 +02:00
|
|
|
def eventplanning( request ):
|
2013-06-30 11:01:12 +02:00
|
|
|
"""
|
|
|
|
View for a specific user, to edit his events
|
|
|
|
"""
|
2013-10-14 19:46:53 +02:00
|
|
|
# non-members see the grid - but cannot edit anything
|
|
|
|
if not EventParticipation.isMember( request.user ):
|
|
|
|
print "Not a member"
|
|
|
|
return events_grid(request)
|
|
|
|
|
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() ) )
|
|
|
|
|
|
|
|
for e in all_future_events:
|
2013-09-26 21:56:32 +02:00
|
|
|
e.participation = EventParticipation.get_or_create( event = e, user = request.user )
|
2013-06-18 22:49:01 +02:00
|
|
|
|
|
|
|
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 ):
|
2013-10-14 19:46:53 +02:00
|
|
|
usernames = [ u.username for u in EventParticipation.members() ]
|
2013-06-30 11:01:12 +02:00
|
|
|
|
|
|
|
all_future_events = list ( Event.objects.filter( date__gte = datetime.date.today() ) )
|
|
|
|
|
|
|
|
for e in all_future_events:
|
2013-10-14 19:46:53 +02:00
|
|
|
e.participation = [ EventParticipation.get_or_create( event = e, user = u ) for u in EventParticipation.members() ]
|
2013-09-26 21:56:32 +02:00
|
|
|
|
|
|
|
context = { 'events': all_future_events,
|
|
|
|
'usernames' : usernames }
|
2013-06-30 11:01:12 +02:00
|
|
|
|
|
|
|
return render ( request, 'eventplanner/events_grid.html', context )
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-09-13 21:04:59 +02:00
|
|
|
def deleteEvent( request, pk ):
|
|
|
|
Event.objects.get( pk = pk ).delete()
|
|
|
|
return redirect( events_grid )
|
2013-06-30 11:01:12 +02:00
|
|
|
|
2013-06-30 16:55:00 +02:00
|
|
|
# ------------------------------------ Detail Views ----------------------------------------------------
|
|
|
|
|
|
|
|
|
2013-09-13 21:04:59 +02:00
|
|
|
from django.views.generic.edit import UpdateView, CreateView
|
2013-06-30 11:01:12 +02:00
|
|
|
|
2013-09-22 17:53:18 +02:00
|
|
|
from location_field.widgets import LocationWidget
|
2013-06-30 11:01:12 +02:00
|
|
|
|
|
|
|
class EventForm( ModelForm ):
|
2013-09-13 21:04:59 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.helper = FormHelper()
|
|
|
|
self.helper.form_class = 'form-horizontal'
|
|
|
|
self.helper.add_input(Submit('submit', 'Speichern'))
|
|
|
|
return super(EventForm, self).__init__(*args, **kwargs)
|
|
|
|
|
2013-06-30 11:01:12 +02:00
|
|
|
class Meta:
|
|
|
|
model = Event
|
2013-09-28 18:57:19 +02:00
|
|
|
fields= [ 'type', 'short_desc', 'date', 'end_date', 'time', 'meeting_time', 'location', 'map_location', 'desc', ]
|
2013-09-13 21:04:59 +02:00
|
|
|
|
|
|
|
widgets = {
|
2013-09-22 17:53:18 +02:00
|
|
|
'location' : TextInput(),
|
|
|
|
'map_location' : LocationWidget(),
|
2013-09-13 21:04:59 +02:00
|
|
|
}
|
|
|
|
|
2013-06-30 11:01:12 +02:00
|
|
|
|
|
|
|
class EventUpdate( UpdateView ):
|
|
|
|
form_class = EventForm
|
|
|
|
model = Event
|
|
|
|
template_name_suffix = "_update_form"
|
2013-09-13 21:04:59 +02:00
|
|
|
success_url = '.'
|
2013-09-28 18:57:19 +02:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(UpdateView, self).get_context_data(**kwargs)
|
|
|
|
context['viewtype'] = "update"
|
|
|
|
return context
|
2013-06-30 16:55:00 +02:00
|
|
|
|
|
|
|
|
2013-09-13 21:04:59 +02:00
|
|
|
class EventCreate( CreateView ):
|
2013-09-28 18:57:19 +02:00
|
|
|
|
2013-09-13 21:04:59 +02:00
|
|
|
form_class = EventForm
|
|
|
|
model = Event
|
|
|
|
template_name_suffix = "_update_form"
|
|
|
|
success_url = '.'
|
2013-09-28 18:57:19 +02:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(CreateView, self).get_context_data(**kwargs)
|
|
|
|
context['viewtype'] = "create"
|
|
|
|
return context
|
2013-06-30 16:55:00 +02:00
|
|
|
|
2013-06-30 11:01:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|