threadpoll.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 ugettext 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):
  20. return self.thread(
  21. request,
  22. get_int_or_404(thread_pk),
  23. ).unwrap()
  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.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(request, thread_pk)
  38. allow_start_poll(request.user, thread)
  39. try:
  40. if thread.poll and thread.poll.pk:
  41. raise PermissionDenied(_("There's already a poll in this thread."))
  42. except Poll.DoesNotExist:
  43. pass
  44. instance = Poll(
  45. thread=thread,
  46. category=thread.category,
  47. poster=request.user,
  48. poster_name=request.user.username,
  49. poster_slug=request.user.slug,
  50. poster_ip=request.user_ip,
  51. )
  52. serializer = NewPollSerializer(instance, data=request.data)
  53. if serializer.is_valid():
  54. serializer.save()
  55. add_acl(request.user, instance)
  56. for choice in instance.choices:
  57. choice['selected'] = False
  58. thread.has_poll = True
  59. thread.save()
  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=None):
  65. thread = self.get_thread(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=None):
  78. thread = self.get_thread(request, thread_pk)
  79. instance = self.get_poll(thread, pk)
  80. allow_delete_poll(request.user, instance)
  81. thread.poll.delete()
  82. thread.has_poll = False
  83. thread.save()
  84. return Response({
  85. 'can_start_poll': can_start_poll(request.user, thread),
  86. })
  87. @detail_route(methods=['get', 'post'])
  88. def votes(self, request, thread_pk, pk=None):
  89. if request.method == 'POST':
  90. return self.post_votes(request, thread_pk, pk)
  91. else:
  92. return self.get_votes(request, thread_pk, pk)
  93. @transaction.atomic
  94. def post_votes(self, request, thread_pk, pk=None):
  95. thread = self.get_thread(request, thread_pk)
  96. instance = self.get_poll(thread, pk)
  97. return poll_vote_create(request, thread, instance)
  98. def get_votes(self, request, thread_pk, pk=None):
  99. poll_pk = get_int_or_404(pk)
  100. try:
  101. thread = self.get_thread(request, thread_pk)
  102. if thread.poll.pk != poll_pk:
  103. raise Http404()
  104. except Poll.DoesNotExist:
  105. raise Http404()
  106. allow_see_poll_votes(request.user, thread.poll)
  107. choices = []
  108. voters = {}
  109. for choice in thread.poll.choices:
  110. choice['voters'] = []
  111. voters[choice['hash']] = choice['voters']
  112. choices.append(choice)
  113. queryset = thread.poll.pollvote_set.values(
  114. 'voter_id', 'voter_name', 'voter_slug', 'voted_on', 'choice_hash'
  115. )
  116. for voter in queryset.order_by('voter_name').iterator():
  117. voters[voter['choice_hash']].append(PollVoteSerializer(voter).data)
  118. return Response(choices)
  119. class ThreadPollViewSet(ViewSet):
  120. thread = ForumThread