2011-07-31 10:46:29 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
openslides.assignment.models
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Models for the assignment app.
|
|
|
|
|
2012-04-25 22:29:19 +02:00
|
|
|
:copyright: 2011, 2012 by OpenSlides team, see AUTHORS.
|
2011-07-31 10:46:29 +02:00
|
|
|
:license: GNU GPL, see LICENSE for more details.
|
|
|
|
"""
|
|
|
|
|
2012-06-11 13:43:48 +02:00
|
|
|
from django.core.urlresolvers import reverse
|
2012-07-10 11:27:06 +02:00
|
|
|
from django.db import models
|
|
|
|
from django.dispatch import receiver
|
|
|
|
from django.utils.translation import ugettext_lazy as _, ugettext_noop
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2012-08-07 22:43:57 +02:00
|
|
|
from openslides.utils.person import PersonField
|
2012-08-03 13:49:05 +02:00
|
|
|
|
2012-07-10 11:27:06 +02:00
|
|
|
from openslides.config.models import config
|
|
|
|
from openslides.config.signals import default_config_value
|
2012-04-21 21:38:59 +02:00
|
|
|
|
2012-07-10 11:27:06 +02:00
|
|
|
from openslides.projector.api import register_slidemodel
|
|
|
|
from openslides.projector.projector import SlideMixin
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2012-07-10 11:27:06 +02:00
|
|
|
from openslides.poll.models import (BasePoll, CountInvalid, CountVotesCast,
|
2012-07-13 11:16:06 +02:00
|
|
|
BaseOption, PublishPollMixin, BaseVote)
|
2012-02-03 23:12:28 +01:00
|
|
|
|
2012-07-10 14:00:51 +02:00
|
|
|
from openslides.agenda.models import Item
|
2012-06-23 11:41:32 +02:00
|
|
|
|
2012-04-21 21:38:59 +02:00
|
|
|
|
2012-08-03 13:49:05 +02:00
|
|
|
class AssignmentCandidate(models.Model):
|
|
|
|
assignment = models.ForeignKey("Assignment")
|
2012-08-07 22:43:57 +02:00
|
|
|
person = PersonField(db_index=True)
|
2012-08-03 13:49:05 +02:00
|
|
|
elected = models.BooleanField(default=False)
|
2012-09-06 15:57:42 +02:00
|
|
|
blocked = models.BooleanField(default=False)
|
2012-08-03 13:49:05 +02:00
|
|
|
|
|
|
|
def __unicode__(self):
|
2012-08-07 22:43:57 +02:00
|
|
|
return unicode(self.person)
|
2012-08-03 13:49:05 +02:00
|
|
|
|
2012-09-13 14:59:14 +02:00
|
|
|
class Meta:
|
|
|
|
unique_together = ("assignment", "person")
|
|
|
|
|
2012-08-03 13:49:05 +02:00
|
|
|
|
2012-03-03 09:11:56 +01:00
|
|
|
class Assignment(models.Model, SlideMixin):
|
2012-02-03 23:12:28 +01:00
|
|
|
prefix = 'assignment'
|
2011-07-31 10:46:29 +02:00
|
|
|
STATUS = (
|
|
|
|
('sea', _('Searching for candidates')),
|
|
|
|
('vot', _('Voting')),
|
|
|
|
('fin', _('Finished')),
|
|
|
|
)
|
|
|
|
|
2012-06-24 22:38:42 +02:00
|
|
|
name = models.CharField(max_length=100, verbose_name=_("Name"))
|
2012-07-10 11:27:06 +02:00
|
|
|
description = models.TextField(null=True, blank=True,
|
|
|
|
verbose_name=_("Description"))
|
|
|
|
posts = models.PositiveSmallIntegerField(
|
|
|
|
verbose_name=_("Number of available posts"))
|
|
|
|
polldescription = models.CharField(max_length=100, null=True, blank=True,
|
2012-07-23 09:30:34 +02:00
|
|
|
verbose_name=_("Comment on the ballot paper"))
|
2012-06-23 13:17:50 +02:00
|
|
|
status = models.CharField(max_length=3, choices=STATUS, default='sea')
|
2011-07-31 10:46:29 +02:00
|
|
|
|
|
|
|
def set_status(self, status):
|
|
|
|
error = True
|
|
|
|
for a, b in Assignment.STATUS:
|
|
|
|
if status == a:
|
|
|
|
error = False
|
|
|
|
break
|
|
|
|
if error:
|
|
|
|
raise NameError(_('%s is not a valid status.') % status)
|
|
|
|
if self.status == status:
|
2012-07-10 11:27:06 +02:00
|
|
|
raise NameError(_('The assignment status is already %s.')
|
|
|
|
% self.status)
|
2011-07-31 10:46:29 +02:00
|
|
|
self.status = status
|
|
|
|
self.save()
|
|
|
|
|
2012-08-07 22:43:57 +02:00
|
|
|
def run(self, candidate, person=None):
|
2011-07-31 10:46:29 +02:00
|
|
|
"""
|
|
|
|
run for a vote
|
2012-09-06 15:57:42 +02:00
|
|
|
candidate: The user who will be a candidate
|
|
|
|
person: The user who chooses the candidate
|
2011-07-31 10:46:29 +02:00
|
|
|
"""
|
2012-08-04 15:02:02 +02:00
|
|
|
# TODO: don't make any permission checks here.
|
|
|
|
# Use other Exceptions
|
|
|
|
if self.is_candidate(candidate):
|
|
|
|
raise NameError(_('<b>%s</b> is already a candidate.') % candidate)
|
2012-08-07 22:43:57 +02:00
|
|
|
if not person.has_perm("assignment.can_manage_assignment") and self.status != 'sea':
|
2012-07-16 22:21:23 +02:00
|
|
|
raise NameError(_('The candidate list is already closed.'))
|
2012-11-21 20:12:08 +01:00
|
|
|
candidation = self.assignment_candidates.filter(person=candidate)
|
2012-09-13 14:59:14 +02:00
|
|
|
if candidation and candidate != person and \
|
|
|
|
not person.has_perm("assignment.can_manage_assignment"):
|
2012-09-06 15:57:42 +02:00
|
|
|
# if the candidation is blocked and anotherone tries to run the
|
|
|
|
# candidate
|
|
|
|
raise NameError(
|
2012-09-11 20:50:53 +02:00
|
|
|
_('%s does not want to be a candidate.') % candidate)
|
2012-09-13 14:59:14 +02:00
|
|
|
elif candidation:
|
2012-09-06 15:57:42 +02:00
|
|
|
candidation[0].blocked = False
|
|
|
|
candidation[0].save()
|
|
|
|
else:
|
|
|
|
AssignmentCandidate(assignment=self, person=candidate).save()
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2012-09-06 15:57:42 +02:00
|
|
|
def delrun(self, candidate, blocked=True):
|
2011-07-31 10:46:29 +02:00
|
|
|
"""
|
|
|
|
stop running for a vote
|
|
|
|
"""
|
2012-09-13 14:59:14 +02:00
|
|
|
try:
|
2012-11-21 20:12:08 +01:00
|
|
|
candidation = self.assignment_candidates.get(person=candidate)
|
2012-09-13 14:59:14 +02:00
|
|
|
except AssignmentCandidate.DoesNotExist:
|
|
|
|
raise Exception(_('%s is no candidate') % candidate)
|
|
|
|
|
|
|
|
if not candidation.blocked:
|
2012-09-06 15:57:42 +02:00
|
|
|
if blocked:
|
|
|
|
candidation.blocked = True
|
|
|
|
candidation.save()
|
|
|
|
else:
|
|
|
|
candidation.delete()
|
2011-07-31 10:46:29 +02:00
|
|
|
else:
|
2012-09-13 14:59:14 +02:00
|
|
|
candidation.delete()
|
|
|
|
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2012-08-07 22:43:57 +02:00
|
|
|
def is_candidate(self, person):
|
2012-09-06 15:57:42 +02:00
|
|
|
"""
|
|
|
|
return True, if person is a candidate.
|
|
|
|
"""
|
2012-09-19 14:16:17 +02:00
|
|
|
try:
|
2012-11-21 20:12:08 +01:00
|
|
|
return self.assignment_candidates.filter(person=person) \
|
2012-09-06 15:57:42 +02:00
|
|
|
.exclude(blocked=True).exists()
|
2012-09-19 14:16:17 +02:00
|
|
|
except AttributeError:
|
|
|
|
return False
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2012-09-13 14:59:14 +02:00
|
|
|
def is_blocked(self, person):
|
|
|
|
"""
|
|
|
|
return True, if the person is blockt for candidation.
|
|
|
|
"""
|
2012-11-21 20:12:08 +01:00
|
|
|
return self.assignment_candidates.filter(person=person) \
|
2012-09-13 14:59:14 +02:00
|
|
|
.filter(blocked=True).exists()
|
|
|
|
|
2012-08-03 18:56:00 +02:00
|
|
|
@property
|
2012-11-21 20:12:08 +01:00
|
|
|
def assignment_candidates(self):
|
2012-08-03 18:56:00 +02:00
|
|
|
return AssignmentCandidate.objects.filter(assignment=self)
|
|
|
|
|
2011-09-03 11:42:44 +02:00
|
|
|
@property
|
|
|
|
def candidates(self):
|
2012-08-03 18:56:00 +02:00
|
|
|
return self.get_participants(only_candidate=True)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def elected(self):
|
|
|
|
return self.get_participants(only_elected=True)
|
2011-09-03 11:42:44 +02:00
|
|
|
|
2012-08-03 18:56:00 +02:00
|
|
|
def get_participants(self, only_elected=False, only_candidate=False):
|
2012-11-21 20:12:08 +01:00
|
|
|
candidates = self.assignment_candidates.exclude(blocked=True)
|
2012-07-03 00:05:48 +02:00
|
|
|
|
2012-09-12 10:17:51 +02:00
|
|
|
assert not (only_elected and only_candidate)
|
2012-08-03 18:56:00 +02:00
|
|
|
|
|
|
|
if only_elected:
|
|
|
|
candidates = candidates.filter(elected=True)
|
|
|
|
|
|
|
|
if only_candidate:
|
|
|
|
candidates = candidates.filter(elected=False)
|
|
|
|
|
2012-09-12 10:17:51 +02:00
|
|
|
participants = []
|
2012-08-07 22:43:57 +02:00
|
|
|
for candidate in candidates.all():
|
2012-09-12 10:17:51 +02:00
|
|
|
participants.append(candidate.person)
|
2012-11-22 16:19:09 +01:00
|
|
|
participants.sort(key=lambda person: person.sort_name)
|
2012-09-12 10:17:51 +02:00
|
|
|
return participants
|
|
|
|
#return candidates.values_list('person', flat=True)
|
2011-09-03 11:42:44 +02:00
|
|
|
|
2012-08-03 18:56:00 +02:00
|
|
|
|
2012-08-07 22:43:57 +02:00
|
|
|
def set_elected(self, person, value=True):
|
2012-11-21 20:12:08 +01:00
|
|
|
candidate = self.assignment_candidates.get(person=person)
|
2012-08-03 18:56:00 +02:00
|
|
|
candidate.elected = value
|
|
|
|
candidate.save()
|
|
|
|
|
2012-08-07 22:43:57 +02:00
|
|
|
def is_elected(self, person):
|
|
|
|
return person in self.elected
|
2011-09-03 11:42:44 +02:00
|
|
|
|
2011-07-31 10:46:29 +02:00
|
|
|
def gen_poll(self):
|
2012-02-19 19:27:00 +01:00
|
|
|
poll = AssignmentPoll(assignment=self)
|
2011-07-31 10:46:29 +02:00
|
|
|
poll.save()
|
2012-08-07 22:43:57 +02:00
|
|
|
poll.set_options([{'candidate': person} for person in self.candidates])
|
2011-07-31 10:46:29 +02:00
|
|
|
return poll
|
|
|
|
|
2012-07-10 00:47:00 +02:00
|
|
|
def vote_results(self, only_published):
|
2012-07-03 00:05:48 +02:00
|
|
|
"""
|
|
|
|
returns a table represented as a list with all candidates from all
|
2012-07-04 11:00:58 +02:00
|
|
|
related polls and their vote results.
|
2012-07-03 00:05:48 +02:00
|
|
|
"""
|
|
|
|
vote_results_dict = {}
|
2012-07-04 11:00:58 +02:00
|
|
|
# All polls related to this assigment
|
2012-07-02 20:32:13 +02:00
|
|
|
polls = self.poll_set.all()
|
2012-07-10 00:47:00 +02:00
|
|
|
if only_published:
|
|
|
|
polls = polls.filter(published=True)
|
2012-07-04 11:00:58 +02:00
|
|
|
# All PollOption-Objects related to this assignment
|
2012-07-03 00:05:48 +02:00
|
|
|
options = []
|
2012-07-02 20:32:13 +02:00
|
|
|
for poll in polls:
|
|
|
|
options += poll.get_options()
|
2012-07-03 00:05:48 +02:00
|
|
|
|
|
|
|
for option in options:
|
|
|
|
candidate = option.candidate
|
|
|
|
if candidate in vote_results_dict:
|
|
|
|
continue
|
|
|
|
vote_results_dict[candidate] = []
|
2012-07-02 20:32:13 +02:00
|
|
|
for poll in polls:
|
2012-07-10 00:47:00 +02:00
|
|
|
votes = {}
|
2012-07-03 00:05:48 +02:00
|
|
|
try:
|
2012-07-04 16:05:31 +02:00
|
|
|
# candidate related to this poll
|
|
|
|
poll_option = poll.get_options().get(candidate=candidate)
|
|
|
|
for vote in poll_option.get_votes():
|
2012-07-13 11:16:06 +02:00
|
|
|
votes[vote.value] = vote.print_weight()
|
2012-07-03 00:05:48 +02:00
|
|
|
except AssignmentOption.DoesNotExist:
|
2012-07-04 11:00:58 +02:00
|
|
|
# candidate not in related to this poll
|
2012-07-10 00:47:00 +02:00
|
|
|
votes = None
|
2012-07-04 16:05:31 +02:00
|
|
|
vote_results_dict[candidate].append(votes)
|
2012-07-03 00:05:48 +02:00
|
|
|
return vote_results_dict
|
|
|
|
|
2012-04-21 21:38:59 +02:00
|
|
|
|
2012-06-23 10:27:58 +02:00
|
|
|
def get_agenda_title(self):
|
|
|
|
return self.name
|
2012-04-21 21:38:59 +02:00
|
|
|
|
2012-06-23 11:41:32 +02:00
|
|
|
def delete(self):
|
2012-10-24 11:04:23 +02:00
|
|
|
# Remove any Agenda-Item, which is related to this assignment.
|
2012-07-04 11:00:58 +02:00
|
|
|
for item in Item.objects.filter(related_sid=self.sid):
|
2012-06-23 11:41:32 +02:00
|
|
|
item.delete()
|
|
|
|
super(Assignment, self).delete()
|
|
|
|
|
2012-02-06 22:08:08 +01:00
|
|
|
def slide(self):
|
2012-02-03 23:12:28 +01:00
|
|
|
"""
|
2012-02-06 22:08:08 +01:00
|
|
|
return the slide dict
|
2012-02-03 23:12:28 +01:00
|
|
|
"""
|
2012-10-29 10:16:12 +01:00
|
|
|
polls = self.poll_set
|
2012-02-06 22:08:08 +01:00
|
|
|
data = super(Assignment, self).slide()
|
2012-02-03 23:12:28 +01:00
|
|
|
data['assignment'] = self
|
|
|
|
data['title'] = self.name
|
2012-10-29 10:16:12 +01:00
|
|
|
data['some_polls_available'] = polls.exists()
|
|
|
|
data['polls'] = polls.filter(published=True)
|
2012-07-10 00:47:00 +02:00
|
|
|
data['vote_results'] = self.vote_results(only_published=True)
|
2012-07-10 11:27:06 +02:00
|
|
|
data['assignment_publish_winner_results_only'] = \
|
|
|
|
config['assignment_publish_winner_results_only']
|
2012-02-06 22:08:08 +01:00
|
|
|
data['template'] = 'projector/Assignment.html'
|
2012-02-03 23:12:28 +01:00
|
|
|
return data
|
|
|
|
|
2011-07-31 10:46:29 +02:00
|
|
|
def get_absolute_url(self, link='view'):
|
|
|
|
if link == 'view':
|
2012-06-28 01:14:51 +02:00
|
|
|
return reverse('assignment_view', args=[str(self.id)])
|
2012-06-11 13:43:48 +02:00
|
|
|
if link == 'edit':
|
2012-06-28 01:14:51 +02:00
|
|
|
return reverse('assignment_edit', args=[str(self.id)])
|
2011-07-31 10:46:29 +02:00
|
|
|
if link == 'delete':
|
2012-06-28 01:14:51 +02:00
|
|
|
return reverse('assignment_delete', args=[str(self.id)])
|
2011-07-31 10:46:29 +02:00
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
permissions = (
|
2012-07-10 11:27:06 +02:00
|
|
|
('can_see_assignment', ugettext_noop("Can see assignment")),
|
|
|
|
('can_nominate_other',
|
|
|
|
ugettext_noop("Can nominate another person")),
|
|
|
|
('can_nominate_self', ugettext_noop("Can nominate themselves")),
|
|
|
|
('can_manage_assignment', ugettext_noop("Can manage assignment")),
|
2011-07-31 10:46:29 +02:00
|
|
|
)
|
2012-09-14 00:21:59 +02:00
|
|
|
ordering = ('name',)
|
2012-02-03 23:12:28 +01:00
|
|
|
|
2012-04-14 11:18:47 +02:00
|
|
|
register_slidemodel(Assignment)
|
2012-03-03 11:16:10 +01:00
|
|
|
|
2012-02-19 19:27:00 +01:00
|
|
|
|
2012-07-13 11:16:06 +02:00
|
|
|
class AssignmentVote(BaseVote):
|
|
|
|
option = models.ForeignKey('AssignmentOption')
|
|
|
|
|
|
|
|
|
2012-02-19 19:27:00 +01:00
|
|
|
class AssignmentOption(BaseOption):
|
2012-07-13 11:16:06 +02:00
|
|
|
poll = models.ForeignKey('AssignmentPoll')
|
2012-08-07 22:43:57 +02:00
|
|
|
candidate = PersonField()
|
2012-07-13 11:16:06 +02:00
|
|
|
vote_class = AssignmentVote
|
2012-02-19 19:27:00 +01:00
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return unicode(self.candidate)
|
|
|
|
|
|
|
|
|
2012-03-03 11:16:10 +01:00
|
|
|
class AssignmentPoll(BasePoll, CountInvalid, CountVotesCast, PublishPollMixin):
|
2012-02-19 19:27:00 +01:00
|
|
|
option_class = AssignmentOption
|
|
|
|
|
|
|
|
assignment = models.ForeignKey(Assignment, related_name='poll_set')
|
2012-06-28 20:11:16 +02:00
|
|
|
yesnoabstain = models.NullBooleanField()
|
2012-02-19 19:27:00 +01:00
|
|
|
|
|
|
|
def get_assignment(self):
|
|
|
|
return self.assignment
|
|
|
|
|
2012-06-18 09:48:27 +02:00
|
|
|
def get_vote_values(self):
|
2012-06-28 20:11:16 +02:00
|
|
|
if self.yesnoabstain is None:
|
2012-06-18 09:48:27 +02:00
|
|
|
if config['assignment_poll_vote_values'] == 'votes':
|
2012-06-28 20:11:16 +02:00
|
|
|
self.yesnoabstain = False
|
2012-06-18 09:48:27 +02:00
|
|
|
elif config['assignment_poll_vote_values'] == 'yesnoabstain':
|
2012-06-28 20:11:16 +02:00
|
|
|
self.yesnoabstain = True
|
2012-06-18 09:48:27 +02:00
|
|
|
else:
|
|
|
|
# candidates <= available posts -> yes/no/abstain
|
2012-11-21 15:03:29 +01:00
|
|
|
if len(self.assignment.candidates) <= (self.assignment.posts - len(self.assignment.elected)):
|
2012-06-28 20:11:16 +02:00
|
|
|
self.yesnoabstain = True
|
2012-06-18 09:48:27 +02:00
|
|
|
else:
|
2012-06-28 20:11:16 +02:00
|
|
|
self.yesnoabstain = False
|
2012-06-18 09:48:27 +02:00
|
|
|
self.save()
|
2012-06-28 20:11:16 +02:00
|
|
|
if self.yesnoabstain:
|
2012-07-10 12:11:07 +02:00
|
|
|
return [ugettext_noop('Yes'), ugettext_noop('No'),
|
|
|
|
ugettext_noop('Abstain')]
|
2012-06-18 09:48:27 +02:00
|
|
|
else:
|
2012-07-10 12:11:07 +02:00
|
|
|
return [ugettext_noop('Votes')]
|
2012-06-18 09:48:27 +02:00
|
|
|
|
2012-02-19 19:27:00 +01:00
|
|
|
def append_pollform_fields(self, fields):
|
|
|
|
CountInvalid.append_pollform_fields(self, fields)
|
|
|
|
CountVotesCast.append_pollform_fields(self, fields)
|
2012-03-03 11:16:10 +01:00
|
|
|
|
2012-04-18 19:02:41 +02:00
|
|
|
def get_ballot(self):
|
2012-05-14 21:34:46 +02:00
|
|
|
return self.assignment.poll_set.filter(id__lte=self.id).count()
|
2012-04-14 17:09:15 +02:00
|
|
|
|
2012-04-14 14:24:13 +02:00
|
|
|
@models.permalink
|
|
|
|
def get_absolute_url(self, link='view'):
|
|
|
|
if link == 'view':
|
|
|
|
return ('assignment_poll_view', [str(self.id)])
|
|
|
|
if link == 'delete':
|
|
|
|
return ('assignment_poll_delete', [str(self.id)])
|
|
|
|
|
2012-06-23 12:43:18 +02:00
|
|
|
def __unicode__(self):
|
|
|
|
return _("Ballot %d") % self.get_ballot()
|
|
|
|
|
2012-03-18 17:11:58 +01:00
|
|
|
|
2012-04-14 12:52:56 +02:00
|
|
|
@receiver(default_config_value, dispatch_uid="assignment_default_config")
|
|
|
|
def default_config(sender, key, **kwargs):
|
|
|
|
return {
|
2012-04-22 16:55:16 +02:00
|
|
|
'assignment_publish_winner_results_only': False,
|
2012-06-29 00:19:06 +02:00
|
|
|
'assignment_pdf_ballot_papers_selection': 'CUSTOM_NUMBER',
|
|
|
|
'assignment_pdf_ballot_papers_number': '8',
|
2012-04-14 12:52:56 +02:00
|
|
|
'assignment_pdf_title': _('Elections'),
|
2012-04-22 16:55:16 +02:00
|
|
|
'assignment_pdf_preamble': '',
|
2012-06-18 09:48:27 +02:00
|
|
|
'assignment_poll_vote_values': 'auto',
|
2012-04-14 12:52:56 +02:00
|
|
|
}.get(key)
|