Commit 7294447b authored by Hayat's avatar Hayat Committed by Bengfort
Browse files

make InvitationUpdateView work with both ids and uuids

parent d27ceec7
......@@ -53,7 +53,7 @@ class ScheduleFormMixin(LoginRequiredMixin):
form_class = ScheduleForm
def get_success_url(self):
return reverse('schedule-update', args=[self.object.pk])
return reverse('schedule-update', args=[self.object.uuid])
def form_valid(self, form, *args):
messages.success(self.request, _('Data has been saved.'))
......@@ -65,11 +65,14 @@ class ScheduleCreateView(ScheduleFormMixin, CreateView):
class ScheduleUpdateView(ScheduleFormMixin, UpdateView):
pass
slug_field = 'uuid'
slug_url_kwarg = 'uuid'
class ScheduleDeleteView(LoginRequiredMixin, DeleteView):
model = Schedule
slug_field = 'uuid'
slug_url_kwarg = 'uuid'
def get_success_url(self):
return reverse('index')
......@@ -82,10 +85,21 @@ class InvitationUpdateView(UpdateView):
fields = ['timeslot']
def get_object(self):
return get_object_or_404(Invitation, **self.kwargs)
if 'schedule_id' in self.kwargs:
return get_object_or_404(
Invitation,
schedule_id=self.kwargs['schedule_id'],
token=self.kwargs['token'],
)
if 'schedule_uuid' in self.kwargs:
return get_object_or_404(
Invitation,
schedule__uuid=self.kwargs['schedule_uuid'],
token=self.kwargs['token'],
)
def get_success_url(self):
return reverse('invitation', args=[self.object.schedule.pk, self.object.token])
return reverse('invitation', args=[self.object.schedule.uuid, self.object.token])
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
......@@ -143,17 +157,29 @@ class InvitationApiView(View):
raise PermissionDenied
return super().dispatch(request, *args, **kwargs)
def get_invitation_kwargs(self):
if 'schedule_id' in self.kwargs:
return {
'schedule_id': self.kwargs['schedule_id'],
'token': self.kwargs['token']
}
if 'schedule_uuid' in self.kwargs:
return {
'schedule_uuid': self.kwargs['schedule_uuid'],
'token': self.kwargs['token']
}
def get(self, request, *args, **kwargs):
invitation = get_object_or_404(Invitation, **kwargs)
invitation = get_object_or_404(Invitation, **self.get_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)
invitation, _ = Invitation.objects.get_or_create(**self.get_invitation_kwargs())
return HttpResponse(status=204)
def delete(self, request, *args, **kwargs):
invitation = get_object_or_404(Invitation, **kwargs)
invitation = get_object_or_404(Invitation, **self.get_invitation_kwargs())
invitation.delete()
return HttpResponse(status=204)
......@@ -35,16 +35,28 @@ from .main.views import ScheduleUpdateView
urlpatterns = [
path('', ScheduleListView.as_view(), name='index'),
path('new/', ScheduleCreateView.as_view(), name='schedule-create'),
path('<int:pk>/', ScheduleUpdateView.as_view(), name='schedule-update'),
path('<int:pk>/delete/', ScheduleDeleteView.as_view(), name='schedule-delete'),
path('<int:pk>/', ScheduleUpdateView.as_view(), name='schedule-update-legacy'),
path('<uuid:uuid>/', ScheduleUpdateView.as_view(), name='schedule-update'),
path('<int:pk>/delete/', ScheduleDeleteView.as_view(), name='schedule-delete-legacy'),
path('<uuid:uuid>/delete/', ScheduleDeleteView.as_view(), name='schedule-delete'),
path(
'invitations/<int:schedule_id>/<token>/',
InvitationUpdateView.as_view(),
name='invitation-legacy',
),
path(
'invitations/<uuid:schedule_uuid>/<token>/',
InvitationUpdateView.as_view(),
name='invitation',
),
path(
'api/<int:schedule_id>/<token>/',
InvitationApiView.as_view(),
name='api-invitation-legacy',
),
path(
'api/<uuid:schedule_uuid>/<token>/',
InvitationApiView.as_view(),
name='api-invitation',
),
path('login/', LoginView.as_view(), name='login'),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment