<commit_before>from django.shortcuts import get_object_or_404
from rest_framework import mixins
from rest_framework import viewsets

from adhocracy4.api.permissions import ViewSetRulesPermission
from apps.contrib.api.mixins import AllowPUTAsCreateMixin
from .models import Poll
from .models import Question
from .models import Vote
from .serializers import PollSerializer
from .serializers import VoteSerializer


class PollViewSet(mixins.UpdateModelMixin,
                  viewsets.GenericViewSet):
    queryset = Poll.objects.all()
    serializer_class = PollSerializer
    permission_classes = (ViewSetRulesPermission,)

    def get_permission_object(self):
        poll = self.get_object()
        return poll.module


class VoteViewSet(AllowPUTAsCreateMixin,
                  mixins.UpdateModelMixin,
                  viewsets.GenericViewSet):
    queryset = Vote.objects.all()
    serializer_class = VoteSerializer
    permission_classes = (ViewSetRulesPermission,)

    def dispatch(self, request, *args, **kwargs):
        self.question_pk = int(kwargs['pk'])
        return super().dispatch(request, *args, **kwargs)

    @property
    def question(self):
        return get_object_or_404(
            Question,
            pk=self.question_pk
        )

    def get_object(self):
        return get_object_or_404(
            Vote,
            creator=self.request.user,
            choice__question=self.question_pk
        )

    def get_permission_object(self):
        return self.question.poll.module

    def get_serializer_context(self):
        context = super(VoteViewSet, self).get_serializer_context()
        context.update({
            'question_pk': self.question_pk,
        })
        return context
<commit_msg>Fix vote submission on create.<commit_after>from django.shortcuts import get_object_or_404
from rest_framework import mixins
from rest_framework import viewsets

from adhocracy4.api.permissions import ViewSetRulesPermission
from apps.contrib.api.mixins import AllowPUTAsCreateMixin
from .models import Poll
from .models import Question
from .models import Vote
from .serializers import PollSerializer
from .serializers import VoteSerializer


class PollViewSet(mixins.UpdateModelMixin,
                  viewsets.GenericViewSet):
    queryset = Poll.objects.all()
    serializer_class = PollSerializer
    permission_classes = (ViewSetRulesPermission,)

    def get_permission_object(self):
        poll = self.get_object()
        return poll.module


class VoteViewSetRulesPermission(ViewSetRulesPermission):
    """Ensures the permission object is returned on update."""

    non_object_actions = ['list', 'create', 'update']


class VoteViewSet(AllowPUTAsCreateMixin,
                  mixins.UpdateModelMixin,
                  viewsets.GenericViewSet):
    queryset = Vote.objects.all()
    serializer_class = VoteSerializer
    permission_classes = (VoteViewSetRulesPermission,)

    def dispatch(self, request, *args, **kwargs):
        self.question_pk = int(kwargs['pk'])
        return super().dispatch(request, *args, **kwargs)

    @property
    def question(self):
        return get_object_or_404(
            Question,
            pk=self.question_pk
        )

    def get_object(self):
        return get_object_or_404(
            Vote,
            creator=self.request.user,
            choice__question=self.question_pk
        )

    def get_permission_object(self):
        return self.question.poll.module

    def get_serializer_context(self):
        context = super(VoteViewSet, self).get_serializer_context()
        context.update({
            'question_pk': self.question_pk,
        })
        return context

{'fraction_non_alphanumeric': 0.07602497963616617, 'fraction_numerical': 0.005430355688297583, 'mean_word_length': 3.0174482006543077, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6110752', 'n_tokens_mistral': 1037, 'n_tokens_neox': 981, 'n_words': 226}