blechreiz-website/eventplanner/views.py

142 lines
4.7 KiB
Python
Raw Normal View History

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
2023-01-03 20:13:35 +01:00
from django.forms import TextInput, TimeInput
2013-06-18 22:49:01 +02:00
from .models import Event, EventParticipation
2013-09-26 21:56:32 +02:00
from .serializers import ParticipationSerializer
2013-06-18 22:49:01 +02:00
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
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
@api_view(['GET', 'PUT'])
def event_api(request, username=None, eventId=None):
2013-06-18 22:49:01 +02:00
try:
participationQs = EventParticipation.objects.filter(event__date__gte=datetime.date.today())
2013-06-30 11:01:12 +02:00
if username:
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)
2013-06-18 22:49:01 +02:00
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)
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)
2013-06-18 22:49:01 +02:00
else:
return Response(status=status.HTTP_400_BAD_REQUEST)
2013-06-18 22:49:01 +02:00
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
"""
2014-04-18 13:43:02 +02:00
# 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()).order_by('date'))
2013-06-18 22:49:01 +02:00
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)
2013-06-30 11:01:12 +02:00
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()).order_by('date'))
2013-06-30 11:01:12 +02:00
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)
2013-06-30 11:01:12 +02:00
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'))
2023-01-03 20:13:35 +01:00
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 = {
2023-01-03 20:13:35 +01:00
'time': TimeInput(format='%H:%M'),
'location': TextInput(),
'map_location': LocationWidget(),
}
2013-06-30 11:01:12 +02:00
class EventUpdate(UpdateView):
2013-06-30 11:01:12 +02:00
form_class = EventForm
model = Event
2013-06-30 11:01:12 +02:00
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