started to rewrite the assignment-code for the new user-api

This commit is contained in:
Oskar Hahn 2012-08-03 13:49:05 +02:00
parent 93020f9ead
commit 4f61c763d7
4 changed files with 54 additions and 32 deletions

View File

@ -15,6 +15,8 @@ from django.db import models
from django.dispatch import receiver from django.dispatch import receiver
from django.utils.translation import ugettext_lazy as _, ugettext_noop 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.models import config
from openslides.config.signals import default_config_value 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 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): class Assignment(models.Model, SlideMixin):
prefix = 'assignment' prefix = 'assignment'
STATUS = ( STATUS = (
@ -44,11 +55,16 @@ class Assignment(models.Model, SlideMixin):
verbose_name=_("Number of available posts")) verbose_name=_("Number of available posts"))
polldescription = models.CharField(max_length=100, null=True, blank=True, polldescription = models.CharField(max_length=100, null=True, blank=True,
verbose_name=_("Comment on the ballot paper")) 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') 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): def set_status(self, status):
error = True error = True
for a, b in Assignment.STATUS: for a, b in Assignment.STATUS:
@ -71,29 +87,31 @@ class Assignment(models.Model, SlideMixin):
raise NameError(_('<b>%s</b> is already a candidate.') % profile) raise NameError(_('<b>%s</b> is already a candidate.') % profile)
if not user.has_perm("assignment.can_manage_assignment") and self.status != 'sea': if not user.has_perm("assignment.can_manage_assignment") and self.status != 'sea':
raise NameError(_('The candidate list is already closed.')) 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 stop running for a vote
""" """
if not user.has_perm("assignment.can_manage_assignment") and self.status != 'sea': if self.status != 'sea':
raise NameError(_('The candidate list is already closed.')) # TODO: Use an OpenSlides Error
if self.is_candidate(profile): raise Exception(_('The candidate list is already closed.'))
self.profile.remove(profile) if self.is_candidate(user):
self.elected.remove(profile) AssignmentCandidate.objects.get(user=user).delete()
else: 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): def is_candidate(self, user):
if profile in self.profile.get_query_set(): if AssignmentCandidate.objects.filter(user=user).exists():
return True return True
else: else:
return False return False
@property @property
def candidates(self): 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): def set_elected(self, profile, value=True):

View File

@ -35,11 +35,12 @@
<h3>{% trans "Candidates" %}</h3> <h3>{% trans "Candidates" %}</h3>
<ol> <ol>
{% for profile in assignment.profile.all|dictsort:"user.first_name" %} {% for user in assignment.candidates %}
<li>{{ profile }} <li>
{{ user }}
{% if perms.assignment.can_manage_assignment %} {% if perms.assignment.can_manage_assignment %}
{% if assignment.status == "sea" or assignment.status == "vot" %} {% if assignment.status == "sea" or assignment.status == "vot" %}
<a href="{% url assignment_delother assignment.id profile.id %}"><img src="{% static 'images/icons/delete.png' %}" title="{% trans 'Remove candidate' %}"></a> <a href="{% url assignment_delother assignment.id user.uid %}"><img src="{% static 'images/icons/delete.png' %}" title="{% trans 'Remove candidate' %}"></a>
{% endif %} {% endif %}
{% endif %} {% endif %}
</li> </li>

View File

@ -55,7 +55,7 @@ urlpatterns = patterns('openslides.assignment.views',
name='assignment_delrun', name='assignment_delrun',
), ),
url(r'^(?P<assignment_id>\d+)/delother/(?P<profile_id>\d+)/$', url(r'^(?P<assignment_id>\d+)/delother/(?P<user_id>[^/]+)/$',
'delother', 'delother',
name='assignment_delother', name='assignment_delother',
), ),

View File

@ -30,6 +30,7 @@ from openslides.utils.template import Tab
from openslides.utils.utils import (template, permission_required, from openslides.utils.utils import (template, permission_required,
gen_confirm_form, del_confirm_form, ajax_request) gen_confirm_form, del_confirm_form, ajax_request)
from openslides.utils.views import FormView, DeleteView, PDFView, RedirectView from openslides.utils.views import FormView, DeleteView, PDFView, RedirectView
from openslides.utils.user import get_user
from openslides.config.models import config from openslides.config.models import config
from openslides.participant.models import Profile from openslides.participant.models import Profile
@ -182,28 +183,30 @@ def run(request, assignment_id):
def delrun(request, assignment_id): def delrun(request, assignment_id):
assignment = Assignment.objects.get(pk=assignment_id) assignment = Assignment.objects.get(pk=assignment_id)
try: try:
assignment.delrun(request.user.profile, request.user) assignment.delrun(request.user.profile)
messages.success(request, _("You have withdrawn your candidature successfully.") ) except Exception, e:
except NameError, e:
messages.error(request, e) messages.error(request, e)
else:
messages.success(request, _("You have withdrawn your candidature successfully.") )
return redirect(reverse('assignment_view', args=[assignment_id])) return redirect(reverse('assignment_view', args=[assignment_id]))
@permission_required('assignment.can_manage_assignment') @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) assignment = Assignment.objects.get(pk=assignment_id)
profile = Profile.objects.get(pk=profile_id) user = get_user(user_id)
if request.method == 'POST': if request.method == 'POST':
try: try:
assignment.delrun(profile, request.user) assignment.delrun(user)
messages.success(request, _("Candidate <b>%s</b> was withdrawn successfully.") % (profile)) except Exception, e:
except NameError, e:
messages.error(request, e) messages.error(request, e)
else:
messages.success(request, _("Candidate <b>%s</b> was withdrawn successfully.") % (user))
else: else:
gen_confirm_form(request, gen_confirm_form(request,
_("Do you really want to withdraw <b>%s</b> from the election?") \ _("Do you really want to withdraw <b>%s</b> from the election?") \
% profile, reverse('assignment_delother', args=[assignment_id, profile_id])) % user, reverse('assignment_delother', args=[assignment_id, user_id]))
return redirect(reverse('assignment_view', args=[assignment_id])) return redirect(reverse('assignment_view', args=[assignment_id]))