Only show voteresult-table where there is at least one published poll.

Also fix AssignmentPDF-Poll-Table
This commit is contained in:
Oskar Hahn 2012-07-10 00:47:00 +02:00
parent cd245e4605
commit 333349a1f2
4 changed files with 110 additions and 132 deletions

View File

@ -108,8 +108,7 @@ class Assignment(models.Model, SlideMixin):
return poll
@property
def vote_results(self):
def vote_results(self, only_published):
"""
returns a table represented as a list with all candidates from all
related polls and their vote results.
@ -117,6 +116,8 @@ class Assignment(models.Model, SlideMixin):
vote_results_dict = {}
# All polls related to this assigment
polls = self.poll_set.all()
if only_published:
polls = polls.filter(published=True)
# All PollOption-Objects related to this assignment
options = []
for poll in polls:
@ -128,18 +129,15 @@ class Assignment(models.Model, SlideMixin):
continue
vote_results_dict[candidate] = []
for poll in polls:
votes = {
'votes': {},
'published': poll.published,
}
votes = {}
try:
# candidate related to this poll
poll_option = poll.get_options().get(candidate=candidate)
for vote in poll_option.get_votes():
votes['votes'][vote.value] = vote.get_weight()
votes[vote.value] = vote.get_weight()
except AssignmentOption.DoesNotExist:
# candidate not in related to this poll
votes['votes'] = None
votes = None
vote_results_dict[candidate].append(votes)
return vote_results_dict
@ -159,8 +157,8 @@ class Assignment(models.Model, SlideMixin):
data = super(Assignment, self).slide()
data['assignment'] = self
data['title'] = self.name
data['polls'] = self.poll_set.all()
data['vote_results'] = self.vote_results
data['polls'] = self.poll_set.filter(published=True)
data['vote_results'] = self.vote_results(only_published=True)
data['assignment_publish_winner_results_only'] = config['assignment_publish_winner_results_only']
data['template'] = 'projector/Assignment.html'
return data

View File

@ -112,9 +112,8 @@
<tr>
<th>{% trans "Candidates" %}</th>
{% for poll in polls %}
{% if poll.published or perms.assignment.can_manage_assignment %}
<th style="vertical-align: top; white-space:nowrap;">
{{ forloop.counter }}. {% trans 'ballot' %}
{{ poll.get_ballot }}. {% trans 'ballot' %}
{% if perms.assignment.can_manage_assignment %}
<br>
<a class="publish_link {% if poll.published %}published{% endif %}"
@ -126,7 +125,6 @@
<a href="{% url assignment_poll_delete poll.id %}"><img src="{% static 'images/icons/delete.png' %}" title="{% trans 'Delete' %}"></a>
{% endif %}
</th>
{% endif %}
{% endfor %}
{% if assignment.profile.exists and perms.assignment.can_manage_assignment and assignment.status == "vot" %}
<th>
@ -144,7 +142,6 @@
<td class="candidate">
{% if candidate in assignment.elected.all %}
{% if perms.assignment.can_manage_assignment %}
{# What does this line do #}
<a class="election_link elected" href='{% url assignment_user_not_elected assignment.id candidate.id %}'></a>
{% else %}
<a class="elected">
@ -158,9 +155,7 @@
{% endif %}
{{ candidate }}
</td>
{% for poll_dict in poll_list %}
{% if perms.assignment.can_manage_assignment or poll_dict.published %}
{% with vote=poll_dict.votes %}
{% for vote in poll_list %}
<td style="white-space:nowrap;">
{% if 'Yes' in vote and 'No' in vote and 'Abstain' in vote %}
<img src="{% static 'images/icons/voting-yes.png' %}" title="{% trans 'Yes' %}"> {{ vote.Yes }}<br>
@ -174,8 +169,6 @@
&nbsp;
{% endif %}
</td>
{% endwith %}
{% endif %}
{% endfor %}
{% if assignment.profile.exists and perms.assignment.can_manage_assignment and assignment.status == "vot" %}
<td></td>

View File

@ -54,10 +54,8 @@
<tr>
<th>{% trans "Candidates" %}</th>
{% for poll in assignment.poll_set.all %}
{% if poll.published %}
<th><nobr>{{forloop.counter}}. {% trans "ballot" %}</nobr></th>
{% endif %}
{% for poll in polls %}
<th><nobr>{{ poll.get_ballot }}. {% trans "ballot" %}</nobr></th>
{% endfor %}
</tr>
@ -71,9 +69,7 @@
{% endif %}
{{ candidate }}
</td>
{% for poll_dict in poll_list %}
{% if poll_dict.published %}
{% with vote=poll_dict.votes %}
{% for vote in poll_list %}
<td style="white-space:nowrap;"{% if candidate in assignment.elected.all %} class="elected"{% endif %}>
{% if not assignment_publish_winner_results_only or candidate in assignment.elected.all %}
{% if 'Yes' in vote and 'No' in vote and 'Abstain' in vote %}
@ -91,8 +87,6 @@
&nbsp;
{% endif %}
</td>
{% endwith %}
{% endif %}
{% endfor %}
</tr>
{% endfor %}

View File

@ -84,8 +84,14 @@ def view(request, assignment_id=None):
if request.user.has_perm('assignment.can_nominate_other'):
form = AssignmentRunForm()
vote_results = assignment.vote_results
polls = assignment.poll_set.all()
if not request.user.has_perm('assignment.can_manage_assignment'):
polls = assignment.poll_set.filter(published=True)
vote_results = assignment.vote_results(only_published=True)
else:
polls = assignment.poll_set.all()
vote_results = assignment.vote_results(only_published=False)
return {
'assignment': assignment,
'form': form,
@ -328,10 +334,12 @@ class AssignmentPDF(PDFView):
# Assignment details (each assignment on single page)
for assignment in assignments:
story.append(PageBreak())
story = self.get_assignment(assignment, story)
# append the assignment to the story-object
self.get_assignment(assignment, story)
else: # print selected assignment
assignment = Assignment.objects.get(id=assignment_id)
story = self.get_assignment(assignment, story)
# append the assignment to the story-object
self.get_assignment(assignment, story)
def get_assignment(self, assignment, story):
# title
@ -356,45 +364,49 @@ class AssignmentPDF(PDFView):
# Vote results
# Preparing
vote_results = assignment.vote_results
vote_results = assignment.vote_results(only_published=True)
polls = assignment.poll_set.filter(published=True)
data_votes = []
# Left side
cell3a = []
cell3a.append(Paragraph("%s:" % (_("Vote results")), stylesheet['Heading4']))
if assignment.poll_set.count() > 1:
cell3a.append(Paragraph("%s %s" % (assignment.poll_set.count(), _("ballots")), stylesheet['Normal']))
if polls.count() == 1:
cell3a.append(Paragraph("%s %s" % (polls.count(), _("ballot")), stylesheet['Normal']))
elif polls.count() > 1:
cell3a.append(Paragraph("%s %s" % (polls.count(), _("ballots")), stylesheet['Normal']))
# Add table head row
headrow = []
headrow.append(_("Candidates"))
for poll in assignment.poll_set.all():
if poll.published:
for poll in polls:
headrow.append("%s." % poll.get_ballot())
data_votes.append(headrow)
# Add result rows
elected_candidates = assignment.elected.all()
for candidate, poll_list in vote_results.iteritems():
row = []
if candidate in assignment.elected.all():
row.append("* %s" % str(candidate).split('(',1)[0])
else:
row.append(str(candidate).split('(',1)[0])
for poll_dict in poll_list:
if poll_dict['published']:
vote = poll_dict['votes']
candidate_string = candidate.user.get_full_name()
if candidate in elected_candidates:
candidate_string = "* " + candidate_string
if candidate.group:
candidate_string += "\n(%s)" % candidate.group
row.append(candidate_string)
for vote in poll_list:
if vote == None:
row.append(_('was not a \ncandidate'))
row.append('')
elif 'Yes' in vote and 'No' in vote and 'Abstain' in vote:
tmp = _("Y")+": "+str(vote['Yes'])+"\n"
tmp += _("N")+": "+str(vote['No'])+"\n"
tmp += _("A")+": "+str(vote['Abstain'])
tmp = _("Y")+": " + vote['Yes'] + "\n"
tmp += _("N")+": " + vote['No'] + "\n"
tmp += _("A")+": " + vote['Abstain']
row.append(tmp)
elif 'Votes' in vote:
row.append(str(vote['Votes']))
else:
pass
row.append(vote['Votes'])
else:
pass
data_votes.append(row)
@ -402,16 +414,14 @@ class AssignmentPDF(PDFView):
# Add votes invalid row
footrow_one = []
footrow_one.append(_("Invalid votes"))
for poll in assignment.poll_set.all():
if poll.published:
for poll in polls:
footrow_one.append(poll.print_votesinvalid())
data_votes.append(footrow_one)
# Add votes cast row
footrow_two = []
footrow_two.append(_("Votes cast"))
for poll in assignment.poll_set.all():
if poll.published:
for poll in polls:
footrow_two.append(poll.print_votescast())
data_votes.append(footrow_two)
@ -428,7 +438,7 @@ class AssignmentPDF(PDFView):
# table
data = []
data.append([cell1a,cell1b])
if table_votes:
if polls:
data.append([cell3a,table_votes])
data.append(['', '* = '+_('elected')])
else:
@ -442,26 +452,9 @@ class AssignmentPDF(PDFView):
]))
story.append(t)
story.append(Spacer(0,1*cm))
# text
story.append(Paragraph("%s" % assignment.description.replace('\r\n','<br/>'), stylesheet['Paragraph']))
return story
def get_assignment_votes(self, assignment):
votes = []
for candidate in assignment.candidates:
tmplist = ((candidate, assignment.is_elected(candidate)), [])
for poll in assignment.poll_set.all():
if poll.published:
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])
except IndexError:
tmplist[1].append('')
else:
tmplist[1].append("-")
votes.append(tmplist)
return votes
class CreateAgendaItem(RedirectView):