blechreiz-website/eventplanner/views.py

152 lines
4.9 KiB
Python
Raw Normal View History

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
from django.forms.models import ModelForm
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-18 22:49:01 +02:00
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
2013-06-18 22:49:01 +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'] )
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 )
2013-06-18 22:49:01 +02:00
except EventParticipation.DoesNotExist:
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':
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():
for serializedObject in serializer.object:
2013-10-15 21:51:05 +02:00
if not ( EventParticipation.isMember( request.user ) or EventParticipation.isAdmin( request.user ) ):
return Response( status = status.HTTP_403_FORBIDDEN )
2013-09-26 21:56:32 +02:00
if serializedObject.user != request.user:
if not EventParticipation.isAdmin( request.user ):
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 ----------------------------------------------------
def eventplanning( request ):
2013-06-30 11:01:12 +02:00
"""
View for a specific user, to edit his events
"""
# non-members see the grid - but cannot edit anything
if not EventParticipation.isMember( request.user ):
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 ):
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:
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 )
def deleteEvent( request, pk ):
Event.objects.get( pk = pk ).delete()
return redirect( events_grid )
2013-06-30 11:01:12 +02:00
# ------------------------------------ Detail Views ----------------------------------------------------
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 ):
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
fields= [ 'type', 'short_desc', 'date', 'end_date', 'time', 'meeting_time', 'location', 'map_location', 'desc', ]
widgets = {
2013-09-22 17:53:18 +02:00
'location' : TextInput(),
'map_location' : LocationWidget(),
}
2013-06-30 11:01:12 +02:00
class EventUpdate( UpdateView ):
form_class = EventForm
model = Event
template_name_suffix = "_update_form"
success_url = '.'
def get_context_data(self, **kwargs):
context = super(UpdateView, self).get_context_data(**kwargs)
context['viewtype'] = "update"
return context
class EventCreate( CreateView ):
form_class = EventForm
model = Event
template_name_suffix = "_update_form"
success_url = '.'
def get_context_data(self, **kwargs):
context = super(CreateView, self).get_context_data(**kwargs)
context['viewtype'] = "create"
return context
2013-06-30 11:01:12 +02:00