Newer
Older
# (c) 2020 MPIB <https://www.mpib-berlin.mpg.de/>,
#
# This file is part of castellum-scheduler.
#
# castellum-scheduler is free software; you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# Castellum is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with Castellum. If not, see
# <http://www.gnu.org/licenses/>.
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib import messages
from django.core.exceptions import PermissionDenied
from django.http import HttpResponse
from django.http import JsonResponse
from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.views.generic import CreateView
from django.views.generic import DeleteView
from django.views.generic import ListView
from django.views.generic import UpdateView
from .forms import ScheduleForm
from .models import Invitation
from .models import Schedule
class ScheduleListView(LoginRequiredMixin, ListView):
model = Schedule
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class ScheduleFormMixin(LoginRequiredMixin):
model = Schedule
form_class = ScheduleForm
def get_success_url(self):
return reverse('schedule-update', args=[self.object.pk])
def form_valid(self, form, *args):
messages.success(self.request, _('Data has been saved.'))
return super().form_valid(form, *args)
class ScheduleCreateView(ScheduleFormMixin, CreateView):
pass
class ScheduleUpdateView(ScheduleFormMixin, UpdateView):
pass
class ScheduleDeleteView(LoginRequiredMixin, DeleteView):
model = Schedule
def get_success_url(self):
return reverse('index')
class InvitationUpdateView(UpdateView):
model = Invitation
slug_field = 'token'
slug_url_kwarg = 'token'
fields = ['timeslot']
def get_object(self):
def get_success_url(self):
return reverse('invitation', args=[self.object.schedule.pk, self.object.token])
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
timeslots = list(self.object.schedule.timeslot_set.all())
dates = []
times = []
for timeslot in timeslots:
dates.append(timeslot.date)
times.append(timeslot.time)
dates = sorted(set(dates))
times = sorted(set(times))
table = OrderedDict()
for date in dates:
table[date] = OrderedDict()
for time in times:
table[date][time] = 0
for timeslot in timeslots:
table[timeslot.date][timeslot.time] = timeslot.id
context['table'] = {
'timeslots': table,
'times': times,
}
return context
def form_valid(self, form, *args):
ok = True
response = super().form_valid(form, *args)
if settings.PING_URL:
r = requests.post(settings.PING_URL.format(
schedule_id=self.object.schedule.id,
token=self.object.token,
))
ok = r.ok
if ok:
messages.success(self.request, _('Your reservation has been saved.'))
else:
self.object.timeslot = None
self.object.save()
messages.error(self.request, _('An error occured.'))
return response
@method_decorator(csrf_exempt, 'dispatch')
class InvitationApiView(View):
def dispatch(self, request, *args, **kwargs):
if request.headers.get('Authorization') != 'token ' + settings.API_TOKEN:
raise PermissionDenied
return super().dispatch(request, *args, **kwargs)
def get(self, request, *args, **kwargs):
invitation = get_object_or_404(Invitation, **kwargs)
return JsonResponse({
'datetime': invitation.timeslot.datetime if invitation.timeslot else None,
})
def put(self, request, *args, **kwargs):
invitation, _ = Invitation.objects.get_or_create(**kwargs)
return HttpResponse(status=204)
def delete(self, request, *args, **kwargs):
invitation = get_object_or_404(Invitation, **kwargs)
invitation.delete()
return HttpResponse(status=204)