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.contrib.auth.decorators import login_required
|
|
|
|
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
|
|
|
from django.contrib.auth.models import User
|
|
|
|
|
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:
|
|
|
|
participationQs = participationQs.filter( event__pk = eventId )
|
|
|
|
|
|
|
|
except EventParticipation.DoesNotExist:
|
2013-06-30 16:55:00 +02:00
|
|
|
return HttpResponse( status=404 )
|
2013-06-18 22:49:01 +02:00
|
|
|
|
|
|
|
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():
|
2013-06-30 16:55:00 +02:00
|
|
|
for serializedObject in serializer.object:
|
2013-09-26 21:56:32 +02:00
|
|
|
if serializedObject.user != request.user:
|
2013-06-30 16:55:00 +02:00
|
|
|
if not request.user.has_perm('change_others_participation') :
|
|
|
|
return Response( status = status.HTTP_403_FORBIDDEN )
|
|
|
|
|
2013-06-18 22:49:01 +02:00
|
|
|
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 16:55:00 +02:00
|
|
|
@login_required
|
|
|
|
def main_view( request ):
|
|
|
|
if request.user.has_perm( 'eventplanner.change_others_participation'):
|
|
|
|
return events_grid( request )
|
|
|
|
else:
|
|
|
|
return eventplanning( request )
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def eventplanning( request ):
|
2013-06-30 11:01:12 +02:00
|
|
|
"""
|
|
|
|
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() ) )
|
|
|
|
|
|
|
|
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 )
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-30 16:55:00 +02:00
|
|
|
@login_required
|
2013-06-30 11:01:12 +02:00
|
|
|
def events_grid( request ):
|
|
|
|
|
2013-09-26 21:56:32 +02:00
|
|
|
usernames = [ u.username for u in User.objects.all() ]
|
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-09-26 21:56:32 +02:00
|
|
|
e.participation = [ EventParticipation.get_or_create( event = e, user = u ) for u in User.objects.all() ]
|
|
|
|
|
|
|
|
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
|
|
|
@login_required
|
|
|
|
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-22 17:53:18 +02:00
|
|
|
fields= [ 'type', 'title', '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-06-30 16:55:00 +02:00
|
|
|
|
|
|
|
|
2013-09-13 21:04:59 +02:00
|
|
|
class EventCreate( CreateView ):
|
|
|
|
form_class = EventForm
|
|
|
|
model = Event
|
|
|
|
template_name_suffix = "_update_form"
|
|
|
|
success_url = '.'
|
2013-06-30 16:55:00 +02:00
|
|
|
|
2013-06-30 11:01:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|