block a user to be a candidate from a assignment, if he delete his

candidation by him self
This commit is contained in:
Oskar Hahn 2012-09-06 15:57:42 +02:00
parent 423f04d0be
commit 75eb596547
2 changed files with 33 additions and 11 deletions

View File

@ -33,6 +33,7 @@ class AssignmentCandidate(models.Model):
assignment = models.ForeignKey("Assignment") assignment = models.ForeignKey("Assignment")
person = PersonField(db_index=True) person = PersonField(db_index=True)
elected = models.BooleanField(default=False) elected = models.BooleanField(default=False)
blocked = models.BooleanField(default=False)
def __unicode__(self): def __unicode__(self):
return unicode(self.person) return unicode(self.person)
@ -72,6 +73,8 @@ class Assignment(models.Model, SlideMixin):
def run(self, candidate, person=None): def run(self, candidate, person=None):
""" """
run for a vote run for a vote
candidate: The user who will be a candidate
person: The user who chooses the candidate
""" """
# TODO: don't make any permission checks here. # TODO: don't make any permission checks here.
# Use other Exceptions # Use other Exceptions
@ -79,23 +82,39 @@ class Assignment(models.Model, SlideMixin):
raise NameError(_('<b>%s</b> is already a candidate.') % candidate) raise NameError(_('<b>%s</b> is already a candidate.') % candidate)
if not person.has_perm("assignment.can_manage_assignment") and self.status != 'sea': if not person.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.'))
AssignmentCandidate(assignment=self, person=candidate, elected=False).save() candidation = self.assignment_candidats.filter(person=candidate)
if candidation and candidate != person:
# if the candidation is blocked and anotherone tries to run the
# candidate
raise NameError(
_('The %s does not want to be a candidate.') % candidate)
elif candidation and candidate == person:
candidation[0].blocked = False
candidation[0].save()
else:
AssignmentCandidate(assignment=self, person=candidate).save()
def delrun(self, candidate): def delrun(self, candidate, blocked=True):
""" """
stop running for a vote stop running for a vote
""" """
if self.is_candidate(candidate): if self.is_candidate(candidate):
self.assignment_candidats.get(person=candidate).delete() candidation = self.assignment_candidats.get(person=candidate)
if blocked:
candidation.blocked = True
candidation.save()
else:
candidation.delete()
else: else:
# TODO: Use an OpenSlides Error # TODO: Use an OpenSlides Error
raise Exception(_('%s is no candidate') % candidate) raise Exception(_('%s is no candidate') % candidate)
def is_candidate(self, person): def is_candidate(self, person):
if self.assignment_candidats.filter(person=person).exists(): """
return True return True, if person is a candidate.
else: """
return False return self.assignment_candidats.filter(person=person) \
.exclude(blocked=True).exists()
@property @property
def assignment_candidats(self): def assignment_candidats(self):
@ -110,7 +129,7 @@ class Assignment(models.Model, SlideMixin):
return self.get_participants(only_elected=True) return self.get_participants(only_elected=True)
def get_participants(self, only_elected=False, only_candidate=False): def get_participants(self, only_elected=False, only_candidate=False):
candidates = self.assignment_candidats candidates = self.assignment_candidats.exclude(blocked=True)
if only_elected and only_candidate: if only_elected and only_candidate:
# TODO: Use right Exception # TODO: Use right Exception

View File

@ -81,9 +81,12 @@ def view(request, assignment_id=None):
user = form.cleaned_data['candidate'] user = form.cleaned_data['candidate']
try: try:
assignment.run(user, request.user) assignment.run(user, request.user)
messages.success(request, _("Candidate <b>%s</b> was nominated successfully.") % (user))
except NameError, e: except NameError, e:
messages.error(request, e) messages.error(request, e)
else:
messages.success(request, _(
"Candidate <b>%s</b> was nominated successfully.")
% user)
else: else:
if request.user.has_perm('assignment.can_nominate_other'): if request.user.has_perm('assignment.can_nominate_other'):
form = AssignmentRunForm() form = AssignmentRunForm()
@ -184,7 +187,7 @@ def delrun(request, assignment_id):
assignment = Assignment.objects.get(pk=assignment_id) assignment = Assignment.objects.get(pk=assignment_id)
try: try:
if assignment.status == 'sea' or user.has_perm("assignment.can_manage_assignment"): if assignment.status == 'sea' or user.has_perm("assignment.can_manage_assignment"):
assignment.delrun(request.user) assignment.delrun(request.user, blocked=True)
else: else:
messages.error(request, _('The candidate list is already closed.')) messages.error(request, _('The candidate list is already closed.'))
except Exception, e: except Exception, e:
@ -201,7 +204,7 @@ def delother(request, assignment_id, user_id):
if request.method == 'POST': if request.method == 'POST':
try: try:
assignment.delrun(person) assignment.delrun(person, blocked=False)
except Exception, e: except Exception, e:
messages.error(request, e) messages.error(request, e)
else: else: