threadpoll.py 4.9 KB

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