yes-no-abstain elections in assignments

This commit is contained in:
Oskar Hahn 2012-06-18 09:48:27 +02:00
parent bf46342d17
commit e8bf2fce1a
7 changed files with 41 additions and 15 deletions

View File

@ -41,11 +41,11 @@ class ConfigForm(Form, CssClassMixin):
assignment_pdf_ballot_papers_selection = forms.ChoiceField(widget=forms.Select(),
required=False,
label=_("Number of ballot papers (selection)"),
choices=[
choices=(
("1", _("Number of all delegates")),
("2", _("Number of all participants")),
("0", _("Use the following custum number"))
]
)
)
assignment_pdf_ballot_papers_number = forms.IntegerField(
widget=forms.TextInput(attrs={'class':'small-input'}),
@ -63,3 +63,12 @@ class ConfigForm(Form, CssClassMixin):
required=False,
label=_("Preamble text for PDF document (all elections)")
)
assignment_poll_vote_values = forms.ChoiceField(widget=forms.Select(),
required=False,
label=_("Values for the election"),
choices=(
("auto", _("Choose the right method.")),
("votes", _("One option per candidate.")),
("yesnoabstain", _("Yes-No-Abatain per candidate.")),
)
)

View File

@ -175,10 +175,29 @@ class AssignmentPoll(BasePoll, CountInvalid, CountVotesCast, PublishPollMixin):
option_class = AssignmentOption
assignment = models.ForeignKey(Assignment, related_name='poll_set')
yesnoababstain = models.NullBooleanField()
def get_assignment(self):
return self.assignment
def get_vote_values(self):
if not self.yesnoababstain:
if config['assignment_poll_vote_values'] == 'votes':
self.yesnoababstain = False
elif config['assignment_poll_vote_values'] == 'yesnoabstain':
self.yesnoababstain = True
else:
# candidates <= available posts -> yes/no/abstain
if self.assignment.candidates.count() <= self.assignment.posts - self.assignment.elected.count():
self.yesnoababstain = True
else:
self.yesnoababstain = False
self.save()
if self.yesnoababstain:
return ['yes', 'no', 'abstain']
else:
return ['votes']
def append_pollform_fields(self, fields):
CountInvalid.append_pollform_fields(self, fields)
CountVotesCast.append_pollform_fields(self, fields)
@ -206,4 +225,5 @@ def default_config(sender, key, **kwargs):
'assignment_pdf_ballot_papers_number': '1',
'assignment_pdf_title': _('Elections'),
'assignment_pdf_preamble': '',
'assignment_poll_vote_values': 'auto',
}.get(key)

View File

@ -162,7 +162,7 @@
<img src="{% static 'images/icons/voting-abstention.png' %}" title="{% trans 'Abstention' %}"> {% if v.2 %}{{ v.2 }}{% else %}&empty;{% endif %}<br>
{% else %}
{% if v != "-" %}<img src="{% static 'images/icons/voting-yes.png' %}" title="{% trans 'Yes' %}">{% endif %}
{{ v }}
{{ v.0 }}
{% endif %}
{% else %}
&empty;

View File

@ -91,7 +91,7 @@ def view(request, assignment_id=None):
if poll.get_options().filter(candidate=candidate).exists():
option = AssignmentOption.objects.filter(poll=poll).get(candidate=candidate)
try:
tmplist[1].append(option.get_votes()[0])
tmplist[1].append(poll.get_form_values(option.id))
except IndexError:
tmplist[1].append('')
else:
@ -577,6 +577,7 @@ class Config(FormView):
'assignment_pdf_ballot_papers_number': config['assignment_pdf_ballot_papers_number'],
'assignment_pdf_title': config['assignment_pdf_title'],
'assignment_pdf_preamble': config['assignment_pdf_preamble'],
'assignment_poll_vote_values': config['assignment_poll_vote_values'],
}
def form_valid(self, form):
@ -588,6 +589,7 @@ class Config(FormView):
config['assignment_pdf_ballot_papers_number'] = form.cleaned_data['assignment_pdf_ballot_papers_number']
config['assignment_pdf_title'] = form.cleaned_data['assignment_pdf_title']
config['assignment_pdf_preamble'] = form.cleaned_data['assignment_pdf_preamble']
config['assignment_poll_vote_values'] = form.cleaned_data['assignment_poll_vote_values']
messages.success(self.request, _('Election settings successfully saved.'))
return super(Config, self).form_valid(form)

View File

@ -25,14 +25,9 @@ class OptionForm(forms.Form, CssClassMixin):
super(OptionForm, self).__init__(*args, **kwargs)
for vote in extra:
if type(vote) is Vote:
key = vote.value
value = vote.get_value()
weight = vote.get_weight(raw=True)
else:
key = vote
value = _(vote)
weight = None
self.fields[key] = forms.IntegerField(
label=value,
initial=weight,

View File

@ -156,7 +156,7 @@ class BasePoll(models.Model, SlideMixin):
vote = Vote.objects.filter(option=option_id).get(value=value)
values.append(vote)
except Vote.DoesNotExist:
values.append(value)
values.append(Vote(value=value, weight=''))
return values
def get_vote_form(self, **kwargs):

View File

@ -68,7 +68,7 @@ class PollFormView(TemplateView):
for form in forms:
data = {}
for value in self.poll.vote_values:
for value in self.poll.get_vote_values():
data[value] = form.cleaned_data[value]
self.poll.set_form_values(form.option, data)