From 4f61c763d73c861976613654d4ac8090d02f8a9b Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Fri, 3 Aug 2012 13:49:05 +0200 Subject: [PATCH] started to rewrite the assignment-code for the new user-api --- openslides/assignment/models.py | 46 +++++++++++++------ .../assignment/templates/assignment/view.html | 15 +++--- openslides/assignment/urls.py | 2 +- openslides/assignment/views.py | 23 ++++++---- 4 files changed, 54 insertions(+), 32 deletions(-) diff --git a/openslides/assignment/models.py b/openslides/assignment/models.py index 5598ad73f..6036c5e5a 100644 --- a/openslides/assignment/models.py +++ b/openslides/assignment/models.py @@ -15,6 +15,8 @@ from django.db import models from django.dispatch import receiver from django.utils.translation import ugettext_lazy as _, ugettext_noop +from openslides.utils.user import UserField + from openslides.config.models import config from openslides.config.signals import default_config_value @@ -29,6 +31,15 @@ from openslides.poll.models import (BasePoll, CountInvalid, CountVotesCast, from openslides.agenda.models import Item +class AssignmentCandidate(models.Model): + assignment = models.ForeignKey("Assignment") + user = UserField(unique=True, db_index=True) + elected = models.BooleanField(default=False) + + def __unicode__(self): + return unicode(self.user) + + class Assignment(models.Model, SlideMixin): prefix = 'assignment' STATUS = ( @@ -44,11 +55,16 @@ class Assignment(models.Model, SlideMixin): verbose_name=_("Number of available posts")) polldescription = models.CharField(max_length=100, null=True, blank=True, verbose_name=_("Comment on the ballot paper")) - profile = models.ManyToManyField(Profile, null=True, blank=True) - elected = models.ManyToManyField(Profile, null=True, blank=True, - related_name='elected_set') status = models.CharField(max_length=3, choices=STATUS, default='sea') + @property + def profile(self): + return AssignmentCandidate.objects.filter(assignment=self).filter(elected=False) + + @property + def elected(self): + return AssignmentCandidate.objects.filter(assignment=self).filter(elected=True) + def set_status(self, status): error = True for a, b in Assignment.STATUS: @@ -71,29 +87,31 @@ class Assignment(models.Model, SlideMixin): raise NameError(_('%s is already a candidate.') % profile) if not user.has_perm("assignment.can_manage_assignment") and self.status != 'sea': raise NameError(_('The candidate list is already closed.')) - self.profile.add(profile) + AssignmentCandidate(assignment=self, user=profile, elected=False).save() - def delrun(self, profile, user=None): + def delrun(self, user): """ stop running for a vote """ - if not user.has_perm("assignment.can_manage_assignment") and self.status != 'sea': - raise NameError(_('The candidate list is already closed.')) - if self.is_candidate(profile): - self.profile.remove(profile) - self.elected.remove(profile) + if self.status != 'sea': + # TODO: Use an OpenSlides Error + raise Exception(_('The candidate list is already closed.')) + if self.is_candidate(user): + AssignmentCandidate.objects.get(user=user).delete() else: - raise NameError(_('%s is no candidate') % profile) + # TODO: Use an OpenSlides Error + raise Exception(_('%s is no candidate') % user) - def is_candidate(self, profile): - if profile in self.profile.get_query_set(): + def is_candidate(self, user): + if AssignmentCandidate.objects.filter(user=user).exists(): return True else: return False @property def candidates(self): - return self.profile.get_query_set() + for candidate in AssignmentCandidate.objects.filter(assignment=self).filter(elected=False): + yield candidate.user def set_elected(self, profile, value=True): diff --git a/openslides/assignment/templates/assignment/view.html b/openslides/assignment/templates/assignment/view.html index 5f3530a0d..d8bae2e12 100644 --- a/openslides/assignment/templates/assignment/view.html +++ b/openslides/assignment/templates/assignment/view.html @@ -35,13 +35,14 @@

{% trans "Candidates" %}

    - {% for profile in assignment.profile.all|dictsort:"user.first_name" %} -
  1. {{ profile }} - {% if perms.assignment.can_manage_assignment %} - {% if assignment.status == "sea" or assignment.status == "vot" %} - - {% endif %} - {% endif %} + {% for user in assignment.candidates %} +
  2. + {{ user }} + {% if perms.assignment.can_manage_assignment %} + {% if assignment.status == "sea" or assignment.status == "vot" %} + + {% endif %} + {% endif %}
  3. {% empty %}
  4. {% trans "No candidates available." %}
  5. diff --git a/openslides/assignment/urls.py b/openslides/assignment/urls.py index 0b6e63798..0c6b1050f 100644 --- a/openslides/assignment/urls.py +++ b/openslides/assignment/urls.py @@ -55,7 +55,7 @@ urlpatterns = patterns('openslides.assignment.views', name='assignment_delrun', ), - url(r'^(?P\d+)/delother/(?P\d+)/$', + url(r'^(?P\d+)/delother/(?P[^/]+)/$', 'delother', name='assignment_delother', ), diff --git a/openslides/assignment/views.py b/openslides/assignment/views.py index 03bd55516..8a0aa7efc 100644 --- a/openslides/assignment/views.py +++ b/openslides/assignment/views.py @@ -30,6 +30,7 @@ from openslides.utils.template import Tab from openslides.utils.utils import (template, permission_required, gen_confirm_form, del_confirm_form, ajax_request) from openslides.utils.views import FormView, DeleteView, PDFView, RedirectView +from openslides.utils.user import get_user from openslides.config.models import config from openslides.participant.models import Profile @@ -182,28 +183,30 @@ def run(request, assignment_id): def delrun(request, assignment_id): assignment = Assignment.objects.get(pk=assignment_id) try: - assignment.delrun(request.user.profile, request.user) - messages.success(request, _("You have withdrawn your candidature successfully.") ) - except NameError, e: + assignment.delrun(request.user.profile) + except Exception, e: messages.error(request, e) + else: + messages.success(request, _("You have withdrawn your candidature successfully.") ) return redirect(reverse('assignment_view', args=[assignment_id])) @permission_required('assignment.can_manage_assignment') -def delother(request, assignment_id, profile_id): +def delother(request, assignment_id, user_id): assignment = Assignment.objects.get(pk=assignment_id) - profile = Profile.objects.get(pk=profile_id) + user = get_user(user_id) if request.method == 'POST': try: - assignment.delrun(profile, request.user) - messages.success(request, _("Candidate %s was withdrawn successfully.") % (profile)) - except NameError, e: + assignment.delrun(user) + except Exception, e: messages.error(request, e) + else: + messages.success(request, _("Candidate %s was withdrawn successfully.") % (user)) else: gen_confirm_form(request, - _("Do you really want to withdraw %s from the election?") \ - % profile, reverse('assignment_delother', args=[assignment_id, profile_id])) + _("Do you really want to withdraw %s from the election?") \ + % user, reverse('assignment_delother', args=[assignment_id, user_id])) return redirect(reverse('assignment_view', args=[assignment_id]))