From 2dc0cc83f118ff1818af9a0e8d17554ef799ae36 Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Tue, 3 Jul 2012 00:05:48 +0200 Subject: [PATCH] rewrote assignment poll output --- openslides/assignment/models.py | 52 +++++++------- .../assignment/templates/assignment/view.html | 71 +++++++++---------- openslides/assignment/views.py | 23 +----- openslides/poll/models.py | 3 + 4 files changed, 67 insertions(+), 82 deletions(-) diff --git a/openslides/assignment/models.py b/openslides/assignment/models.py index dfb69d6c5..36759b0a9 100644 --- a/openslides/assignment/models.py +++ b/openslides/assignment/models.py @@ -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 diff --git a/openslides/assignment/templates/assignment/view.html b/openslides/assignment/templates/assignment/view.html index bf120d4d4..74ca2ca2b 100644 --- a/openslides/assignment/templates/assignment/view.html +++ b/openslides/assignment/templates/assignment/view.html @@ -103,7 +103,8 @@ {% with ballotnumber=polls.count %} - {% trans "ballot" %} + {% trans "ballot" %} + {% endwith %} @@ -125,7 +126,7 @@ {# 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" %} @@ -135,45 +136,41 @@ {% endif %} - - {% for vote in votes %} - - - {% with vote|first as candidate %} - {% if candidate.1 %} - {% if perms.assignment.can_manage_assignment %} - - {% else %} - - {% endif %} - {% else %} - {% if perms.assignment.can_manage_assignment %} - - {% endif %} - {% endif %} - {{ candidate.0 }} - {% endwith %} - - {% for v in vote|last %} - - {% if v %} - {% if v|length == 3 %} - {% if v.0 %}{{ v.0 }}{% else %}∅{% endif %}
- {% if v.1 %}{{ v.1 }}{% else %}∅{% endif %}
- {% if v.2 %}{{ v.2 }}{% else %}∅{% endif %}
+ {% for candidate, votes in vote_results.items %} + + + {% if candidate in assignment.elected.all %} + {% if perms.assignment.can_manage_assignment %} + {% else %} - {% if v != "-" %}{% endif %} - {{ v.0 }} + {% endif %} {% else %} - ∅ + {% if perms.assignment.can_manage_assignment %} + + {% endif %} {% endif %} + {{ candidate }} - {% endfor %} - {% if assignment.profile.exists and perms.assignment.can_manage_assignment and assignment.status == "vot" %} - - {% endif %} - + {% for vote in votes %} + + {% if vote %} + {% if 'Yes' in vote and 'No' in vote and 'Abstain' in vote %} + {% if vote.Yes %}{{ vote.Yes }}{% else %}∅{% endif %}
+ {% if vote.No %}{{ vote.No }}{% else %}∅{% endif %}
+ {% if vote.Abstain %}{{ vote.Abstain }}{% else %}∅{% endif %}
+ {% elif 'Votes' in vote %} + {% if vote.Votes %}{{ vote.Votes }}{% else %}∅{% endif %} + {% endif %} + {% else %} + – + {% endif %} + + {% endfor %} + {% if assignment.profile.exists and perms.assignment.can_manage_assignment and assignment.status == "vot" %} + + {% endif %} + {% endfor %} {%trans 'Invalid votes' %} @@ -190,7 +187,7 @@ {%trans 'Votes cast' %} {% for poll in polls %} {% if poll.published and not perms.assignment.can_manage_assignment or perms.assignment.can_manage_assignment %} - {{ poll.print_votescast }} + {{ poll.print_votescast }} {% endif %} {% endfor %} {% if assignment.profile.exists and perms.assignment.can_manage_assignment and assignment.status == "vot" %} diff --git a/openslides/assignment/views.py b/openslides/assignment/views.py index 451b24c80..bc6ff8a34 100644 --- a/openslides/assignment/views.py +++ b/openslides/assignment/views.py @@ -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, } diff --git a/openslides/poll/models.py b/openslides/poll/models.py index a01715e5b..706d8c503 100644 --- a/openslides/poll/models.py +++ b/openslides/poll/models.py @@ -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)