コード例 #1
0
def poll_vote_create(request, thread, poll):
    poll.make_choices_votes_aware(request.user)

    allow_vote_poll(request.user, poll)

    serializer = NewVoteSerializer(
        data={
            'choices': request.data,
        },
        context={
            'allowed_choices': poll.allowed_choices,
            'choices': poll.choices,
        },
    )

    if not serializer.is_valid():
        return Response(
            {
                'detail': serializer.errors['choices'][0],
            },
            status=400,
        )

    remove_user_votes(request.user, poll, serializer.data['choices'])
    set_new_votes(request, poll, serializer.data['choices'])

    add_acl(request.user, poll)
    serialized_poll = PollSerializer(poll).data

    poll.choices = list(map(presave_clean_choice, deepcopy(poll.choices)))
    poll.save()

    return Response(serialized_poll)
コード例 #2
0
    def create(self, request, thread_pk):
        thread = self.get_thread_for_update(request, thread_pk)
        allow_start_poll(request.user, thread)

        try:
            if thread.poll and thread.poll.pk:
                raise PermissionDenied(
                    _("There's already a poll in this thread."))
        except Poll.DoesNotExist:
            pass

        instance = Poll(
            thread=thread,
            category=thread.category,
            poster=request.user,
            poster_name=request.user.username,
            poster_slug=request.user.slug,
            poster_ip=request.user_ip,
        )

        serializer = NewPollSerializer(instance, data=request.data)
        if serializer.is_valid():
            serializer.save()

            add_acl(request.user, instance)
            for choice in instance.choices:
                choice['selected'] = False

            thread.has_poll = True
            thread.save()

            return Response(PollSerializer(instance).data)
        else:
            return Response(serializer.errors, status=400)
コード例 #3
0
def poll_vote_create(request, thread, poll):
    poll.make_choices_votes_aware(request.user)

    allow_vote_poll(request.user, poll)

    serializer = NewVoteSerializer(
        # FIXME: lets use {'choices': []} JSON instead!
        data={'choices': request.data},
        context={
            'allowed_choices': poll.allowed_choices,
            'choices': poll.choices,
        },
    )

    serializer.is_valid(raise_exception=True)

    validated_choices = serializer.validated_data['choices']

    remove_user_votes(request.user, poll, validated_choices)
    set_new_votes(request, poll, validated_choices)

    add_acl(request.user, poll)
    serialized_poll = PollSerializer(poll).data

    poll.choices = list(map(presave_clean_choice, deepcopy(poll.choices)))
    poll.save()

    return Response(serialized_poll)
コード例 #4
0
    def update(self, request, thread_pk, pk=None):
        thread = self.get_thread(request, thread_pk)
        instance = self.get_poll(thread, pk)

        allow_edit_poll(request.user, instance)

        serializer = EditPollSerializer(instance, data=request.data)
        serializer.is_valid(raise_exception=True)

        serializer.save()

        add_acl(request.user, instance)
        instance.make_choices_votes_aware(request.user)

        return Response(PollSerializer(instance).data)
コード例 #5
0
    def update(self, request, thread_pk, pk):
        thread = self.get_thread_for_update(request, thread_pk)
        instance = self.get_poll(thread, pk)

        allow_edit_poll(request.user, instance)

        serializer = EditPollSerializer(instance, data=request.data)
        if serializer.is_valid():
            serializer.save()

            add_acl(request.user, instance)
            instance.make_choices_votes_aware(request.user)

            return Response(PollSerializer(instance).data)
        else:
            return Response(serializer.errors, status=400)
コード例 #6
0
def poll_vote_create(request, thread, poll):
    poll.make_choices_votes_aware(request.user)

    allow_vote_poll(request.user, poll)

    try:
        clean_votes = validate_votes(poll, request.data)
    except ValidationError as e:
        return Response({'detail': six.text_type(e)}, status=400)

    remove_user_votes(request.user, poll, clean_votes)
    set_new_votes(request, poll, clean_votes)

    add_acl(request.user, poll)
    serialized_poll = PollSerializer(poll).data

    poll.choices = list(map(presave_clean_choice, deepcopy(poll.choices)))
    poll.save()

    return Response(serialized_poll)