2016-01-09 09:58:22 +01:00
|
|
|
from collections import OrderedDict
|
2018-08-22 17:34:16 +02:00
|
|
|
from decimal import Decimal
|
2017-08-24 12:26:55 +02:00
|
|
|
from typing import Any, Dict, List, Optional # noqa
|
2016-01-09 09:58:22 +01:00
|
|
|
|
2015-09-07 17:09:29 +02:00
|
|
|
from django.conf import settings
|
2016-09-18 16:00:31 +02:00
|
|
|
from django.contrib.contenttypes.fields import GenericRelation
|
2018-08-22 17:34:16 +02:00
|
|
|
from django.core.validators import MinValueValidator
|
2012-07-10 11:27:06 +02:00
|
|
|
from django.db import models
|
2016-01-25 21:22:22 +01:00
|
|
|
from django.utils.translation import ugettext as _
|
2016-06-17 09:47:05 +02:00
|
|
|
from django.utils.translation import ugettext_noop
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2014-01-12 11:03:43 +01:00
|
|
|
from openslides.agenda.models import Item, Speaker
|
2015-06-29 12:08:15 +02:00
|
|
|
from openslides.core.config import config
|
2017-07-28 15:42:58 +02:00
|
|
|
from openslides.core.models import Projector, Tag
|
2015-06-16 10:37:23 +02:00
|
|
|
from openslides.poll.models import (
|
|
|
|
BaseOption,
|
|
|
|
BasePoll,
|
|
|
|
BaseVote,
|
|
|
|
CollectDefaultVotesMixin,
|
|
|
|
PublishPollMixin,
|
|
|
|
)
|
2016-10-01 01:30:55 +02:00
|
|
|
from openslides.utils.autoupdate import inform_changed_data
|
2014-01-12 11:03:43 +01:00
|
|
|
from openslides.utils.exceptions import OpenSlidesError
|
2018-08-22 17:34:16 +02:00
|
|
|
from openslides.utils.models import RESTModelMixin
|
2012-06-23 11:41:32 +02:00
|
|
|
|
2016-02-11 22:58:32 +01:00
|
|
|
from .access_permissions import AssignmentAccessPermissions
|
|
|
|
|
2012-04-21 21:38:59 +02:00
|
|
|
|
2015-01-25 15:10:34 +01:00
|
|
|
class AssignmentRelatedUser(RESTModelMixin, models.Model):
|
2013-10-03 21:49:51 +02:00
|
|
|
"""
|
2015-01-25 15:10:34 +01:00
|
|
|
Many to Many table between an assignment and user.
|
2013-10-03 21:49:51 +02:00
|
|
|
"""
|
2016-12-06 12:21:29 +01:00
|
|
|
|
2015-01-25 15:10:34 +01:00
|
|
|
assignment = models.ForeignKey(
|
|
|
|
'Assignment',
|
2016-01-09 09:58:22 +01:00
|
|
|
on_delete=models.CASCADE,
|
2015-01-25 15:10:34 +01:00
|
|
|
related_name='assignment_related_users')
|
2016-12-06 12:21:29 +01:00
|
|
|
"""
|
|
|
|
ForeinKey to the assignment.
|
|
|
|
"""
|
|
|
|
|
2016-01-09 09:58:22 +01:00
|
|
|
user = models.ForeignKey(
|
|
|
|
settings.AUTH_USER_MODEL,
|
|
|
|
on_delete=models.CASCADE)
|
2016-12-06 12:21:29 +01:00
|
|
|
"""
|
|
|
|
ForeinKey to the user who is related to the assignment.
|
|
|
|
"""
|
|
|
|
|
2016-01-09 16:26:00 +01:00
|
|
|
elected = models.BooleanField(default=False)
|
2016-12-06 12:21:29 +01:00
|
|
|
"""
|
|
|
|
Saves the election state of each user
|
|
|
|
"""
|
|
|
|
|
|
|
|
weight = models.IntegerField(default=0)
|
|
|
|
"""
|
|
|
|
The sort order of the candidates.
|
|
|
|
"""
|
2012-08-03 13:49:05 +02:00
|
|
|
|
2013-10-03 21:49:51 +02:00
|
|
|
class Meta:
|
2015-12-10 00:20:59 +01:00
|
|
|
default_permissions = ()
|
2015-01-25 15:10:34 +01:00
|
|
|
unique_together = ('assignment', 'user')
|
2013-10-03 21:49:51 +02:00
|
|
|
|
2014-08-16 09:25:18 +02:00
|
|
|
def __str__(self):
|
2015-01-25 15:10:34 +01:00
|
|
|
return "%s <-> %s" % (self.assignment, self.user)
|
2012-08-03 13:49:05 +02:00
|
|
|
|
2015-01-17 14:25:05 +01:00
|
|
|
def get_root_rest_element(self):
|
|
|
|
"""
|
2015-01-24 16:35:50 +01:00
|
|
|
Returns the assignment to this instance which is the root REST element.
|
2015-01-17 14:25:05 +01:00
|
|
|
"""
|
|
|
|
return self.assignment
|
|
|
|
|
2012-08-03 13:49:05 +02:00
|
|
|
|
2016-09-18 16:00:31 +02:00
|
|
|
class AssignmentManager(models.Manager):
|
2016-09-30 20:42:58 +02:00
|
|
|
"""
|
|
|
|
Customized model manager to support our get_full_queryset method.
|
|
|
|
"""
|
2016-09-18 16:00:31 +02:00
|
|
|
def get_full_queryset(self):
|
2016-09-30 20:42:58 +02:00
|
|
|
"""
|
|
|
|
Returns the normal queryset with all assignments. In the background
|
|
|
|
all related users (candidates), the related agenda item and all
|
|
|
|
polls are prefetched from the database.
|
|
|
|
"""
|
2016-09-18 16:00:31 +02:00
|
|
|
return self.get_queryset().prefetch_related(
|
|
|
|
'related_users',
|
|
|
|
'agenda_items',
|
2016-12-17 09:30:20 +01:00
|
|
|
'polls',
|
|
|
|
'tags')
|
2016-09-18 16:00:31 +02:00
|
|
|
|
|
|
|
|
2015-06-29 13:31:07 +02:00
|
|
|
class Assignment(RESTModelMixin, models.Model):
|
2016-02-11 22:58:32 +01:00
|
|
|
"""
|
|
|
|
Model for assignments.
|
|
|
|
"""
|
|
|
|
access_permissions = AssignmentAccessPermissions()
|
2013-08-04 12:59:11 +02:00
|
|
|
|
2016-09-18 16:00:31 +02:00
|
|
|
objects = AssignmentManager()
|
|
|
|
|
2015-03-29 15:49:37 +02:00
|
|
|
PHASE_SEARCH = 0
|
|
|
|
PHASE_VOTING = 1
|
|
|
|
PHASE_FINISHED = 2
|
2015-01-25 15:10:34 +01:00
|
|
|
|
|
|
|
PHASES = (
|
2016-06-17 09:47:05 +02:00
|
|
|
(PHASE_SEARCH, 'Searching for candidates'),
|
|
|
|
(PHASE_VOTING, 'Voting'),
|
|
|
|
(PHASE_FINISHED, 'Finished'),
|
2011-07-31 10:46:29 +02:00
|
|
|
)
|
|
|
|
|
2015-01-25 15:10:34 +01:00
|
|
|
title = models.CharField(
|
2016-01-09 13:32:56 +01:00
|
|
|
max_length=100)
|
2015-01-25 15:10:34 +01:00
|
|
|
"""
|
|
|
|
Title of the assignment.
|
|
|
|
"""
|
|
|
|
|
|
|
|
description = models.TextField(
|
2016-01-09 13:32:56 +01:00
|
|
|
blank=True)
|
2015-01-25 15:10:34 +01:00
|
|
|
"""
|
|
|
|
Text to describe the assignment.
|
|
|
|
"""
|
|
|
|
|
2016-01-09 13:32:56 +01:00
|
|
|
open_posts = models.PositiveSmallIntegerField()
|
2015-01-25 15:10:34 +01:00
|
|
|
"""
|
|
|
|
The number of members to be elected.
|
|
|
|
"""
|
|
|
|
|
2014-01-11 17:07:47 +01:00
|
|
|
poll_description_default = models.CharField(
|
2015-01-25 15:10:34 +01:00
|
|
|
max_length=79,
|
2016-01-09 13:32:56 +01:00
|
|
|
blank=True)
|
2015-01-25 15:10:34 +01:00
|
|
|
"""
|
|
|
|
Default text for the poll description.
|
|
|
|
"""
|
|
|
|
|
|
|
|
phase = models.IntegerField(
|
|
|
|
choices=PHASES,
|
|
|
|
default=PHASE_SEARCH)
|
|
|
|
"""
|
|
|
|
Phase in which the assignment is.
|
|
|
|
"""
|
|
|
|
|
|
|
|
related_users = models.ManyToManyField(
|
2015-09-07 17:09:29 +02:00
|
|
|
settings.AUTH_USER_MODEL,
|
2015-01-25 15:10:34 +01:00
|
|
|
through='AssignmentRelatedUser')
|
|
|
|
"""
|
2016-01-09 16:26:00 +01:00
|
|
|
Users that are candidates or elected.
|
2015-01-25 15:10:34 +01:00
|
|
|
|
2016-01-09 16:26:00 +01:00
|
|
|
See AssignmentRelatedUser for more information.
|
2015-01-25 15:10:34 +01:00
|
|
|
"""
|
|
|
|
|
2014-12-26 13:45:13 +01:00
|
|
|
tags = models.ManyToManyField(Tag, blank=True)
|
2015-01-25 15:10:34 +01:00
|
|
|
"""
|
|
|
|
Tags for the assignment.
|
|
|
|
"""
|
|
|
|
|
2016-09-30 20:42:58 +02:00
|
|
|
# In theory there could be one then more agenda_item. But we support only
|
|
|
|
# one. See the property agenda_item.
|
2016-09-18 16:00:31 +02:00
|
|
|
agenda_items = GenericRelation(Item, related_name='assignments')
|
|
|
|
|
2013-08-04 12:59:11 +02:00
|
|
|
class Meta:
|
2015-12-10 00:20:59 +01:00
|
|
|
default_permissions = ()
|
2013-08-04 12:59:11 +02:00
|
|
|
permissions = (
|
2016-01-27 13:41:19 +01:00
|
|
|
('can_see', 'Can see elections'),
|
|
|
|
('can_nominate_other', 'Can nominate another participant'),
|
|
|
|
('can_nominate_self', 'Can nominate oneself'),
|
|
|
|
('can_manage', 'Can manage elections'),
|
2013-08-04 12:59:11 +02:00
|
|
|
)
|
2015-01-25 15:10:34 +01:00
|
|
|
ordering = ('title', )
|
2013-11-16 20:20:53 +01:00
|
|
|
verbose_name = ugettext_noop('Election')
|
2013-08-04 12:59:11 +02:00
|
|
|
|
2014-08-16 09:25:18 +02:00
|
|
|
def __str__(self):
|
2015-01-25 15:10:34 +01:00
|
|
|
return self.title
|
2013-08-04 12:59:11 +02:00
|
|
|
|
2017-07-28 15:42:58 +02:00
|
|
|
def delete(self, skip_autoupdate=False, *args, **kwargs):
|
2015-01-25 15:10:34 +01:00
|
|
|
"""
|
2017-07-28 15:42:58 +02:00
|
|
|
Customized method to delete an assignment. Ensures that a respective
|
|
|
|
assignment projector element is disabled.
|
2015-01-25 15:10:34 +01:00
|
|
|
"""
|
2017-07-28 15:42:58 +02:00
|
|
|
Projector.remove_any(
|
|
|
|
skip_autoupdate=skip_autoupdate,
|
|
|
|
name='assignments/assignment',
|
|
|
|
id=self.pk)
|
2017-08-24 12:26:55 +02:00
|
|
|
return super().delete(skip_autoupdate=skip_autoupdate, *args, **kwargs) # type: ignore # TODO fix typing
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2015-01-25 15:10:34 +01:00
|
|
|
@property
|
|
|
|
def candidates(self):
|
2011-07-31 10:46:29 +02:00
|
|
|
"""
|
2015-01-25 15:10:34 +01:00
|
|
|
Queryset that represents the candidates for the assignment.
|
2011-07-31 10:46:29 +02:00
|
|
|
"""
|
2015-01-25 15:10:34 +01:00
|
|
|
return self.related_users.filter(
|
2016-01-09 16:26:00 +01:00
|
|
|
assignmentrelateduser__elected=False)
|
2012-09-13 14:59:14 +02:00
|
|
|
|
2015-01-25 15:10:34 +01:00
|
|
|
@property
|
|
|
|
def elected(self):
|
|
|
|
"""
|
|
|
|
Queryset that represents all elected users for the assignment.
|
|
|
|
"""
|
|
|
|
return self.related_users.filter(
|
2016-01-09 16:26:00 +01:00
|
|
|
assignmentrelateduser__elected=True)
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2015-01-25 15:10:34 +01:00
|
|
|
def is_candidate(self, user):
|
2012-09-13 14:59:14 +02:00
|
|
|
"""
|
2015-01-25 15:10:34 +01:00
|
|
|
Returns True if user is a candidate.
|
|
|
|
|
|
|
|
Costs one database query.
|
2012-09-13 14:59:14 +02:00
|
|
|
"""
|
2015-01-25 15:10:34 +01:00
|
|
|
return self.candidates.filter(pk=user.pk).exists()
|
2012-09-13 14:59:14 +02:00
|
|
|
|
2015-01-25 15:10:34 +01:00
|
|
|
def is_elected(self, user):
|
|
|
|
"""
|
|
|
|
Returns True if the user is elected for this assignment.
|
2012-08-03 18:56:00 +02:00
|
|
|
|
2015-01-25 15:10:34 +01:00
|
|
|
Costs one database query.
|
|
|
|
"""
|
|
|
|
return self.elected.filter(pk=user.pk).exists()
|
2012-08-03 18:56:00 +02:00
|
|
|
|
2015-01-25 15:10:34 +01:00
|
|
|
def set_candidate(self, user):
|
|
|
|
"""
|
|
|
|
Adds the user as candidate.
|
|
|
|
"""
|
2016-12-06 12:21:29 +01:00
|
|
|
weight = self.assignment_related_users.aggregate(
|
|
|
|
models.Max('weight'))['weight__max'] or 0
|
|
|
|
defaults = {
|
|
|
|
'elected': False,
|
|
|
|
'weight': weight + 1}
|
2015-01-25 15:10:34 +01:00
|
|
|
related_user, __ = self.assignment_related_users.update_or_create(
|
|
|
|
user=user,
|
2016-12-06 12:21:29 +01:00
|
|
|
defaults=defaults)
|
2012-07-03 00:05:48 +02:00
|
|
|
|
2015-01-25 15:10:34 +01:00
|
|
|
def set_elected(self, user):
|
|
|
|
"""
|
|
|
|
Makes user an elected user for this assignment.
|
|
|
|
"""
|
|
|
|
related_user, __ = self.assignment_related_users.update_or_create(
|
|
|
|
user=user,
|
2016-01-09 16:26:00 +01:00
|
|
|
defaults={'elected': True})
|
2012-08-03 18:56:00 +02:00
|
|
|
|
2015-01-25 15:10:34 +01:00
|
|
|
def delete_related_user(self, user):
|
|
|
|
"""
|
|
|
|
Delete the connection from the assignment to the user.
|
|
|
|
"""
|
|
|
|
self.assignment_related_users.filter(user=user).delete()
|
2016-10-01 01:30:55 +02:00
|
|
|
inform_changed_data(self)
|
2012-08-03 18:56:00 +02:00
|
|
|
|
2015-01-25 15:10:34 +01:00
|
|
|
def set_phase(self, phase):
|
|
|
|
"""
|
|
|
|
Sets the phase attribute of the assignment.
|
2011-09-03 11:42:44 +02:00
|
|
|
|
2015-01-25 15:10:34 +01:00
|
|
|
Raises a ValueError if the phase is not valide.
|
|
|
|
"""
|
|
|
|
if phase not in dict(self.PHASES):
|
|
|
|
raise ValueError("Invalid phase %s" % phase)
|
2012-08-03 18:56:00 +02:00
|
|
|
|
2015-01-25 15:10:34 +01:00
|
|
|
self.phase = phase
|
2011-09-03 11:42:44 +02:00
|
|
|
|
2015-01-25 15:10:34 +01:00
|
|
|
def create_poll(self):
|
2014-01-12 11:03:43 +01:00
|
|
|
"""
|
2015-06-14 23:26:06 +02:00
|
|
|
Creates a new poll for the assignment and adds all candidates to all
|
2014-01-12 11:03:43 +01:00
|
|
|
lists of speakers of related agenda items.
|
|
|
|
"""
|
2015-01-25 15:10:34 +01:00
|
|
|
candidates = self.candidates.all()
|
|
|
|
|
|
|
|
# Find out the method of the election
|
2015-06-16 18:12:59 +02:00
|
|
|
if config['assignments_poll_vote_values'] == 'votes':
|
2016-08-26 13:46:57 +02:00
|
|
|
pollmethod = 'votes'
|
2015-06-16 18:12:59 +02:00
|
|
|
elif config['assignments_poll_vote_values'] == 'yesnoabstain':
|
2016-08-26 13:46:57 +02:00
|
|
|
pollmethod = 'yna'
|
2016-06-08 18:14:11 +02:00
|
|
|
elif config['assignments_poll_vote_values'] == 'yesno':
|
2016-08-26 13:46:57 +02:00
|
|
|
pollmethod = 'yn'
|
2014-05-12 17:56:07 +02:00
|
|
|
else:
|
2015-06-16 18:12:59 +02:00
|
|
|
# config['assignments_poll_vote_values'] == 'auto'
|
2014-05-12 17:56:07 +02:00
|
|
|
# candidates <= available posts -> yes/no/abstain
|
2015-01-25 15:10:34 +01:00
|
|
|
if len(candidates) <= (self.open_posts - self.elected.count()):
|
2016-08-26 13:46:57 +02:00
|
|
|
pollmethod = 'yna'
|
2014-05-12 17:56:07 +02:00
|
|
|
else:
|
2016-08-26 13:46:57 +02:00
|
|
|
pollmethod = 'votes'
|
2014-05-12 17:56:07 +02:00
|
|
|
|
2015-01-25 15:10:34 +01:00
|
|
|
# Create the poll with the candidates.
|
|
|
|
poll = self.polls.create(
|
2014-05-12 17:56:07 +02:00
|
|
|
description=self.poll_description_default,
|
2016-08-26 13:46:57 +02:00
|
|
|
pollmethod=pollmethod)
|
2016-12-06 12:21:29 +01:00
|
|
|
options = []
|
2017-03-04 21:38:25 +01:00
|
|
|
related_users = AssignmentRelatedUser.objects.filter(assignment__id=self.id).exclude(elected=True)
|
2016-12-06 12:21:29 +01:00
|
|
|
for related_user in related_users:
|
|
|
|
options.append({
|
|
|
|
'candidate': related_user.user,
|
|
|
|
'weight': related_user.weight})
|
2017-03-24 08:14:08 +01:00
|
|
|
poll.set_options(options, skip_autoupdate=True)
|
|
|
|
inform_changed_data(self)
|
2014-05-12 17:56:07 +02:00
|
|
|
|
2015-11-25 21:31:08 +01:00
|
|
|
# Add all candidates to list of speakers of related agenda item
|
2015-01-25 15:10:34 +01:00
|
|
|
# TODO: Try to do this in a bulk create
|
2018-05-09 08:47:05 +02:00
|
|
|
if config['assignments_add_candidates_to_list_of_speakers']:
|
|
|
|
for candidate in self.candidates:
|
|
|
|
try:
|
|
|
|
Speaker.objects.add(candidate, self.agenda_item, skip_autoupdate=True)
|
|
|
|
except OpenSlidesError:
|
|
|
|
# The Speaker is already on the list. Do nothing.
|
|
|
|
# TODO: Find a smart way not to catch the error concerning AnonymousUser.
|
|
|
|
pass
|
|
|
|
inform_changed_data(self.agenda_item)
|
2014-05-12 17:56:07 +02:00
|
|
|
|
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
|
|
|
"""
|
2015-01-25 15:10:34 +01: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
|
|
|
"""
|
2017-08-24 12:26:55 +02:00
|
|
|
vote_results_dict = OrderedDict() # type: Dict[Any, List[AssignmentVote]]
|
2015-01-25 15:10:34 +01:00
|
|
|
|
|
|
|
polls = self.polls.all()
|
2012-07-10 00:47:00 +02:00
|
|
|
if only_published:
|
|
|
|
polls = polls.filter(published=True)
|
2015-01-25 15:10:34 +01:00
|
|
|
|
2012-07-04 11:00:58 +02:00
|
|
|
# All PollOption-Objects related to this assignment
|
2017-08-24 12:26:55 +02:00
|
|
|
options = [] # type: List[AssignmentOption]
|
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:
|
2017-08-24 12:26:55 +02:00
|
|
|
votes = {} # type: Any
|
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
|
|
|
|
|
2017-08-03 15:49:14 +02:00
|
|
|
"""
|
|
|
|
Container for runtime information for agenda app (on create or update of this instance).
|
|
|
|
"""
|
2018-01-20 09:54:46 +01:00
|
|
|
agenda_item_update_information = {} # type: Dict[str, Any]
|
2017-08-03 15:49:14 +02:00
|
|
|
|
2012-06-23 10:27:58 +02:00
|
|
|
def get_agenda_title(self):
|
2015-01-25 15:10:34 +01:00
|
|
|
return str(self)
|
2012-04-21 21:38:59 +02:00
|
|
|
|
2016-01-25 21:22:22 +01:00
|
|
|
def get_agenda_list_view_title(self):
|
|
|
|
"""
|
|
|
|
Return a title string for the agenda list view.
|
|
|
|
|
|
|
|
Contains agenda item number, title and assignment verbose name.
|
|
|
|
Note: It has to be the same return value like in JavaScript.
|
|
|
|
"""
|
|
|
|
return '%s (%s)' % (self.title, _(self._meta.verbose_name))
|
|
|
|
|
2015-10-24 19:02:43 +02:00
|
|
|
@property
|
|
|
|
def agenda_item(self):
|
|
|
|
"""
|
|
|
|
Returns the related agenda item.
|
|
|
|
"""
|
2016-09-30 20:42:58 +02:00
|
|
|
# We support only one agenda item so just return the first element of
|
|
|
|
# the queryset.
|
2016-09-18 16:00:31 +02:00
|
|
|
return self.agenda_items.all()[0]
|
2015-10-24 19:02:43 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def agenda_item_id(self):
|
|
|
|
"""
|
|
|
|
Returns the id of the agenda item object related to this object.
|
|
|
|
"""
|
|
|
|
return self.agenda_item.pk
|
2013-09-07 00:18:13 +02:00
|
|
|
|
2012-02-19 19:27:00 +01:00
|
|
|
|
2015-01-17 14:25:05 +01:00
|
|
|
class AssignmentVote(RESTModelMixin, BaseVote):
|
2016-01-09 09:58:22 +01:00
|
|
|
option = models.ForeignKey(
|
|
|
|
'AssignmentOption',
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name='votes')
|
2012-07-13 11:16:06 +02:00
|
|
|
|
2015-12-10 00:20:59 +01:00
|
|
|
class Meta:
|
|
|
|
default_permissions = ()
|
|
|
|
|
2015-01-17 14:25:05 +01:00
|
|
|
def get_root_rest_element(self):
|
|
|
|
"""
|
2015-01-24 16:35:50 +01:00
|
|
|
Returns the assignment to this instance which is the root REST element.
|
2015-01-17 14:25:05 +01:00
|
|
|
"""
|
|
|
|
return self.option.poll.assignment
|
|
|
|
|
2012-07-13 11:16:06 +02:00
|
|
|
|
2015-01-17 14:25:05 +01:00
|
|
|
class AssignmentOption(RESTModelMixin, BaseOption):
|
2016-01-09 09:58:22 +01:00
|
|
|
poll = models.ForeignKey(
|
|
|
|
'AssignmentPoll',
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name='options')
|
|
|
|
candidate = models.ForeignKey(
|
|
|
|
settings.AUTH_USER_MODEL,
|
|
|
|
on_delete=models.CASCADE)
|
2016-12-06 12:21:29 +01:00
|
|
|
weight = models.IntegerField(default=0)
|
|
|
|
|
2012-07-13 11:16:06 +02:00
|
|
|
vote_class = AssignmentVote
|
2012-02-19 19:27:00 +01:00
|
|
|
|
2015-12-10 00:20:59 +01:00
|
|
|
class Meta:
|
|
|
|
default_permissions = ()
|
|
|
|
|
2014-08-16 09:25:18 +02:00
|
|
|
def __str__(self):
|
|
|
|
return str(self.candidate)
|
2012-02-19 19:27:00 +01:00
|
|
|
|
2015-01-17 14:25:05 +01:00
|
|
|
def get_root_rest_element(self):
|
|
|
|
"""
|
2015-01-24 16:35:50 +01:00
|
|
|
Returns the assignment to this instance which is the root REST element.
|
2015-01-17 14:25:05 +01:00
|
|
|
"""
|
|
|
|
return self.poll.assignment
|
2012-02-19 19:27:00 +01:00
|
|
|
|
2015-01-17 14:25:05 +01:00
|
|
|
|
2017-08-23 20:51:06 +02:00
|
|
|
# TODO: remove the type-ignoring in the next line, after this is solved:
|
|
|
|
# https://github.com/python/mypy/issues/3855
|
|
|
|
class AssignmentPoll(RESTModelMixin, CollectDefaultVotesMixin, # type: ignore
|
2015-06-16 10:37:23 +02:00
|
|
|
PublishPollMixin, BasePoll):
|
2012-02-19 19:27:00 +01:00
|
|
|
option_class = AssignmentOption
|
2015-01-25 15:10:34 +01:00
|
|
|
|
2016-01-09 09:58:22 +01:00
|
|
|
assignment = models.ForeignKey(
|
|
|
|
Assignment,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name='polls')
|
2016-08-26 13:46:57 +02:00
|
|
|
pollmethod = models.CharField(
|
|
|
|
max_length=5,
|
|
|
|
default='yna')
|
2014-01-11 17:07:47 +01:00
|
|
|
description = models.CharField(
|
2015-01-25 15:10:34 +01:00
|
|
|
max_length=79,
|
2016-01-09 13:32:56 +01:00
|
|
|
blank=True)
|
2012-02-19 19:27:00 +01:00
|
|
|
|
2018-08-22 17:34:16 +02:00
|
|
|
votesabstain = models.DecimalField(null=True, blank=True, validators=[
|
|
|
|
MinValueValidator(Decimal('-2'))], max_digits=15, decimal_places=6)
|
2018-07-03 15:47:31 +02:00
|
|
|
""" General abstain votes, used for pollmethod 'votes' """
|
2018-08-22 17:34:16 +02:00
|
|
|
votesno = models.DecimalField(null=True, blank=True, validators=[
|
|
|
|
MinValueValidator(Decimal('-2'))], max_digits=15, decimal_places=6)
|
2018-07-03 15:47:31 +02:00
|
|
|
""" General no votes, used for pollmethod 'votes' """
|
|
|
|
|
2015-12-10 00:20:59 +01:00
|
|
|
class Meta:
|
|
|
|
default_permissions = ()
|
|
|
|
|
2017-07-28 15:42:58 +02:00
|
|
|
def delete(self, skip_autoupdate=False, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Customized method to delete an assignment poll. Ensures that a respective
|
|
|
|
assignment projector element (with poll, so called poll slide) is disabled.
|
|
|
|
"""
|
|
|
|
Projector.remove_any(
|
|
|
|
skip_autoupdate=skip_autoupdate,
|
|
|
|
name='assignments/assignment',
|
|
|
|
id=self.assignment.pk,
|
|
|
|
poll=self.pk)
|
2017-08-24 12:26:55 +02:00
|
|
|
return super().delete(skip_autoupdate=skip_autoupdate, *args, **kwargs) # type: ignore # TODO: fix typing
|
2017-07-28 15:42:58 +02:00
|
|
|
|
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):
|
2016-08-26 13:46:57 +02:00
|
|
|
if self.pollmethod == 'yna':
|
2015-12-07 12:40:30 +01:00
|
|
|
return ['Yes', 'No', 'Abstain']
|
2016-08-26 13:46:57 +02:00
|
|
|
elif self.pollmethod == 'yn':
|
2016-06-08 18:14:11 +02:00
|
|
|
return ['Yes', 'No']
|
2012-06-18 09:48:27 +02:00
|
|
|
else:
|
2015-12-07 12:40:30 +01:00
|
|
|
return ['Votes']
|
2012-06-18 09:48:27 +02:00
|
|
|
|
2012-04-18 19:02:41 +02:00
|
|
|
def get_ballot(self):
|
2015-01-25 15:10:34 +01:00
|
|
|
return self.assignment.polls.filter(id__lte=self.pk).count()
|
2014-01-11 17:07:47 +01:00
|
|
|
|
2014-04-10 20:18:22 +02:00
|
|
|
def get_percent_base_choice(self):
|
2015-06-16 18:12:59 +02:00
|
|
|
return config['assignments_poll_100_percent_base']
|
2014-04-10 20:18:22 +02:00
|
|
|
|
2015-01-17 14:25:05 +01:00
|
|
|
def get_root_rest_element(self):
|
|
|
|
"""
|
2015-01-24 16:35:50 +01:00
|
|
|
Returns the assignment to this instance which is the root REST element.
|
2015-01-17 14:25:05 +01:00
|
|
|
"""
|
|
|
|
return self.assignment
|