rewrote assignment poll output

This commit is contained in:
Oskar Hahn 2012-07-03 00:05:48 +02:00
parent a21a96026b
commit 2dc0cc83f1
4 changed files with 67 additions and 82 deletions

View File

@ -82,6 +82,7 @@ class Assignment(models.Model, SlideMixin):
def candidates(self):
return self.profile.get_query_set()
def set_elected(self, profile, value=True):
if profile in self.candidates:
if value and not self.is_elected(profile):
@ -106,36 +107,39 @@ class Assignment(models.Model, SlideMixin):
poll.set_options([{'candidate': profile} for profile in candidates])
return poll
@property
def vote_results(self):
# TODO: Rewrite this code. Use a matrix and transform it.
votes = []
publish_winner_results_only = config["assignment_publish_winner_results_only"]
# list of votes
votes = []
options = []
"""
returns a table represented as a list with all candidates from all
releated polls and their vote results.
"""
vote_results_dict = {}
# All polls releated to this assigment
polls = self.poll_set.all()
# All PollOption-Objects releated to this assignment
options = []
for poll in polls:
options += poll.get_options()
for candidate in set([option.candidate for option in options]):
tmplist = [[candidate, self.is_elected(candidate)], []]
for option in options:
candidate = option.candidate
if candidate in vote_results_dict:
continue
vote_results_dict[candidate] = []
for poll in polls:
if poll.published:
if poll.get_options().filter(candidate=candidate).exists():
# check config option 'publish_winner_results_only'
if not publish_winner_results_only \
or publish_winner_results_only and self.is_elected(candidate):
option = AssignmentOption.objects.filter(poll=poll).get(candidate=candidate)
try:
tmplist[1].append(option.get_votes())
except IndexError:
tmplist[1].append('')
else:
tmplist[1].append("")
else:
tmplist[1].append("-")
votes.append(tmplist)
return votes
try:
polloption = poll.get_options().get(candidate=candidate)
# candidate is releated to this poll
votes = {}
for vote in polloption.get_votes():
votes[vote.value] = vote.get_weight()
vote_results_dict[candidate].append(votes)
except AssignmentOption.DoesNotExist:
# candidate not in releated to this poll
vote_results_dict[candidate].append(None)
return vote_results_dict
def get_agenda_title(self):
return self.name

View File

@ -103,7 +103,8 @@
<th></th>
{% with ballotnumber=polls.count %}
<th colspan="{{ ballotnumber|add:'1' }}" style="text-align: center;">
{% trans "ballot" %}</th>
{% trans "ballot" %}
</th>
{% endwith %}
</tr>
<tr>
@ -125,7 +126,7 @@
</th>
{# endif #}
{% endfor %}
{% if assignment.profile.count > 0 and perms.assignment.can_manage_assignment and assignment.status == "vot" %}
{% if assignment.profile.exists and perms.assignment.can_manage_assignment and assignment.status == "vot" %}
<th>
<a href='{% url assignment_gen_poll assignment.id %}'>
<span class="button">
@ -135,45 +136,41 @@
</th>
{% endif %}
</tr>
{% for vote in votes %}
<tr class="{% cycle 'odd' '' %}">
<td class="candidate">
{% with vote|first as candidate %}
{% if candidate.1 %}
{% if perms.assignment.can_manage_assignment %}
<a class="election_link elected" href='{% url assignment_user_not_elected assignment.id candidate.0.id %}'></a>
{% else %}
<a class="elected"><img src="{% static 'images/icons/voting-yes.png' %}" title="{% trans 'Candidate is elected' %}"></a>
{% endif %}
{% else %}
{% if perms.assignment.can_manage_assignment %}
<a class="election_link" href='{% url assignment_user_elected assignment.id candidate.0.id %}'></a>
{% endif %}
{% endif %}
{{ candidate.0 }}
{% endwith %}
</td>
{% for v in vote|last %}
<td style="white-space:nowrap;">
{% if v %}
{% if v|length == 3 %}
<img src="{% static 'images/icons/voting-yes.png' %}" title="{% trans 'Yes' %}"> {% if v.0 %}{{ v.0 }}{% else %}&empty;{% endif %}<br>
<img src="{% static 'images/icons/voting-no.png' %}" title="{% trans 'No' %}"> {% if v.1 %}{{ v.1 }}{% else %}&empty;{% endif %}<br>
<img src="{% static 'images/icons/voting-abstention.png' %}" title="{% trans 'Abstention' %}"> {% if v.2 %}{{ v.2 }}{% else %}&empty;{% endif %}<br>
{% for candidate, votes in vote_results.items %}
<tr class="{% cycle 'odd' '' %}">
<td class="candidate">
{% if candidate in assignment.elected.all %}
{% if perms.assignment.can_manage_assignment %}
<a class="election_link elected" href='{% url assignment_user_not_elected assignment.id candidate.id %}'></a>
{% else %}
{% if v != "-" %}<img src="{% static 'images/icons/voting-yes.png' %}" title="{% trans 'Yes' %}">{% endif %}
{{ v.0 }}
<a class="elected"><img src="{% static 'images/icons/voting-yes.png' %}" title="{% trans 'Candidate is elected' %}"></a>
{% endif %}
{% else %}
&empty;
{% if perms.assignment.can_manage_assignment %}
<a class="election_link" href='{% url assignment_user_elected assignment.id candidate.id %}'></a>
{% endif %}
{% endif %}
{{ candidate }}
</td>
{% endfor %}
{% if assignment.profile.exists and perms.assignment.can_manage_assignment and assignment.status == "vot" %}
<td></td>
{% endif %}
</tr>
{% for vote in votes %}
<td style="white-space:nowrap;">
{% if vote %}
{% if 'Yes' in vote and 'No' in vote and 'Abstain' in vote %}
<img src="{% static 'images/icons/voting-yes.png' %}" title="{% trans 'Yes' %}"> {% if vote.Yes %}{{ vote.Yes }}{% else %}&empty;{% endif %}<br>
<img src="{% static 'images/icons/voting-no.png' %}" title="{% trans 'No' %}"> {% if vote.No %}{{ vote.No }}{% else %}&empty;{% endif %}<br>
<img src="{% static 'images/icons/voting-abstention.png' %}" title="{% trans 'Abstention' %}"> {% if vote.Abstain %}{{ vote.Abstain }}{% else %}&empty;{% endif %}<br>
{% elif 'Votes' in vote %}
<img src="{% static 'images/icons/voting-yes.png' %}" title="{% trans 'Yes' %}"> {% if vote.Votes %}{{ vote.Votes }}{% else %}&empty;{% endif %}
{% endif %}
{% else %}
{% endif %}
</td>
{% endfor %}
{% if assignment.profile.exists and perms.assignment.can_manage_assignment and assignment.status == "vot" %}
<td></td>
{% endif %}
</tr>
{% endfor %}
<tr>
<td>{%trans 'Invalid votes' %}</td>
@ -190,7 +187,7 @@
<td><b>{%trans 'Votes cast' %}</b></td>
{% for poll in polls %}
{% if poll.published and not perms.assignment.can_manage_assignment or perms.assignment.can_manage_assignment %}
<td style="white-space:nowrap;"><img src="{% static 'images/icons/voting-total.png' %}" title="{% trans 'Votes cast' %}"> <b>{{ poll.print_votescast }}</b></td>
<td style="white-space:nowrap;"><img src="{% static 'images/icons/voting-total.png' %}" title="{% trans 'Votes cast' %}"> <strong>{{ poll.print_votescast }}</strong></td>
{% endif %}
{% endfor %}
{% if assignment.profile.exists and perms.assignment.can_manage_assignment and assignment.status == "vot" %}

View File

@ -84,31 +84,12 @@ def view(request, assignment_id=None):
if request.user.has_perm('assignment.can_nominate_other'):
form = AssignmentRunForm()
# why not use assignment.vote_results?
vote_results = assignment.vote_results
polls = assignment.poll_set.all()
votes = []
options = []
for poll in polls:
options += poll.get_options()
for candidate in set([option.candidate for option in options]):
tmplist = ((candidate, assignment.is_elected(candidate)), [])
for poll in polls:
if (poll.published and not request.user.has_perm('assignment.can_manage_assignment')) or request.user.has_perm('assignment.can_manage_assignment'):
# candidate exists in poll
if poll.get_options().filter(candidate=candidate).exists():
option = AssignmentOption.objects.filter(poll=poll).get(candidate=candidate)
try:
tmplist[1].append(poll.get_form_values(option.id))
except IndexError:
tmplist[1].append('')
else:
tmplist[1].append("-")
votes.append(tmplist)
return {
'assignment': assignment,
'form': form,
'votes': votes,
'vote_results': vote_results,
'polls': polls,
}

View File

@ -16,6 +16,7 @@ from projector.api import register_slidemodel
from projector.models import SlideMixin
from utils.translation_ext import ugettext as _
class BaseOption(models.Model):
poll = models.ForeignKey('BasePoll')
@ -194,5 +195,7 @@ def print_value(value):
value = _('majority')
elif value == -2:
value = _('undocumented')
elif value is None:
value = ''
return unicode(value)