Merge pull request #14 from ostcar/no_recandidate

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-11 00:52:50 -07:00
commit 8732b67bd8
2 changed files with 33 additions and 11 deletions

View File

@ -33,6 +33,7 @@ class AssignmentCandidate(models.Model):
assignment = models.ForeignKey("Assignment")
person = PersonField(db_index=True)
elected = models.BooleanField(default=False)
blocked = models.BooleanField(default=False)
def __unicode__(self):
return unicode(self.person)
@ -72,6 +73,8 @@ class Assignment(models.Model, SlideMixin):
def run(self, candidate, person=None):
"""
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.
# Use other Exceptions
@ -79,23 +82,39 @@ class Assignment(models.Model, SlideMixin):
raise NameError(_('<b>%s</b> is already a candidate.') % candidate)
if not person.has_perm("assignment.can_manage_assignment") and self.status != 'sea':
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
"""
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:
# TODO: Use an OpenSlides Error
raise Exception(_('%s is no candidate') % candidate)
def is_candidate(self, person):
if self.assignment_candidats.filter(person=person).exists():
return True
else:
return False
"""
return True, if person is a candidate.
"""
return self.assignment_candidats.filter(person=person) \
.exclude(blocked=True).exists()
@property
def assignment_candidats(self):
@ -110,7 +129,7 @@ class Assignment(models.Model, SlideMixin):
return self.get_participants(only_elected=True)
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:
# TODO: Use right Exception

View File

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