def save_vote(self, raw_data):
        serializer = VoteSerializer(data=raw_data)
        if serializer.is_valid():
            vote = serializer.save()
            return VoteSerializer(vote).data

        return serializer.errors
Example #2
0
 def post(self, request, pk, choice_pk):
     vote_by = request.data.get('vote_by')
     data = {'choice': choice_pk, 'poll': pk, 'vote_by': vote_by}
     serializer = VoteSerializer(data=data)
     if serializer.is_valid():
         vote = serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     else:
         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #3
0
    def vote(self, request,  pk=None):
        question = self.get_object()

        serializer = VoteSerializer(question=question, data=request.data)

        if serializer.is_valid():
            serializer.validated_data['choice'].vote()
            return Response({'result': 'vote accepted'})
        else:
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)
Example #4
0
 def post(self, request, pk, choice_pk):
     voted_by = request.query_params.get("voted_by")
     data = {"choice": choice_pk, "poll": pk, "voted_by": voted_by}
     serializer = VoteSerializer(data=data)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     return Response(serializer.data, status=status.HTTP_201_CREATED)
Example #5
0
    def post(self, request, poll_id=None):
        poll = Poll.objects.get(id=poll_id)

        serializer = VoteSerializer(data=request.data, poll=poll)
        if serializer.is_valid(raise_exception=True):
            uuid = serializer.validated_data.get('uuid')
            choices = serializer.validated_data.get('choices')
            answer = serializer.validated_data.get('answer')
            respondent = self.get_or_create_respondent(uuid, poll)

            if choices:
                respondent.choices.clear()
                for choice in choices:
                    respondents_choice = PollChoice.objects.get(choice=choice)
                    respondent.choices.add(respondents_choice)

            if answer:
                Answer.objects.get_or_create(text=answer,
                                             respondent=respondent,
                                             poll=poll)

            return Response(PollsSerializer(poll).data)
    def create(self, request, thread_id):
        thread = Thread.objects.get(id=thread_id)
        subject = thread.poll.votes.filter(user=request.user).first()

        if subject:
            return Response({"error": "Already voted!"},
                            status=status.HTTP_400_BAD_REQUEST)

        request.data['user'] = request.user.id
        serializer = VoteSerializer(data=request.data)

        if serializer.is_valid():
            self.perform_create(serializer)
            thread.poll.votes.add(serializer.instance)

            headers = self.get_success_headers(serializer.data)

            return Response(serializer.data,
                            status=status.HTTP_201_CREATED,
                            headers=headers)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
 def get_votes(self):
     votes = Vote.objects.filter(task__token=self.task_id)
     serializer = VoteSerializer(votes, many=True)
     return serializer.data