threadpoll.py 4.6 KB

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