poll.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. from rest_framework import serializers
  2. from django.urls import reverse
  3. from django.utils.crypto import get_random_string
  4. from django.utils.translation import ugettext as _
  5. from django.utils.translation import ungettext
  6. from misago.threads.models import Poll
  7. __all__ = [
  8. 'PollSerializer',
  9. 'EditPollSerializer',
  10. 'NewPollSerializer',
  11. 'PollChoiceSerializer',
  12. ]
  13. MAX_POLL_OPTIONS = 16
  14. class PollSerializer(serializers.ModelSerializer):
  15. acl = serializers.SerializerMethodField()
  16. choices = serializers.SerializerMethodField()
  17. api = serializers.SerializerMethodField()
  18. url = serializers.SerializerMethodField()
  19. class Meta:
  20. model = Poll
  21. fields = (
  22. 'id',
  23. 'poster_name',
  24. 'posted_on',
  25. 'length',
  26. 'question',
  27. 'allowed_choices',
  28. 'allow_revotes',
  29. 'votes',
  30. 'is_public',
  31. 'acl',
  32. 'choices',
  33. 'api',
  34. 'url',
  35. )
  36. def get_api(self, obj):
  37. return {
  38. 'index': obj.get_api_url(),
  39. 'votes': obj.get_votes_api_url(),
  40. }
  41. def get_url(self, obj):
  42. return {
  43. 'poster': self.get_poster_url(obj),
  44. }
  45. def get_poster_url(self, obj):
  46. if obj.poster_id:
  47. return reverse('misago:user', kwargs={
  48. 'slug': obj.poster_slug,
  49. 'pk': obj.poster_id,
  50. })
  51. else:
  52. return None
  53. def get_acl(self, obj):
  54. try:
  55. return obj.acl
  56. except AttributeError:
  57. return None
  58. def get_choices(self, obj):
  59. return obj.choices
  60. class EditPollSerializer(serializers.ModelSerializer):
  61. length = serializers.IntegerField(required=True, min_value=0, max_value=180)
  62. question = serializers.CharField(required=True, max_length=255)
  63. allowed_choices = serializers.IntegerField(required=True, min_value=1)
  64. choices = serializers.ListField(
  65. allow_empty=False,
  66. child=serializers.DictField(),
  67. )
  68. class Meta:
  69. model = Poll
  70. fields = (
  71. 'length',
  72. 'question',
  73. 'allowed_choices',
  74. 'allow_revotes',
  75. 'choices',
  76. )
  77. def validate_choices(self, choices):
  78. clean_choices = list(map(self.clean_choice, choices))
  79. # generate hashes for added choices
  80. choices_map = {}
  81. for choice in self.instance.choices:
  82. choices_map[choice['hash']] = choice
  83. final_choices = []
  84. for choice in clean_choices:
  85. if choice['hash'] in choices_map:
  86. choices_map[choice['hash']].update({
  87. 'label': choice['label']
  88. })
  89. final_choices.append(choices_map[choice['hash']])
  90. else:
  91. choice.update({
  92. 'hash': get_random_string(12),
  93. 'votes': 0
  94. })
  95. final_choices.append(choice)
  96. self.validate_choices_num(final_choices)
  97. return final_choices
  98. def clean_choice(self, choice):
  99. clean_choice = {
  100. 'hash': choice.get('hash', get_random_string(12)),
  101. 'label': choice.get('label', ''),
  102. }
  103. serializer = PollChoiceSerializer(data=clean_choice)
  104. if not serializer.is_valid():
  105. raise serializers.ValidationError(_("One or more poll choices are invalid."))
  106. return serializer.data
  107. def validate_choices_num(self, choices):
  108. total_choices = len(choices)
  109. if total_choices < 2:
  110. raise serializers.ValidationError(_("You need to add at least two choices to a poll."))
  111. if total_choices > MAX_POLL_OPTIONS:
  112. message = ungettext(
  113. "You can't add more than %(limit_value)s option to a single poll (added %(show_value)s).",
  114. "You can't add more than %(limit_value)s options to a single poll (added %(show_value)s).",
  115. MAX_POLL_OPTIONS)
  116. raise serializers.ValidationError(message % {
  117. 'limit_value': MAX_POLL_OPTIONS,
  118. 'show_value': total_choices
  119. })
  120. def validate(self, data):
  121. if data['allowed_choices'] > len(data['choices']):
  122. raise serializers.ValidationError(
  123. _("Number of allowed choices can't be greater than number of all choices."))
  124. return data
  125. def update(self, instance, validated_data):
  126. if instance.choices:
  127. self.update_choices(instance, validated_data['choices'])
  128. return super(EditPollSerializer, self).update(instance, validated_data)
  129. def update_choices(self, instance, cleaned_choices):
  130. removed_hashes = []
  131. final_hashes = [c['hash'] for c in cleaned_choices]
  132. for choice in instance.choices:
  133. if choice['hash'] not in final_hashes:
  134. instance.votes -= choice['votes']
  135. removed_hashes.append(choice['hash'])
  136. if removed_hashes:
  137. instance.pollvote_set.filter(choice_hash__in=removed_hashes).delete()
  138. class NewPollSerializer(EditPollSerializer):
  139. class Meta:
  140. model = Poll
  141. fields = (
  142. 'length',
  143. 'question',
  144. 'allowed_choices',
  145. 'allow_revotes',
  146. 'is_public',
  147. 'choices',
  148. )
  149. def validate_choices(self, choices):
  150. clean_choices = list(map(self.clean_choice, choices))
  151. self.validate_choices_num(clean_choices)
  152. for choice in clean_choices:
  153. choice.update({
  154. 'hash': get_random_string(12),
  155. 'votes': 0
  156. })
  157. return clean_choices
  158. class PollChoiceSerializer(serializers.Serializer):
  159. hash = serializers.CharField(required=True, min_length=12, max_length=12)
  160. label = serializers.CharField(required=True, max_length=255)