Andrei Kavaleu  committed 89b6777

#245 pool logic smail refactoring

Added validation for non zero values to PollForm.choice_count field (as models.PositiveSmallIntegerField allows zero values, so the validation added explicitly)
PollForm.create_poll() renamed to PollForm.has_pool()
Pool.auto_deactivate() renamed to Pool.deactivate_if_expired()
"poll.choice_count == 1" extracted to poll.single_choice()

  • Participants
  • Parent commits d7a107a

Comments (0)

Files changed (3)

File djangobb_forum/

         super(VotePollForm, self).__init__(*args, **kwargs)
         choices = self.poll.choices.all().values_list("id", "choice")
-        if self.poll.choice_count == 1:
+        if self.poll.single_choice():
             self.fields["choice"] = forms.ChoiceField(
                 choices=choices, widget=forms.RadioSelect
     def clean_choice(self):
         ids = self.cleaned_data["choice"]
-        if self.poll.choice_count == 1:
-            ids = [ids] # in a single choice scenario ChoiceField+RadioSelect are used, which return a value itself, not a list
+        if self.poll.single_choice(): # in a single choice scenario ChoiceField+RadioSelect are used (see above)
+            ids = [ids]               # which return a value itself, not a list
         count = len(ids)
         if count > self.poll.choice_count:
             raise forms.ValidationError(
     days = forms.IntegerField(required=False, min_value=1,
         help_text=_("Number of days for this poll to run. Leave empty for never ending poll.")
+    choice_count = forms.IntegerField(required=True, initial=1, min_value=1,
+        error_messages={'min_value': _("Number of choices must be positive.")},
+    )
     class Meta:
         model = Poll
         fields = ['question', 'choice_count']
-    def create_poll(self):
+    def has_data(self):
         return True if one field filled with data -> the user wants to create a poll

File djangobb_forum/

     users = models.ManyToManyField(User, blank=True, null=True,
         help_text=_("Users who has voted this poll."),
-    def auto_deactivate(self):
+    def deactivate_if_expired(self):
         if and self.deactivate_date:
             now =
             if now > self.deactivate_date:
        = False
+    def single_choice(self):
+        return self.choice_count == 1
     def __unicode__(self):
         return self.question

File djangobb_forum/

         poll = polls[0]
         if user_is_authenticated: # Only logged in users can vote
-            poll.auto_deactivate()
+            poll.deactivate_if_expired()
             has_voted = request.user in poll.users.all()
             if not post_request or not VotePollForm.FORM_NAME in request.POST:
                 # It's not a POST request or: The reply form was send and not a poll vote
             all_valid = False
         poll_form = PollForm(request.POST)
-        create_poll = poll_form.create_poll()
-        if not create_poll:
+        if not poll_form.has_data():
             # All poll fields are empty: User didn't want to create a poll
             # Don't run validation and remove all form error messages
             poll_form = PollForm() # create clean form without form errors
         if all_valid:
             post =
-            if create_poll:
+            if poll_form.has_data():
                 messages.success(request, _("Topic with poll saved."))