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: try:
if poll.get_options().filter(candidate=candidate).exists(): polloption = poll.get_options().get(candidate=candidate)
# check config option 'publish_winner_results_only' # candidate is releated to this poll
if not publish_winner_results_only \ votes = {}
or publish_winner_results_only and self.is_elected(candidate): for vote in polloption.get_votes():
option = AssignmentOption.objects.filter(poll=poll).get(candidate=candidate) votes[vote.value] = vote.get_weight()
try: vote_results_dict[candidate].append(votes)
tmplist[1].append(option.get_votes()) except AssignmentOption.DoesNotExist:
except IndexError: # candidate not in releated to this poll
tmplist[1].append('') vote_results_dict[candidate].append(None)
else: return vote_results_dict
tmplist[1].append("")
else:
tmplist[1].append("-")
votes.append(tmplist)
return votes
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,45 +136,41 @@
</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"> {% if candidate in assignment.elected.all %}
{% with vote|first as candidate %} {% if perms.assignment.can_manage_assignment %}
{% if candidate.1 %} <a class="election_link elected" href='{% url assignment_user_not_elected assignment.id candidate.id %}'></a>
{% 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>
{% else %} {% else %}
{% if v != "-" %}<img src="{% static 'images/icons/voting-yes.png' %}" title="{% trans 'Yes' %}">{% endif %} <a class="elected"><img src="{% static 'images/icons/voting-yes.png' %}" title="{% trans 'Candidate is elected' %}"></a>
{{ v.0 }}
{% endif %} {% endif %}
{% else %} {% else %}
&empty; {% if perms.assignment.can_manage_assignment %}
<a class="election_link" href='{% url assignment_user_elected assignment.id candidate.id %}'></a>
{% endif %}
{% endif %} {% endif %}
{{ candidate }}
</td> </td>
{% endfor %} {% for vote in votes %}
{% if assignment.profile.exists and perms.assignment.can_manage_assignment and assignment.status == "vot" %} <td style="white-space:nowrap;">
<td></td> {% if vote %}
{% endif %} {% if 'Yes' in vote and 'No' in vote and 'Abstain' in vote %}
</tr> <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 %} {% endfor %}
<tr> <tr>
<td>{%trans 'Invalid votes' %}</td> <td>{%trans 'Invalid votes' %}</td>
@ -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)