diff --git a/djangoldp_polls/views.py b/djangoldp_polls/views.py index 750516be5b5a3000198ea06e059cf8fe7a66a355..7431a32b3c408d709dd70588265d8f2068e5a32b 100644 --- a/djangoldp_polls/views.py +++ b/djangoldp_polls/views.py @@ -1,22 +1,46 @@ from django.http import Http404 -from djangoldp.views import LDPViewSet +from djangoldp.views import LDPViewSet, NoCSRFAuthentication from datetime import datetime from rest_framework import status from rest_framework.views import APIView from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response +from rest_framework.decorators import permission_classes from .models import Poll,Vote from .serializers import PollOptionSerializer class CanVoteOnPollViewSet(APIView): - permission_classes = (IsAuthenticated,) - + authentication_classes = (NoCSRFAuthentication,) + + def dispatch(self, request, *args, **kwargs): + '''overriden dispatch method to append some custom headers''' + response = super(CanVoteOnPollViewSet, self).dispatch(request, *args, **kwargs) + response["Access-Control-Allow-Origin"] = request.META.get('HTTP_ORIGIN') + response["Access-Control-Allow-Methods"] = "GET,POST,PUT,PATCH,DELETE" + response["Access-Control-Allow-Headers"] = "authorization, Content-Type, if-match, accept, cache-control, pragma, user-agent" + response["Access-Control-Expose-Headers"] = "Location, User" + response["Access-Control-Allow-Credentials"] = 'true' + response["Accept-Post"] = "application/ld+json" + + if request.user.is_authenticated: + try: + response['User'] = request.user.webid() + except AttributeError: + pass + return response + + @permission_classes([IsAuthenticated]) def get(self, request, pk): - '''returns True if the user can vote, or False if they have already voted''' - headers = {"Access-Control-Allow-Origin" : request.META.get('HTTP_ORIGIN')} + # '''returns True if the user can vote, or False if they have already voted''' + headers = { + "Access-Control-Allow-Origin" : request.META.get('HTTP_ORIGIN'), + "Access-Control-Allow-Headers": "authorization, Content-Type, if-match, accept, cache-control, pragma, user-agent", + "Access-Control-Expose-Headers": "Location, User", + "Access-Control-Allow-Credentials": 'true' + } try: poll = Poll.objects.get(pk=pk) @@ -27,7 +51,7 @@ class CanVoteOnPollViewSet(APIView): except Poll.DoesNotExist: return Response(data={'error': {'poll': ['Could not find poll with this ID!']}}, - status=status.HTTP_404_NOT_FOUND) + status=status.HTTP_404_NOT_FOUND, headers=headers) class FuturePollViewset(LDPViewSet):