forms.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. from django.utils.translation import ungettext, ugettext_lazy as _
  2. import floppyforms as forms
  3. from misago.apps.threadtype.posting.forms import NewThreadForm as NewThreadFormBase, EditThreadForm as EditThreadFormBase
  4. from misago.forms import Form
  5. from misago.validators import validate_sluggable
  6. class PollFormMixin(object):
  7. def create_poll_form(self):
  8. self.add_field('poll_question',
  9. forms.CharField(label=_("Poll Question"),
  10. required=False))
  11. self.add_field('poll_choices',
  12. forms.CharField(label=_("Poll Choices"),
  13. help_text=_("Enter options poll members will vote on, every one in new line."),
  14. required=False,
  15. widget=forms.Textarea))
  16. self.add_field('poll_max_choices',
  17. forms.IntegerField(label=_("Choices Per User"),
  18. help_text=_("Select on how many options individual user will be able to vote on."),
  19. min_value=1,
  20. initial=1))
  21. self.add_field('poll_length',
  22. forms.IntegerField(label=_("Poll Length"),
  23. help_text=_("Number of days since poll creations users will be allowed to vote in poll. Enter zero for permanent poll."),
  24. min_value=0,
  25. initial=0))
  26. self.add_field('poll_public',
  27. forms.BooleanField(label=_("Public Voting"),
  28. required=False))
  29. self.add_field('poll_changing_votes',
  30. forms.BooleanField(label=_("Allow Changing Votes"),
  31. required=False))
  32. def edit_poll_form(self):
  33. self.add_field('poll_question',
  34. forms.CharField(label=_("Poll Question"),
  35. initial=self.poll.question))
  36. self.add_field('poll_choices',
  37. forms.CharField(label=_("New Choices"),
  38. help_text=_("If you want, you can add new options to poll. Enter every option in new line."),
  39. required=False,
  40. widget=forms.Textarea))
  41. self.add_field('poll_max_choices',
  42. forms.IntegerField(label=_("Choices Per User"),
  43. help_text=_("Select on how many options individual user will be able to vote on."),
  44. min_value=1,
  45. initial=1))
  46. self.add_field('poll_length',
  47. forms.IntegerField(label=_("Poll Length"),
  48. help_text=_("Number of days since poll creations users will be allowed to vote in poll. Enter zero for permanent poll."),
  49. min_value=0,
  50. initial=0))
  51. self.add_field('poll_changing_votes',
  52. forms.BooleanField(label=_("Allow Changing Votes"),
  53. required=False))
  54. def clean_poll_question(self):
  55. data = self.cleaned_data['poll_question'].strip()
  56. if data:
  57. if len(data) < 3:
  58. raise forms.ValidationError(_("Poll quesiton should be at least three characters long."))
  59. if len(data) > 255:
  60. raise forms.ValidationError(_("Poll quesiton should be no longer than 250 characters."))
  61. return data
  62. def clean_poll_choices(self):
  63. self.clean_choices = []
  64. data = self.cleaned_data['poll_choices']
  65. if data:
  66. for choice in data.splitlines():
  67. choice = choice.strip()
  68. if not choice in self.clean_choices:
  69. if len(choice) < 3:
  70. raise forms.ValidationError(_("Poll choices should be at least three characters long."))
  71. if len(choice) > 250:
  72. raise forms.ValidationError(_("Poll choices should be no longer than 250 characters."))
  73. self.clean_choices.append(choice)
  74. if len(self.clean_choices) < 2:
  75. raise forms.ValidationError(_("Poll needs at least two choices."))
  76. if len(self.clean_choices) > 10:
  77. raise forms.ValidationError(_("Poll cannot have more than 10 choices."))
  78. return '\r\n'.join(self.clean_choices)
  79. def clean_poll_max_choices(self):
  80. data = self.cleaned_data['poll_max_choices']
  81. if data < 1:
  82. raise forms.ValidationError(_("Voters must be allowed to make at least one choice."))
  83. if data > len(self.clean_choices):
  84. raise forms.ValidationError(_("Users cannot cast more votes than there are options."))
  85. return data
  86. def clean_poll_length(self):
  87. data = self.cleaned_data['poll_length']
  88. if data < 0:
  89. raise forms.ValidationError(_("Poll length cannot be negative."))
  90. if data > 300:
  91. raise forms.ValidationError(_("Poll length cannot be longer than 300 days."))
  92. return data
  93. def clean_poll(self, data):
  94. data = super(NewThreadForm, self).clean()
  95. try:
  96. if bool(data['poll_question']) != bool(self.clean_choices):
  97. if bool(data['poll_question']):
  98. raise forms.ValidationError(_("You have to define poll choices."))
  99. else:
  100. raise forms.ValidationError(_("You have to define poll question."))
  101. except KeyError:
  102. pass
  103. return data
  104. class NewThreadForm(NewThreadFormBase, PollFormMixin):
  105. def type_fields(self):
  106. if self.request.acl.threads.can_make_polls(self.forum):
  107. self.create_poll_form()
  108. def clean(self):
  109. data = super(NewThreadForm, self).clean()
  110. data = self.clean_poll(data)
  111. return data
  112. class EditThreadForm(EditThreadFormBase, PollFormMixin):
  113. def type_fields(self):
  114. self.poll = self.thread.poll
  115. if self.poll:
  116. if self.request.acl.threads.can_edit_poll(self.forum, self.poll):
  117. self.edit_poll_form()
  118. else:
  119. if self.request.acl.threads.can_make_polls(self.forum):
  120. self.create_poll_form()
  121. if self.poll and self.request.acl.threads.can_delete_poll(self.forum, self.poll):
  122. self.add_field('poll_delete',
  123. forms.BooleanField(label=_("Delete poll"),
  124. required=False))
  125. def clean(self):
  126. data = super(EditThreadForm, self).clean()
  127. data = self.clean_poll(data)
  128. return data
  129. class PollVoteForm(Form):
  130. def __init__(self, *args, **kwargs):
  131. self.poll = kwargs.pop('poll')
  132. super(PollVoteForm, self).__init__(*args, **kwargs)
  133. def finalize_form(self):
  134. choices = []
  135. for choice in self.poll.choices_cache:
  136. choices.append((choice['pk'], choice['name']))
  137. if self.poll.max_choices > 1:
  138. self.add_field('options',
  139. forms.TypedMultipleChoiceField(choices=choices, coerce=int, required=False,
  140. widget=forms.CheckboxSelectMultiple))
  141. else:
  142. self.add_field('options',
  143. forms.TypedChoiceField(choices=choices, coerce=int, required=False,
  144. widget=forms.RadioSelect))
  145. def clean_options(self):
  146. data = self.cleaned_data['options']
  147. try:
  148. if not data:
  149. raise forms.ValidationError(_("You have to make selection."))
  150. if len(data) > self.poll.max_choices:
  151. raise forms.ValidationError(ungettext("You cannot select more than one option.",
  152. "You cannot select more than %(limit)s options.",
  153. self.poll.max_choices) % {'limit': self.poll.max_choices})
  154. except TypeError:
  155. pass
  156. return data