|
@@ -4,7 +4,6 @@ from misago.apps.threadtype.posting.forms import NewThreadForm as NewThreadFormB
|
|
|
from misago.forms import Form
|
|
|
from misago.validators import validate_sluggable
|
|
|
|
|
|
-
|
|
|
class PollFormMixin(object):
|
|
|
def create_poll_form(self):
|
|
|
self.add_field('poll_question',
|
|
@@ -37,7 +36,7 @@ class PollFormMixin(object):
|
|
|
forms.CharField(label=_("Poll Question"),
|
|
|
initial=self.poll.question))
|
|
|
self.add_field('poll_choices',
|
|
|
- forms.CharField(label=_("New Choices"),
|
|
|
+ forms.CharField(label=_("Add New Choices"),
|
|
|
help_text=_("If you want, you can add new options to poll. Enter every option in new line."),
|
|
|
required=False,
|
|
|
widget=forms.Textarea))
|
|
@@ -45,20 +44,21 @@ class PollFormMixin(object):
|
|
|
forms.IntegerField(label=_("Choices Per User"),
|
|
|
help_text=_("Select on how many options individual user will be able to vote on."),
|
|
|
min_value=1,
|
|
|
- initial=1))
|
|
|
+ initial=self.poll.max_choices))
|
|
|
self.add_field('poll_length',
|
|
|
forms.IntegerField(label=_("Poll Length"),
|
|
|
help_text=_("Number of days since poll creations users will be allowed to vote in poll. Enter zero for permanent poll."),
|
|
|
min_value=0,
|
|
|
- initial=0))
|
|
|
+ initial=self.poll.length))
|
|
|
|
|
|
self.add_field('poll_changing_votes',
|
|
|
forms.BooleanField(label=_("Allow Changing Votes"),
|
|
|
- required=False))
|
|
|
+ required=False,
|
|
|
+ initial=self.poll.vote_changing))
|
|
|
|
|
|
def clean_poll_question(self):
|
|
|
data = self.cleaned_data['poll_question'].strip()
|
|
|
- if data:
|
|
|
+ if data or self.poll:
|
|
|
if len(data) < 3:
|
|
|
raise forms.ValidationError(_("Poll quesiton should be at least three characters long."))
|
|
|
if len(data) > 255:
|
|
@@ -69,6 +69,9 @@ class PollFormMixin(object):
|
|
|
self.clean_choices = []
|
|
|
data = self.cleaned_data['poll_choices']
|
|
|
|
|
|
+ if self.poll:
|
|
|
+ self.clean_poll_edited_choices()
|
|
|
+
|
|
|
if data:
|
|
|
for choice in data.splitlines():
|
|
|
choice = choice.strip()
|
|
@@ -85,11 +88,26 @@ class PollFormMixin(object):
|
|
|
|
|
|
return '\r\n'.join(self.clean_choices)
|
|
|
|
|
|
+ def clean_poll_edited_choices(self):
|
|
|
+ self.changed_choices = []
|
|
|
+ self.deleted_choices = []
|
|
|
+
|
|
|
+ for option in self.poll.option_set.all():
|
|
|
+ new_name = self.request.POST.get('poll_current_choices[%s]' % option.pk, u'')
|
|
|
+ new_name = new_name.strip()
|
|
|
+ if new_name:
|
|
|
+ self.clean_choices.append(new_name)
|
|
|
+ if new_name != option.name:
|
|
|
+ option.name = new_name
|
|
|
+ self.changed_choices.append(option)
|
|
|
+ else:
|
|
|
+ self.deleted_choices.append(option)
|
|
|
+
|
|
|
def clean_poll_max_choices(self):
|
|
|
data = self.cleaned_data['poll_max_choices']
|
|
|
if data < 1:
|
|
|
raise forms.ValidationError(_("Voters must be allowed to make at least one choice."))
|
|
|
- if data > len(self.clean_choices):
|
|
|
+ if self.clean_choices and data > len(self.clean_choices):
|
|
|
raise forms.ValidationError(_("Users cannot cast more votes than there are options."))
|
|
|
return data
|
|
|
|
|
@@ -99,10 +117,18 @@ class PollFormMixin(object):
|
|
|
raise forms.ValidationError(_("Poll length cannot be negative."))
|
|
|
if data > 300:
|
|
|
raise forms.ValidationError(_("Poll length cannot be longer than 300 days."))
|
|
|
+ if self.poll:
|
|
|
+ org_length = self.poll.length
|
|
|
+ self.poll.length = data
|
|
|
+ try:
|
|
|
+ if self.poll.over:
|
|
|
+ raise forms.ValidationError(_("You cannot close poll that way."))
|
|
|
+ finally:
|
|
|
+ org_length = self.poll.length
|
|
|
+ self.poll.length = org_length
|
|
|
return data
|
|
|
|
|
|
def clean_poll(self, data):
|
|
|
- data = super(NewThreadForm, self).clean()
|
|
|
try:
|
|
|
if bool(data['poll_question']) != bool(self.clean_choices):
|
|
|
if bool(data['poll_question']):
|
|
@@ -116,6 +142,7 @@ class PollFormMixin(object):
|
|
|
|
|
|
class NewThreadForm(NewThreadFormBase, PollFormMixin):
|
|
|
def type_fields(self):
|
|
|
+ self.poll = None
|
|
|
if self.request.acl.threads.can_make_polls(self.forum):
|
|
|
self.create_poll_form()
|
|
|
|
|
@@ -128,6 +155,7 @@ class NewThreadForm(NewThreadFormBase, PollFormMixin):
|
|
|
class EditThreadForm(EditThreadFormBase, PollFormMixin):
|
|
|
def type_fields(self):
|
|
|
self.poll = self.thread.poll
|
|
|
+
|
|
|
if self.poll:
|
|
|
if self.request.acl.threads.can_edit_poll(self.forum, self.poll):
|
|
|
self.edit_poll_form()
|
|
@@ -175,4 +203,4 @@ class PollVoteForm(Form):
|
|
|
self.poll.max_choices) % {'limit': self.poll.max_choices})
|
|
|
except TypeError:
|
|
|
pass
|
|
|
- return data
|
|
|
+ return data
|