From 6b0c8c49698d183882e441e3f966f28da20aad93 Mon Sep 17 00:00:00 2001
From: maxime_senza <maxime@happy-dev.fr>
Date: Fri, 8 May 2020 15:48:12 +0200
Subject: [PATCH] total views based on vote model

---
 .../{djangoldp-urls.py => djangoldp_urls.py}  | 17 ++++-------
 djangoldp_polls/models.py                     | 23 ++-------------
 djangoldp_polls/views.py                      | 28 ++++++++++++-------
 3 files changed, 25 insertions(+), 43 deletions(-)
 rename djangoldp_polls/{djangoldp-urls.py => djangoldp_urls.py} (57%)

diff --git a/djangoldp_polls/djangoldp-urls.py b/djangoldp_polls/djangoldp_urls.py
similarity index 57%
rename from djangoldp_polls/djangoldp-urls.py
rename to djangoldp_polls/djangoldp_urls.py
index 7c8f875..d31da44 100644
--- a/djangoldp_polls/djangoldp-urls.py
+++ b/djangoldp_polls/djangoldp_urls.py
@@ -13,19 +13,12 @@ Including another URLconf
     1. Import the include() function: from django.conf.urls import url, include
     2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
 """
-from django.conf.urls import url, include
-from django.contrib import admin
-from django.conf import settings
-from django.conf.urls.static import static
-from django.views import TotalVotes
+
+"""djangoldp project URL Configuration"""
+from django.conf.urls import url
+from .views import TotalVotes
 
 urlpatterns = [
-    url(r'^', include('djangoldp.urls')),
-    url(r'^admin/', admin.site.urls),
-    url(r'^/polls/?P<pk>[0-9]+/total_votes/$', TotalVotes.as_view(), name='api-poll-votes'),
+    url(r'votes/(?P<pk>.+)/total_votes/$', TotalVotes.as_view(), name='api-poll-votes'),
 
 ]
-
-if settings.DEBUG:
-  urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
-
diff --git a/djangoldp_polls/models.py b/djangoldp_polls/models.py
index 42f5fb7..ea01956 100644
--- a/djangoldp_polls/models.py
+++ b/djangoldp_polls/models.py
@@ -32,33 +32,14 @@ class PollOption (Model):
 
     class Meta :
         serializer_fields = ['@id','name']
-        nested_fields = []
+        nested_fields = ['userVote','pollOptions']
         anonymous_perms = ['view','add']
         authenticated_perms =  ['inherit','add']
 
 
     def __str__(self):
         return self.name
-
-
-class Debate (Model):
-    created_at = models.DateTimeField(auto_now_add=True)
-    author = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='debate_author')
-    comment = models.TextField(verbose_name="Long description")
-    #likes = key to likes model
-
-    class Meta :
-        auto_author = "author"
-        serializer_fields = ['@id','comment','author']
-        nested_fields = []
-        anonymous_perms = ['view','add']
-        authenticated_perms =  ['inherit','add']
-
-
-    def __str__(self):
-        return self.comment
-
-
+        
 class Poll (Model):
     created_at = models.DateTimeField(auto_now_add=True)
     author = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='createdVotes', null=True,blank=True)
diff --git a/djangoldp_polls/views.py b/djangoldp_polls/views.py
index b673be8..027c4b0 100644
--- a/djangoldp_polls/views.py
+++ b/djangoldp_polls/views.py
@@ -1,6 +1,10 @@
 from djangoldp.views import LDPViewSet
 from datetime import datetime
-from .models import Poll
+from .models import Poll,Vote
+from rest_framework.views import APIView
+from rest_framework.response import Response
+
+
 
 class FuturePollViewset(LDPViewSet):
     model = Poll
@@ -8,13 +12,17 @@ class FuturePollViewset(LDPViewSet):
         return super().get_queryset().filter(enddate__gte=datetime.now())
 
 class TotalVotes(APIView) : 
-	poll = Poll.objects.get(pk=pk)
-	# gets all votes for this poll
-	votes = poll.votes.all()
-	choices = []
-
-	for choice in poll.userVote.all():
-	     total_votes = votes.filter(chosenOption=choice).count()
-	     choices.append((choice.name, total_votes))
+	def get(self, request, pk):
+		vote = Vote.objects.get(pk=pk)
+		# gets all votes for this poll
+		votes = vote.relatedPoll.votes.all()
+		choices = []
 
-	# return response with choices in content
+		#calcute the number of votes per choice
+		for choice in vote.chosenOption.userVote.all():
+			#get the number of voter per option and count them
+		     total_votes = votes.filter(pollOptions=choice).count()
+		     choices.append((choice.name, total_votes))
+		# return response with choices in content
+		
+		return Response()
\ No newline at end of file
-- 
GitLab