poll.py 5.7 KB

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