diff --git a/wahlfang_api/serializers.py b/wahlfang_api/serializers.py index 51044924c2a4f480cd3ef7f0aa1c90011780c650..956a29d5dbd3b9601f2e775e42548425af84a4a6 100644 --- a/wahlfang_api/serializers.py +++ b/wahlfang_api/serializers.py @@ -4,6 +4,7 @@ from rest_framework import serializers from rest_framework_simplejwt.serializers import TokenObtainPairSerializer from rest_framework_simplejwt.tokens import RefreshToken + from vote.models import Election, Session, Application, Voter diff --git a/wahlfang_api/urls.py b/wahlfang_api/urls.py index 3d04b19eac04d7b4bec174d69f33bfd6310b92e0..fe69745471cdac109159cdd39d472c189802c6d8 100644 --- a/wahlfang_api/urls.py +++ b/wahlfang_api/urls.py @@ -18,7 +18,6 @@ app_name = 'rest_api' router = routers.SimpleRouter() router.register('vote/elections', ElectionViewset) -router.register('management/add-session', ManagerSessionView) urlpatterns = [ path('', include(router.urls)), @@ -28,5 +27,6 @@ urlpatterns = [ path('auth/token/', TokenObtainElectionManagerView.as_view(), name='token_obtain_pair'), path('auth/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'), path('auth/token/verify/', TokenVerifyView.as_view(), name='token_verify'), + path('management/add-session', ManagerSessionView.as_view(), name='add_session'), path('drf/', include('rest_framework.urls', namespace='rest_framework')) ] diff --git a/wahlfang_api/views.py b/wahlfang_api/views.py index c49f872ede221894de136e2ed112b491450b08fd..85e8fbeb932df16fc8cffd6a70245a7545dcb0e8 100644 --- a/wahlfang_api/views.py +++ b/wahlfang_api/views.py @@ -9,7 +9,8 @@ from rest_framework_simplejwt.views import TokenViewBase from vote.forms import VoteForm from management.forms import AddSessionForm from vote.models import Election, Voter, Application, Session -from wahlfang_api.authentication import IsVoter +from wahlfang_api.authentication import IsVoter, IsElectionManager, ElectionManagerJWTAuthentication, \ + VoterJWTAuthentication from wahlfang_api.serializers import ( TokenObtainVoterSerializer, TokenObtainElectionManagerSerializer, @@ -40,6 +41,7 @@ class TokenObtainElectionManagerView(TokenViewBase): class VoterInfoView(generics.RetrieveAPIView): + authentication_classes = [VoterJWTAuthentication] queryset = Voter.objects.all() permission_classes = [IsVoter] serializer_class = VoterDetailSerializer @@ -56,23 +58,18 @@ class SpectatorView(generics.RetrieveAPIView): return self.queryset.get(spectator_token=self.kwargs['uuid']) -class ManagerSessionView(generics.RetrieveAPIView): +class ManagerSessionView(generics.ListCreateAPIView): + authentication_classes = [ElectionManagerJWTAuthentication] queryset = Session.objects.all() + permission_classes = [IsElectionManager] + serializer_class = SessionSerializer - @action(detail=True, methods=['post']) - def create_session(self, request): - user = self.request.user.pk - - form = AddSessionForm(request, user, data=request.data) - if form.is_valid(): - form.save() - return Response(status=status.HTTP_204_NO_CONTENT) - - return Response(data=form.errors, status=status.HTTP_400_BAD_REQUEST) - + def perform_create(self, serializer_class): + serializer_class.save() class ElectionViewset(viewsets.ReadOnlyModelViewSet): + authentication_classes = [VoterJWTAuthentication] queryset = Election.objects.all() permission_classes = [IsVoter] serializer_class = ElectionSerializer