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.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(_('<b>%s</b> 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):

View File

@ -35,11 +35,12 @@
<h3>{% trans "Candidates" %}</h3>
<ol>
{% for profile in assignment.profile.all|dictsort:"user.first_name" %}
<li>{{ profile }}
{% for user in assignment.candidates %}
<li>
{{ user }}
{% if perms.assignment.can_manage_assignment %}
{% 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 %}
</li>

View File

@ -55,7 +55,7 @@ urlpatterns = patterns('openslides.assignment.views',
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',
name='assignment_delother',
),

View File

@ -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 <b>%s</b> was withdrawn successfully.") % (profile))
except NameError, e:
assignment.delrun(user)
except Exception, e:
messages.error(request, e)
else:
messages.success(request, _("Candidate <b>%s</b> was withdrawn successfully.") % (user))
else:
gen_confirm_form(request,
_("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]))