threadpoll.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. from django.core.exceptions import PermissionDenied
  2. from django.db import transaction
  3. from django.http import Http404
  4. from django.utils.translation import gettext as _
  5. from rest_framework import viewsets
  6. from rest_framework.decorators import detail_route
  7. from rest_framework.response import Response
  8. from misago.acl import add_acl
  9. from misago.core.shortcuts import get_int_or_404
  10. from ..models import Poll
  11. from ..permissions.polls import (
  12. allow_delete_poll,
  13. allow_edit_poll,
  14. allow_see_poll_votes,
  15. allow_start_poll,
  16. can_start_poll
  17. )
  18. from ..serializers import EditPollSerializer, NewPollSerializer, PollSerializer, PollVoteSerializer
  19. from ..viewmodels import ForumThread
  20. from .pollvotecreateendpoint import poll_vote_create
  21. class ViewSet(viewsets.ViewSet):
  22. thread = None
  23. def get_thread(self, request, thread_pk, select_for_update=False):
  24. return self.thread(
  25. request,
  26. get_int_or_404(thread_pk),
  27. select_for_update=select_for_update,
  28. ).unwrap()
  29. def get_thread_for_update(self, request, thread_pk):
  30. return self.get_thread(request, thread_pk, select_for_update=True)
  31. def get_poll(self, thread, pk):
  32. try:
  33. poll_id = get_int_or_404(pk)
  34. if thread.poll.pk != poll_id:
  35. raise Http404()
  36. poll = Poll.objects.select_for_update().get(pk=thread.poll.pk)
  37. poll.thread = thread
  38. poll.category = thread.category
  39. return poll
  40. except Poll.DoesNotExist:
  41. raise Http404()
  42. @transaction.atomic
  43. def create(self, request, thread_pk):
  44. thread = self.get_thread_for_update(request, thread_pk)
  45. allow_start_poll(request.user, thread)
  46. try:
  47. if thread.poll and thread.poll.pk:
  48. raise PermissionDenied(_("There's already a poll in this thread."))
  49. except Poll.DoesNotExist:
  50. pass
  51. instance = Poll(
  52. thread=thread,
  53. category=thread.category,
  54. poster=request.user,
  55. poster_name=request.user.username,
  56. poster_slug=request.user.slug,
  57. poster_ip=request.user_ip,
  58. )
  59. serializer = NewPollSerializer(instance, data=request.data)
  60. if serializer.is_valid():
  61. serializer.save()
  62. add_acl(request.user, instance)
  63. for choice in instance.choices:
  64. choice['selected'] = False
  65. thread.has_poll = True
  66. thread.save()
  67. return Response(PollSerializer(instance).data)
  68. else:
  69. return Response(serializer.errors, status=400)
  70. @transaction.atomic
  71. def update(self, request, thread_pk, pk):
  72. thread = self.get_thread_for_update(request, thread_pk)
  73. instance = self.get_poll(thread, pk)
  74. allow_edit_poll(request.user, instance)
  75. serializer = EditPollSerializer(instance, data=request.data)
  76. if serializer.is_valid():
  77. serializer.save()
  78. add_acl(request.user, instance)
  79. instance.make_choices_votes_aware(request.user)
  80. return Response(PollSerializer(instance).data)
  81. else:
  82. return Response(serializer.errors, status=400)
  83. @transaction.atomic
  84. def delete(self, request, thread_pk, pk):
  85. thread = self.get_thread_for_update(request, thread_pk)
  86. instance = self.get_poll(thread, pk)
  87. allow_delete_poll(request.user, instance)
  88. thread.poll.delete()
  89. thread.has_poll = False
  90. thread.save()
  91. return Response({
  92. 'can_start_poll': can_start_poll(request.user, thread)
  93. })
  94. @detail_route(methods=['get', 'post'])
  95. def votes(self, request, thread_pk, pk):
  96. if request.method == 'POST':
  97. return self.post_votes(request, thread_pk, pk)
  98. else:
  99. return self.get_votes(request, thread_pk, pk)
  100. @transaction.atomic
  101. def post_votes(self, request, thread_pk, pk):
  102. thread = self.get_thread_for_update(request, thread_pk)
  103. instance = self.get_poll(thread, pk)
  104. return poll_vote_create(request, thread, instance)
  105. def get_votes(self, request, thread_pk, pk):
  106. poll_pk = get_int_or_404(pk)
  107. try:
  108. thread = self.get_thread(request, thread_pk)
  109. if thread.poll.pk != poll_pk:
  110. raise Http404()
  111. except Poll.DoesNotExist:
  112. raise Http404()
  113. allow_see_poll_votes(request.user, thread.poll)
  114. choices = []
  115. voters = {}
  116. for choice in thread.poll.choices:
  117. choice['voters'] = []
  118. voters[choice['hash']] = choice['voters']
  119. choices.append(choice)
  120. queryset = thread.poll.pollvote_set.values(
  121. 'voter_id', 'voter_name', 'voter_slug', 'voted_on', 'choice_hash')
  122. for voter in queryset.order_by('voter_name').iterator():
  123. voters[voter['choice_hash']].append(PollVoteSerializer(voter).data)
  124. return Response(choices)
  125. class ThreadPollViewSet(ViewSet):
  126. thread = ForumThread