From 03df8932babd08e1053f6ef73d98c8660352117c Mon Sep 17 00:00:00 2001 From: maxime_senza <maxime@happy-dev.fr> Date: Mon, 18 May 2020 11:42:43 +0200 Subject: [PATCH] working on total views --- djangoldp_polls/__init__.py | 2 +- djangoldp_polls/djangoldp_urls.py | 8 ++++--- djangoldp_polls/serializers.py | 9 ++++++++ djangoldp_polls/views.py | 38 +++++++++++++++++++------------ 4 files changed, 38 insertions(+), 19 deletions(-) create mode 100644 djangoldp_polls/serializers.py diff --git a/djangoldp_polls/__init__.py b/djangoldp_polls/__init__.py index 7a87626..359e6f6 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 1157ba4..9ed6b63 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 0000000..39bc2ec --- /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 d2b10b4..44ec1fa 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 -- GitLab