From 70f1cb8b3e3dff802e78916cb21fecba56f89cfd Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Sun, 19 Feb 2012 19:27:00 +0100 Subject: [PATCH] use the new poll-api in assignments --- openslides/application/models.py | 8 +- .../templates/application/view.html | 4 +- openslides/assignment/models.py | 51 ++++----- .../templates/assignment/poll_view.html | 49 ++++---- .../assignment/templates/assignment/view.html | 95 ++++++++-------- openslides/assignment/urls.py | 8 +- openslides/assignment/views.py | 105 +++++++----------- openslides/poll/models.py | 18 ++- 8 files changed, 159 insertions(+), 179 deletions(-) diff --git a/openslides/application/models.py b/openslides/application/models.py index 3eb00d636..728ae393d 100644 --- a/openslides/application/models.py +++ b/openslides/application/models.py @@ -496,12 +496,7 @@ class ApplicationOption(BaseOption): def __getattr__(self, name): if name in ['yes', 'no', 'contained']: try: - value = self.get_votes().get(value=name).weight - if value == -1: - return _('majority') - if value == -2: - return _('undocumented') - return value + return self.get_votes().get(value=name) except Vote.DoesNotExist: pass raise AttributeError(name) @@ -517,6 +512,7 @@ class ApplicationPoll(BasePoll, CountInvalid, CountVotesCast): return self.application def set_options(self): + #TODO: maybe it is possible with .create() to call this without poll=self self.get_option_class()(poll=self).save() def append_pollform_fields(self, fields): diff --git a/openslides/application/templates/application/view.html b/openslides/application/templates/application/view.html index 10cc82ce2..cad41af32 100644 --- a/openslides/application/templates/application/view.html +++ b/openslides/application/templates/application/view.html @@ -79,9 +79,9 @@ {{ option.yes }}
{{ option.no }}
{{ option.contained }}
- {{ poll.votesinvalid }}
+ {{ poll.print_votesinvalid }}
- {{ poll.votescast }} + {{ poll.poll_votescast }}
{% endwith %} {% if perms.application.can_manage_application %} diff --git a/openslides/assignment/models.py b/openslides/assignment/models.py index 553f2a899..519d1395a 100644 --- a/openslides/assignment/models.py +++ b/openslides/assignment/models.py @@ -17,6 +17,7 @@ from participant.models import Profile from projector.models import Slide from projector.api import register_slidemodel +from poll.models import BasePoll, CountInvalid, CountVotesCast, BaseOption class Assignment(models.Model, Slide): @@ -73,7 +74,7 @@ class Assignment(models.Model, Slide): @property def candidates(self): - return Profile.objects.filter(option__poll__assignment=self).order_by('user__first_name').distinct() + return self.profile.get_query_set() def set_elected(self, profile, value=True): if profile in self.candidates: @@ -88,33 +89,9 @@ class Assignment(models.Model, Slide): return False def gen_poll(self): - from poll.models import Poll - poll = Poll() - - candidates = list(self.profile.all()) - for elected in self.elected.all(): - try: - candidates.remove(elected) - except ValueError: - pass - - # Option A: candidates <= available posts -> yes/no/abstention - if len(candidates) <= self.posts - self.elected.count(): - poll.optiondecision = True - else: - poll.optiondecision = False - - # Option B: candidates == 1 -> yes/no/abstention - #if self.profile.count() == 1: - # poll.optiondecision = True - #else: - # poll.optiondecision = False - - poll.assignment = self - poll.description = self.polldescription + poll = AssignmentPoll(assignment=self) poll.save() - for candidate in candidates: - poll.add_option(candidate) + poll.set_options([{'candidate': profile} for profile in self.profile.all()]) return poll def slide(self): @@ -146,3 +123,23 @@ class Assignment(models.Model, Slide): ) register_slidemodel(Assignment) + +class AssignmentOption(BaseOption): + candidate = models.ForeignKey(Profile) + + def __unicode__(self): + return unicode(self.candidate) + + +class AssignmentPoll(BasePoll, CountInvalid, CountVotesCast): + option_class = AssignmentOption + + assignment = models.ForeignKey(Assignment, related_name='poll_set') + + def get_assignment(self): + return self.assignment + + + def append_pollform_fields(self, fields): + CountInvalid.append_pollform_fields(self, fields) + CountVotesCast.append_pollform_fields(self, fields) diff --git a/openslides/assignment/templates/assignment/poll_view.html b/openslides/assignment/templates/assignment/poll_view.html index 2ebb5ba2c..62e4b1ea3 100644 --- a/openslides/assignment/templates/assignment/poll_view.html +++ b/openslides/assignment/templates/assignment/poll_view.html @@ -12,37 +12,32 @@

{% trans "Short description" %}: {{ poll.description }}

-1 := {% trans 'majority' %}, -2 := {% trans 'undocumented' %}
{% csrf_token %} - +
- - {% if poll.optiondecision %} - - - - {% else %} - - {% endif %} + + {% for value in poll.get_vote_values %} + + {% endfor %} - {% for option in options %} - - - {% if poll.optiondecision %} - - - - {% else %} - - {% endif %} + {% for form in forms %} + + + {% for value in form %} + + {% endfor %} {% endfor %} - - - - - - - - + + + + + + + +
{%trans "Option" %}{%trans "Yes" %}{%trans "No" %}{%trans "Abstention" %}{%trans "Votes" %}Option{{ value }}
{{ option }}{{ option.form.yes.errors }}{{ option.form.yes }}{{ option.form.no.errors }}{{ option.form.no }}{{ option.form.undesided.errors }}{{ option.form.undesided }}{{ option.form.yes.errors }}{{ option.form.yes }}
{{ form.option }} + {{ value.errors }} + {{ value }} +
{% trans "Invalid votes" %}{{ form.invalid.errors }}{{ form.invalid }}
{% trans "Votes cast" %}{{ form.votescast.errors }}{{ form.votescast }}
{% trans "Invalid votes" %}{{ pollform.votesinvalid.errors }}{{ pollform.votesinvalid }}
{% trans "Votes cast" %}{{ pollform.votescast.errors }}{{ pollform.votescast }}

diff --git a/openslides/assignment/templates/assignment/view.html b/openslides/assignment/templates/assignment/view.html index 99b165ca2..dfdd8c4fa 100644 --- a/openslides/assignment/templates/assignment/view.html +++ b/openslides/assignment/templates/assignment/view.html @@ -119,46 +119,46 @@


{% trans "Election results" %}

-{% if assignment.poll_set.all.count > 0 %} +{% if polls %} - - {% with ballotnumber=assignment.poll_set.all.count %} - - {% endwith %} + + {% with ballotnumber=polls.count %} + + {% endwith %} - - {% for poll in assignment.poll_set.all %} - {% if poll.published and not perms.assignment.can_manage_assignment or perms.assignment.can_manage_assignment %} - + {% for poll in polls %} + {# if poll.published and not perms.assignment.can_manage_assignment or perms.assignment.can_manage_assignment #} + + {# endif #} + {% endfor %} + {% if assignment.profile.count > 0 and perms.assignment.can_manage_assignment and assignment.status == "vot" %} + {% endif %} - - {% endif %} - - {% endif %} - {% endfor %} - {% if assignment.profile.count > 0 and perms.assignment.can_manage_assignment and assignment.status == "vot" %} - - {% endif %} {% for vote in votes %} @@ -167,20 +167,21 @@ {% 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 %} - {% endfor %} {% if assignment.profile.exists and perms.assignment.can_manage_assignment and assignment.status == "vot" %} @@ -199,9 +202,9 @@ {% endfor %} - {% for p in polls %} - {% if p.published and not perms.assignment.can_manage_assignment or perms.assignment.can_manage_assignment %} - + {% for poll in polls %} + {% if poll.published and not perms.assignment.can_manage_assignment or perms.assignment.can_manage_assignment %} + {% endif %} {% endfor %} {% if assignment.profile.exists and perms.assignment.can_manage_assignment and assignment.status == "vot" %} @@ -210,9 +213,9 @@ - {% for p in polls %} - {% if p.published and not perms.assignment.can_manage_assignment or perms.assignment.can_manage_assignment %} - + {% for poll in polls %} + {% if poll.published and not perms.assignment.can_manage_assignment or perms.assignment.can_manage_assignment %} + {% endif %} {% endfor %} {% if assignment.profile.exists and perms.assignment.can_manage_assignment and assignment.status == "vot" %} diff --git a/openslides/assignment/urls.py b/openslides/assignment/urls.py index 72f6ea12c..c5e07120a 100644 --- a/openslides/assignment/urls.py +++ b/openslides/assignment/urls.py @@ -12,6 +12,8 @@ from django.conf.urls.defaults import * +from assignment.views import ViewPoll #, ActivatePoll + urlpatterns = patterns('assignment.views', url(r'^assignment/$', 'get_overview', \ name='assignment_overview'), @@ -55,8 +57,10 @@ urlpatterns = patterns('assignment.views', url(r'^assignment/(?P\d+)/gen_poll$', 'gen_poll', \ name='assignment_gen_poll'), - url(r'^assignment/poll/(?P\d+)$', 'poll_view', \ - name='assignment_poll_view'), + url(r'^assignment/poll/(?P\d+)$', + ViewPoll.as_view(), + name='assignment_poll_view' + ), url(r'^assignment/poll/(?P\d+)/del$', 'delete_poll', \ name='assignment_poll_delete'), diff --git a/openslides/assignment/views.py b/openslides/assignment/views.py index 14c32e648..3a576da55 100644 --- a/openslides/assignment/views.py +++ b/openslides/assignment/views.py @@ -16,13 +16,14 @@ from django.contrib import messages from django.contrib.auth.decorators import login_required from django.utils.translation import ugettext as _ -from openslides.agenda.models import Item -#from poll.models import Poll, Option -#from poll.forms import OptionResultForm, PollForm -from assignment.models import Assignment -from assignment.forms import AssignmentForm, AssignmentRunForm from utils.utils import template, permission_required, gen_confirm_form, del_confirm_form, ajax_request from utils.pdf import print_assignment, print_assignment_poll + +from poll.views import PollFormView + +from assignment.models import Assignment, AssignmentPoll, AssignmentOption +from assignment.forms import AssignmentForm, AssignmentRunForm + from participant.models import Profile @@ -69,26 +70,26 @@ def view(request, assignment_id=None): votes = [] for candidate in assignment.candidates: tmplist = [[candidate, assignment.is_elected(candidate)], []] + #TODO: only show published polls for poll in assignment.poll_set.all(): - if (poll.published and not request.user.has_perm('assignment.can_manage_assignment')) or request.user.has_perm('assignment.can_manage_assignment'): - if candidate in poll.options_values: - option = Option.objects.filter(poll=poll).filter(user=candidate)[0] - if poll.optiondecision: - tmplist[1].append([option.yes, option.no, option.undesided]) - else: - tmplist[1].append(option.yes) - else: - tmplist[1].append("-") + #if (poll.published and not request.user.has_perm('assignment.can_manage_assignment')) or request.user.has_perm('assignment.can_manage_assignment'): + + # exisitiert der Spieler in der poll + if poll.get_options().filter(candidate=candidate).exists(): + option = AssignmentOption.objects.filter(poll=poll).get(candidate=candidate) + tmplist[1].append(option.get_votes()[0]) + else: + tmplist[1].append("-") votes.append(tmplist) - polls = [] - for poll in assignment.poll_set.filter(assignment=assignment): - polls.append(poll) + polls = assignment.poll_set.all() - return {'assignment': assignment, - 'form': form, - 'votes': votes, - 'polls': polls } + return { + 'assignment': assignment, + 'form': form, + 'votes': votes, + 'polls': polls, + } @permission_required('assignment.can_manage_assignment') @@ -193,56 +194,26 @@ def set_active(request, assignment_id): @permission_required('assignment.can_manage_assignment') def gen_poll(request, assignment_id): - try: - poll = Assignment.objects.get(pk=assignment_id).gen_poll() - messages.success(request, _("New ballot was successfully created.") ) - except Assignment.DoesNotExist: - pass + poll = Assignment.objects.get(pk=assignment_id).gen_poll() + messages.success(request, _("New ballot was successfully created.") ) return redirect(reverse('assignment_poll_view', args=[poll.id])) -@permission_required('assignment.can_manage_assignment') -@template('assignment/poll_view.html') -def poll_view(request, poll_id): - poll = Poll.objects.get(pk=poll_id) - ballotnumber = poll.ballot - options = poll.options.order_by('user__user__first_name') - assignment = poll.assignment - if request.user.has_perm('assignment.can_manage_assignment'): - if request.method == 'POST': - form = PollForm(request.POST, prefix="poll") - if form.is_valid(): - poll.votesinvalid = form.cleaned_data['invalid'] or 0 - poll.votescast = form.cleaned_data['votescast'] or 0 - poll.save() +class ViewPoll(PollFormView): + poll_class = AssignmentPoll + template_name = 'assignment/poll_view.html' + + def get_context_data(self, **kwargs): + context = super(ViewPoll, self).get_context_data(**kwargs) + self.assignment = self.poll.get_assignment() + context['application'] = self.assignment + return context + + def get_success_url(self): + if not 'apply' in self.request.POST: + return reverse('assignment_view', args=[self.poll.assignment.id]) + return '' - success = 0 - for option in options: - option.form = OptionResultForm(request.POST, prefix="o%d" % option.id) - if option.form.is_valid(): - option.voteyes = option.form.cleaned_data['yes'] - option.voteno = option.form.cleaned_data['no'] or 0 - option.voteundesided = option.form.cleaned_data['undesided'] or 0 - option.save() - success = success + 1 - if success == options.count(): - messages.success(request, _("Votes are successfully saved.") ) - if not 'apply' in request.POST: - return redirect(reverse('assignment_view', args=[assignment.id])) - else: - form = PollForm(initial={'invalid': poll.votesinvalid, 'votescast': poll.votescast}, prefix="poll") - for option in options: - option.form = OptionResultForm(initial={ - 'yes': option.voteyes, - 'no': option.voteno, - 'undesided': option.voteundesided, - }, prefix="o%d" % option.id) - return { - 'poll': poll, - 'form': form, - 'options': options, - 'ballotnumber': ballotnumber, - } @permission_required('assignment.can_manage_assignment') def set_published(request, poll_id, published=True): diff --git a/openslides/poll/models.py b/openslides/poll/models.py index b1eff8938..c97b71305 100644 --- a/openslides/poll/models.py +++ b/openslides/poll/models.py @@ -17,8 +17,6 @@ from projector.api import register_slidemodel from projector.models import Slide - - class BaseOption(models.Model): poll = models.ForeignKey('BasePoll') @@ -39,6 +37,9 @@ class Vote(models.Model): weight = models.IntegerField(default=1) value = models.CharField(max_length=255, null=True) + def __unicode__(self): + return print_value(self.weight) + class CountVotesCast(models.Model): votescast = models.IntegerField(null=True, blank=True, verbose_name=_("Votes cast")) @@ -46,6 +47,9 @@ class CountVotesCast(models.Model): def append_pollform_fields(self, fields): fields.append('votescast') + def print_votescast(self): + return print_value(self.votescast) + class Meta: abstract = True @@ -56,6 +60,9 @@ class CountInvalid(models.Model): def append_pollform_fields(self, fields): fields.append('votesinvalid') + def print_votesinvalid(self): + return print_value(self.votesinvalid) + class Meta: abstract = True @@ -130,3 +137,10 @@ class BasePoll(models.Model, Slide): register_slidemodel(BasePoll) +def print_value(value): + if value == -1: + return _('majority') + elif value == -2: + return _('undocumented') + return unicode(value) +
- {% trans "ballot" %} + {% trans "ballot" %}
{% trans "Candidates" %}{% if perms.assignment.can_manage_assignment %}{% endif %} - {{ forloop.counter }}. {% trans 'ballot' %} - {% if perms.assignment.can_manage_assignment %} - - {% if poll.published %} - - {% else %} - + {% trans "Candidates" %}{% if perms.assignment.can_manage_assignment %}{% endif %} + {{ forloop.counter }}. {% trans 'ballot' %} + {% if perms.assignment.can_manage_assignment %} + + {% if poll.published %} + + {% else %} + + {% endif %} + + {% endif %} + + + + {%trans 'New ballot' %} + + + - - - {%trans 'New ballot' %} - - -
{% if v %} + + {% if v %} {% if v|length == 3 %} {% if v.0 %}{{ v.0 }}{% else %}∅{% endif %}
{% if v.1 %}{{ v.1 }}{% else %}∅{% endif %}
@@ -189,7 +190,9 @@ {% if v != "-" %}{% endif %} {{ v }} {% endif %} - {% else %}∅{% endif %} + {% else %} + ∅ + {% endif %}
{%trans 'Invalid votes' %} {{ p.votesinvalidf }} {{ poll.print_votesinvalid }}
{%trans 'Votes cast' %} {{ p.votescastf }} {{ poll.print_votescast }}