Source code for events.api.v1.viewsets.event_registrations
from rest_framework.exceptions import PermissionDenied, NotFound
from rest_framework.mixins import RetrieveModelMixin, UpdateModelMixin
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet
from events import services
from events.api.v1.serializers import EventRegistrationSerializer
from events.exceptions import RegistrationError
from events.models import EventRegistration
from payments.exceptions import PaymentError
[docs]class EventRegistrationViewSet(RetrieveModelMixin, UpdateModelMixin, GenericViewSet):
"""Defines the viewset for registrations, requires an authenticated user.
Has custom update and destroy methods that use the services.
"""
queryset = EventRegistration.objects.all()
serializer_class = EventRegistrationSerializer
permission_classes = [IsAuthenticated]
[docs] def get_serializer_context(self):
context = super().get_serializer_context()
context["request"] = self.request
return context
[docs] def get_object(self):
instance = super().get_object()
if (
instance.name or instance.member.pk != self.request.member.pk
) and not services.is_organiser(self.request.member, instance.event):
raise NotFound()
return instance
[docs] def destroy(self, request, pk=None, **kwargs):
registration = self.get_object()
try:
services.cancel_registration(registration.member, registration.event)
return Response(status=204)
except RegistrationError as e:
raise PermissionDenied(detail=e) from e