forms.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. from django.utils.translation import ugettext_lazy as _
  2. import floppyforms as forms
  3. from misago.apps.threadtype.posting.forms import NewThreadForm as NewThreadFormBase
  4. from misago.forms import Form
  5. from misago.validators import validate_sluggable
  6. class NewThreadForm(NewThreadFormBase):
  7. def type_fields(self):
  8. if self.request.acl.threads.can_make_polls(self.forum):
  9. self.add_field('poll_question',
  10. forms.CharField(label=_("Poll Question"),
  11. required=False))
  12. self.add_field('poll_choices',
  13. forms.CharField(label=_("Poll Choices"),
  14. help_text=_("Enter options poll members will vote on, every one in new line."),
  15. required=False,
  16. widget=forms.Textarea))
  17. self.add_field('poll_max_choices',
  18. forms.IntegerField(label=_("Choices Per User"),
  19. help_text=_("Select on how many options individual user will be able to vote on."),
  20. min_value=1,
  21. initial=1))
  22. self.add_field('poll_length',
  23. forms.IntegerField(label=_("Poll Length"),
  24. help_text=_("Number of days since poll creations users will be allowed to vote in poll. Enter zero for permanent poll."),
  25. min_value=0,
  26. initial=0))
  27. self.add_field('poll_public',
  28. forms.BooleanField(label=_("Public Voting"),
  29. required=False))
  30. self.add_field('poll_changing_votes',
  31. forms.BooleanField(label=_("Allow Changing Votes"),
  32. required=False))
  33. def clean_poll_question(self):
  34. data = self.cleaned_data['poll_question'].strip()
  35. if data:
  36. if len(data) < 3:
  37. raise forms.ValidationError(_("Poll quesiton should be at least three characters long."))
  38. if len(data) > 255:
  39. raise forms.ValidationError(_("Poll quesiton should be no longer than 250 characters."))
  40. return data
  41. def clean_poll_choices(self):
  42. self.clean_choices = []
  43. data = self.cleaned_data['poll_choices']
  44. if data:
  45. for choice in data.splitlines():
  46. choice = choice.strip()
  47. if not choice in self.clean_choices:
  48. if len(choice) < 3:
  49. raise forms.ValidationError(_("Poll choices should be at least three characters long."))
  50. if len(choice) > 250:
  51. raise forms.ValidationError(_("Poll choices should be no longer than 250 characters."))
  52. self.clean_choices.append(choice)
  53. if len(self.clean_choices) < 2:
  54. raise forms.ValidationError(_("Poll needs at least two choices."))
  55. if len(self.clean_choices) > 10:
  56. raise forms.ValidationError(_("Poll cannot have more than 10 choices."))
  57. return '\r\n'.join(self.clean_choices)
  58. def clean_poll_max_choices(self):
  59. data = self.cleaned_data['poll_max_choices']
  60. if data < 1:
  61. raise forms.ValidationError(_("Voters must be allowed to make at least one choice."))
  62. if data > len(self.clean_choices):
  63. raise forms.ValidationError(_("Users cannot cast more votes than there are options."))
  64. return data
  65. def clean_poll_length(self):
  66. data = self.cleaned_data['poll_length']
  67. if data < 0:
  68. raise forms.ValidationError(_("Poll length cannot be negative."))
  69. if data > 300:
  70. raise forms.ValidationError(_("Poll length cannot be longer than 300 days."))
  71. return data
  72. def clean(self):
  73. data = super(NewThreadForm, self).clean()
  74. try:
  75. if bool(data['poll_question']) != bool(self.clean_choices):
  76. if bool(data['poll_question']):
  77. raise forms.ValidationError(_("You have to define poll choices."))
  78. else:
  79. raise forms.ValidationError(_("You have to define poll question."))
  80. except KeyError:
  81. pass
  82. return data
  83. class PollVoteForm(Form):
  84. def __init__(self, *args, **kwargs):
  85. self.poll = kwargs.pop('poll')
  86. super(PollVoteForm, self).__init__(*args, **kwargs)
  87. def finalize_form(self):
  88. choices = []
  89. for choice in self.poll.option_set.all():
  90. choices.append((choice.pk, choice.name))
  91. if self.poll.max_choices > 1:
  92. self.add_field('options',
  93. forms.TypedMultipleChoiceField(choices=choices, coerce=int,
  94. widget=forms.CheckboxSelectMultiple))
  95. else:
  96. self.add_field('options',
  97. forms.TypedChoiceField(choices=choices, coerce=int,
  98. widget=forms.RadioSelect))