142 lines
4.7 KiB
Python
142 lines
4.7 KiB
Python
from django.shortcuts import render, redirect
|
|
from django.http import HttpResponse
|
|
from django.forms.models import ModelForm
|
|
from django.forms import TextInput, TimeInput
|
|
|
|
from .models import Event, EventParticipation
|
|
|
|
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 crispy_forms.helper import FormHelper
|
|
from crispy_forms.layout import Submit
|
|
|
|
|
|
# ---------------------------------------- API ---------------------------------------------------------
|
|
|
|
|
|
@api_view(['GET', 'PUT'])
|
|
def event_api(request, username=None, eventId=None):
|
|
try:
|
|
participationQs = EventParticipation.objects.filter(event__date__gte=datetime.date.today())
|
|
if username:
|
|
participationQs = EventParticipation.objects.filter(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():
|
|
for serializedObject in serializer.object:
|
|
if not (EventParticipation.isMember(request.user) or EventParticipation.isAdmin(request.user)):
|
|
return Response(status=status.HTTP_403_FORBIDDEN)
|
|
if serializedObject.user != request.user:
|
|
if not EventParticipation.isAdmin(request.user):
|
|
return Response(status=status.HTTP_403_FORBIDDEN)
|
|
|
|
serializer.save()
|
|
return Response(serializer.data)
|
|
else:
|
|
return Response(status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
# ------------------------------------ Normal Views ----------------------------------------------------
|
|
|
|
def eventplanning(request):
|
|
"""
|
|
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)
|
|
|
|
# All events in the future sorted by date
|
|
all_future_events = list(Event.objects.filter(date__gte=datetime.date.today()).order_by('date'))
|
|
|
|
for e in all_future_events:
|
|
e.participation = EventParticipation.get_or_create(event=e, user=request.user)
|
|
|
|
context = {'events': all_future_events}
|
|
return render(request, 'eventplanner/eventplanning_view.html', context)
|
|
|
|
|
|
def events_grid(request):
|
|
usernames = [u.username for u in EventParticipation.members()]
|
|
|
|
all_future_events = list(Event.objects.filter(date__gte=datetime.date.today()).order_by('date'))
|
|
|
|
for e in all_future_events:
|
|
e.participation = [EventParticipation.get_or_create(event=e, user=u) for u in EventParticipation.members()]
|
|
|
|
context = {'events': all_future_events,
|
|
'usernames': usernames}
|
|
|
|
return render(request, 'eventplanner/events_grid.html', context)
|
|
|
|
|
|
def deleteEvent(request, pk):
|
|
Event.objects.get(pk=pk).delete()
|
|
return redirect(events_grid)
|
|
|
|
|
|
# ------------------------------------ Detail Views ----------------------------------------------------
|
|
|
|
|
|
from django.views.generic.edit import UpdateView, CreateView
|
|
|
|
from location_field.widgets import LocationWidget
|
|
|
|
|
|
class EventForm(ModelForm):
|
|
def __init__(self, *args, **kwargs):
|
|
self.helper = FormHelper()
|
|
self.helper.form_class = 'form-horizontal'
|
|
self.helper.add_input(Submit('submit', 'Speichern'))
|
|
super(EventForm, self).__init__(*args, **kwargs)
|
|
|
|
class Meta:
|
|
model = Event
|
|
fields = ['type', 'short_desc', 'date', 'end_date', 'time', 'meeting_time', 'location', 'map_location',
|
|
'desc', ]
|
|
|
|
widgets = {
|
|
'time': TimeInput(format='%H:%M'),
|
|
'location': TextInput(),
|
|
'map_location': LocationWidget(),
|
|
}
|
|
|
|
|
|
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
|