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.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.db import models
|
2012-06-11 13:43:48 +02:00
|
|
|
from django.core.urlresolvers import reverse
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2012-04-21 21:38:59 +02:00
|
|
|
from config.models import config
|
|
|
|
|
2011-07-31 10:46:29 +02:00
|
|
|
from participant.models import Profile
|
|
|
|
|
2012-03-12 18:27:13 +01:00
|
|
|
from projector.projector import SlideMixin
|
2012-02-06 22:22:16 +01:00
|
|
|
from projector.api import register_slidemodel
|
2012-03-03 11:16:10 +01:00
|
|
|
from poll.models import BasePoll, CountInvalid, CountVotesCast, BaseOption, PublishPollMixin
|
2012-04-14 14:31:09 +02:00
|
|
|
from utils.translation_ext import ugettext as _
|
2012-02-03 23:12:28 +01:00
|
|
|
|
2012-06-23 11:41:32 +02:00
|
|
|
from agenda.models import Item
|
|
|
|
|
2012-04-21 21:38:59 +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"))
|
|
|
|
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, verbose_name=_("Short description (for ballot paper)"))
|
2012-06-28 20:11:16 +02:00
|
|
|
profile = models.ManyToManyField(Profile, null=True, blank=True) # Rename it in candidate
|
2011-09-03 11:42:44 +02:00
|
|
|
elected = models.ManyToManyField(Profile, null=True, blank=True, related_name='elected_set')
|
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:
|
|
|
|
raise NameError(_('The assignment status is already %s.') % self.status)
|
|
|
|
self.status = status
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
def run(self, profile):
|
|
|
|
"""
|
|
|
|
run for a vote
|
|
|
|
"""
|
|
|
|
if self.is_candidate(profile):
|
|
|
|
raise NameError(_('<b>%s</b> is already a candidate.') % profile)
|
|
|
|
self.profile.add(profile)
|
|
|
|
|
|
|
|
def delrun(self, profile):
|
|
|
|
"""
|
|
|
|
stop running for a vote
|
|
|
|
"""
|
|
|
|
if self.is_candidate(profile):
|
|
|
|
self.profile.remove(profile)
|
2012-06-28 20:12:49 +02:00
|
|
|
self.elected.remove(profile)
|
2011-07-31 10:46:29 +02:00
|
|
|
else:
|
|
|
|
raise NameError(_('%s is no candidate') % profile)
|
|
|
|
|
|
|
|
def is_candidate(self, profile):
|
|
|
|
if profile in self.profile.get_query_set():
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2011-09-03 11:42:44 +02:00
|
|
|
@property
|
|
|
|
def candidates(self):
|
2012-02-19 19:27:00 +01:00
|
|
|
return self.profile.get_query_set()
|
2011-09-03 11:42:44 +02:00
|
|
|
|
2012-07-03 00:05:48 +02:00
|
|
|
|
2011-09-03 11:42:44 +02:00
|
|
|
def set_elected(self, profile, value=True):
|
|
|
|
if profile in self.candidates:
|
|
|
|
if value and not self.is_elected(profile):
|
|
|
|
self.elected.add(profile)
|
|
|
|
elif not value:
|
|
|
|
self.elected.remove(profile)
|
|
|
|
|
|
|
|
def is_elected(self, profile):
|
|
|
|
if profile in self.elected.all():
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
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-04-21 21:38:59 +02:00
|
|
|
candidates = list(self.profile.all())
|
|
|
|
for elected in self.elected.all():
|
|
|
|
try:
|
|
|
|
candidates.remove(elected)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
poll.set_options([{'candidate': profile} for profile in candidates])
|
2011-07-31 10:46:29 +02:00
|
|
|
return poll
|
|
|
|
|
2012-07-03 00:05:48 +02:00
|
|
|
|
2012-04-21 21:38:59 +02:00
|
|
|
@property
|
|
|
|
def vote_results(self):
|
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-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-03 00:05:48 +02:00
|
|
|
try:
|
|
|
|
polloption = poll.get_options().get(candidate=candidate)
|
2012-07-04 11:00:58 +02:00
|
|
|
# candidate is related to this poll
|
2012-07-03 00:05:48 +02:00
|
|
|
votes = {}
|
|
|
|
for vote in polloption.get_votes():
|
|
|
|
votes[vote.value] = vote.get_weight()
|
|
|
|
vote_results_dict[candidate].append(votes)
|
|
|
|
except AssignmentOption.DoesNotExist:
|
2012-07-04 11:00:58 +02:00
|
|
|
# candidate not in related to this poll
|
2012-07-03 00:05:48 +02:00
|
|
|
vote_results_dict[candidate].append(None)
|
|
|
|
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-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-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-04-21 21:38:59 +02:00
|
|
|
data['polls'] = self.poll_set.all()
|
2012-07-04 00:40:25 +02:00
|
|
|
data['vote_results'] = self.vote_results
|
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-04-14 09:28:28 +02:00
|
|
|
('can_see_assignment', _("Can see assignment", fixstr=True)),
|
|
|
|
('can_nominate_other', _("Can nominate another person", fixstr=True)),
|
|
|
|
('can_nominate_self', _("Can nominate themselves", fixstr=True)),
|
|
|
|
('can_manage_assignment', _("Can manage assignment", fixstr=True)),
|
2011-07-31 10:46:29 +02:00
|
|
|
)
|
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
|
|
|
|
|
|
|
class AssignmentOption(BaseOption):
|
|
|
|
candidate = models.ForeignKey(Profile)
|
|
|
|
|
|
|
|
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
|
|
|
|
if self.assignment.candidates.count() <= self.assignment.posts - self.assignment.elected.count():
|
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-06-24 22:21:15 +02:00
|
|
|
return [_('Yes', fixstr=True), _('No', fixstr=True), _('Abstain', fixstr=True)]
|
2012-06-18 09:48:27 +02:00
|
|
|
else:
|
2012-06-24 22:21:15 +02:00
|
|
|
return [_('Votes', fixstr=True)]
|
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
|
|
|
from django.dispatch import receiver
|
|
|
|
from openslides.config.signals import default_config_value
|
|
|
|
|
|
|
|
|
|
|
|
@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)
|