threadpoll.py 5.0 KB

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