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): def candidates(self):
return self.profile.get_query_set() return self.profile.get_query_set()
def set_elected(self, profile, value=True): def set_elected(self, profile, value=True):
if profile in self.candidates: if profile in self.candidates:
if value and not self.is_elected(profile): 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]) poll.set_options([{'candidate': profile} for profile in candidates])
return poll return poll
@property @property
def vote_results(self): def vote_results(self):
# TODO: Rewrite this code. Use a matrix and transform it. """
votes = [] returns a table represented as a list with all candidates from all
publish_winner_results_only = config["assignment_publish_winner_results_only"] releated polls and their vote results.
# list of votes """
votes = [] vote_results_dict = {}
options = [] # All polls releated to this assigment
polls = self.poll_set.all() polls = self.poll_set.all()
# All PollOption-Objects releated to this assignment
options = []
for poll in polls: for poll in polls:
options += poll.get_options() 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: 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: try:
tmplist[1].append(option.get_votes()) polloption = poll.get_options().get(candidate=candidate)
except IndexError: # candidate is releated to this poll
tmplist[1].append('') votes = {}
else: for vote in polloption.get_votes():
tmplist[1].append("") votes[vote.value] = vote.get_weight()
else: vote_results_dict[candidate].append(votes)
tmplist[1].append("-") except AssignmentOption.DoesNotExist:
votes.append(tmplist) # candidate not in releated to this poll
return votes vote_results_dict[candidate].append(None)
return vote_results_dict
def get_agenda_title(self): def get_agenda_title(self):
return self.name return self.name

View File

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

View File

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