diff --git a/djangoldp_polls/__init__.py b/djangoldp_polls/__init__.py index 7a876269a902b69193d65dbf0054dacce255c89d..359e6f6f952c41d96ec8c4db5c3fa9275a637c9b 100644 --- a/djangoldp_polls/__init__.py +++ b/djangoldp_polls/__init__.py @@ -1,2 +1,2 @@ __version__ = '0.1.0' -name = "djangoldp_polls" +name = "djangoldp_polls" \ No newline at end of file diff --git a/djangoldp_polls/djangoldp_urls.py b/djangoldp_polls/djangoldp_urls.py index 1157ba4da6737cb7aae14f2de29b094cbed1f436..9ed6b6373726abc713e7ecf979d49c7f7ac6fdf0 100644 --- a/djangoldp_polls/djangoldp_urls.py +++ b/djangoldp_polls/djangoldp_urls.py @@ -15,10 +15,12 @@ Including another URLconf """ """djangoldp project URL Configuration""" -from django.conf.urls import url +from django.conf.urls import url,include +from django.contrib import admin from .views import TotalVotes +from djangoldp_poll.models import PollOption urlpatterns = [ - url(r'polls/(?P<pk>.+)/total_votes/$', TotalVotes.as_view(), name='api-poll-votes'), + url(r'^test/$', Testing.as_view()), -] +] \ No newline at end of file diff --git a/djangoldp_polls/serializers.py b/djangoldp_polls/serializers.py new file mode 100644 index 0000000000000000000000000000000000000000..39bc2ecef024bef60e58f298a89fe3cecb32f326 --- /dev/null +++ b/djangoldp_polls/serializers.py @@ -0,0 +1,9 @@ +from djangoldp.serializers import LDPSerializer +from djangoldp_polls.models import Poll,PollOption + +class PollOptionSerializer(LDPSerializer): + total_votes = serializers.SerializerMethodField() + + def get_total_votes(self, obj): + votes_queryset = self.context.get("votes_queryset") + return votes.filter(chosenOption=choice).count() \ No newline at end of file diff --git a/djangoldp_polls/views.py b/djangoldp_polls/views.py index d2b10b4287f8e8d4f74182a35abd52eb6a986210..44ec1fad45bd84d2260f19a1eae51a87a89b3b2c 100644 --- a/djangoldp_polls/views.py +++ b/djangoldp_polls/views.py @@ -11,18 +11,26 @@ class FuturePollViewset(LDPViewSet): def get_queryset(self): return super().get_queryset().filter(enddate__gte=datetime.now()) -class TotalVotes(APIView) : - def get(self, request, pk): - poll = Poll.objects.get(pk=pk) - # gets all votes for this poll - votes = poll.votes.all() - choices = [] - - #calcute the number of votes per choice - for choice in poll.pollOptions.all(): - #get the number of voter per option and count them - total_votes = votes.filter(chosenOption=choice).count() - choices.append((choice.name, total_votes)) - # return response with choices in content - - return Response(choices.total_votes) +class TotalVotes(LDPViewSet) : + list_actions = {'get': 'list'} + detail_actions = {} + + # view to GET the total counts of votes selecting a particular option + def list(self, request, pk, *args, **kwargs): + try: + poll = Poll.objects.get(pk=pk) + except Poll.DoesNotExist: + return Response(data={'error': 'could not get a Poll with this ID!'}, status=status.HTTP_404_NOT_FOUND) + + votes = poll.votes.all() + kwargs['context'] = self.get_serializer_context() + kwargs['context'].update({'votes_queryset': votes}) + choices = poll.userVote.all() + serializer = PollOptionSerializer(choices, many=True) + + return Response(serializer.data, status=status.HTTP_200_OK) + + +class Testing(APIView): + def get(self, request): + return Response("Hello world") \ No newline at end of file