2019-10-29 09:44:19 +01:00
|
|
|
from decimal import Decimal
|
2016-01-09 09:58:22 +01:00
|
|
|
|
2015-09-07 17:09:29 +02:00
|
|
|
from django.conf import settings
|
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
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2019-04-23 16:57:35 +02:00
|
|
|
from openslides.agenda.mixins import AgendaItemWithListOfSpeakersMixin
|
|
|
|
from openslides.agenda.models import Speaker
|
2015-06-29 12:08:15 +02:00
|
|
|
from openslides.core.config import config
|
2018-12-23 11:05:38 +01:00
|
|
|
from openslides.core.models import Tag
|
2019-04-26 13:25:45 +02:00
|
|
|
from openslides.mediafiles.models import Mediafile
|
2019-10-18 14:18:49 +02:00
|
|
|
from openslides.poll.models import BaseOption, BasePoll, BaseVote
|
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
|
2019-11-04 14:56:01 +01:00
|
|
|
from openslides.utils.manager import BaseManager
|
2018-08-22 17:34:16 +02:00
|
|
|
from openslides.utils.models import RESTModelMixin
|
2012-06-23 11:41:32 +02:00
|
|
|
|
2019-06-28 07:24:28 +02:00
|
|
|
from ..utils.models import CASCADE_AND_AUTOUPDATE, SET_NULL_AND_AUTOUPDATE
|
2019-10-18 14:18:49 +02:00
|
|
|
from .access_permissions import (
|
|
|
|
AssignmentAccessPermissions,
|
2019-11-12 18:30:26 +01:00
|
|
|
AssignmentOptionAccessPermissions,
|
2019-10-18 14:18:49 +02:00
|
|
|
AssignmentPollAccessPermissions,
|
2019-10-29 09:00:11 +01:00
|
|
|
AssignmentVoteAccessPermissions,
|
2019-10-18 14:18:49 +02:00
|
|
|
)
|
2016-02-11 22:58:32 +01:00
|
|
|
|
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(
|
2019-01-06 16:22:33 +01:00
|
|
|
"Assignment", on_delete=models.CASCADE, related_name="assignment_related_users"
|
|
|
|
)
|
2016-12-06 12:21:29 +01:00
|
|
|
"""
|
|
|
|
ForeinKey to the assignment.
|
|
|
|
"""
|
|
|
|
|
2019-06-28 07:24:28 +02:00
|
|
|
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=CASCADE_AND_AUTOUPDATE)
|
2016-12-06 12:21:29 +01:00
|
|
|
"""
|
|
|
|
ForeinKey to the user who is related to the assignment.
|
|
|
|
"""
|
|
|
|
|
|
|
|
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 = ()
|
2019-01-06 16:22:33 +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):
|
2019-01-12 23:01:42 +01:00
|
|
|
return f"{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
|
|
|
|
2019-11-04 14:56:01 +01:00
|
|
|
class AssignmentManager(BaseManager):
|
2016-09-30 20:42:58 +02:00
|
|
|
"""
|
2019-11-04 14:56:01 +01:00
|
|
|
Customized model manager to support our get_prefetched_queryset method.
|
2016-09-30 20:42:58 +02:00
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2019-11-04 14:56:01 +01:00
|
|
|
def get_prefetched_queryset(self, *args, **kwargs):
|
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.
|
|
|
|
"""
|
2019-11-04 14:56:01 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
super()
|
|
|
|
.get_prefetched_queryset(*args, **kwargs)
|
|
|
|
.prefetch_related(
|
|
|
|
"assignment_related_users",
|
|
|
|
"agenda_items",
|
|
|
|
"lists_of_speakers",
|
|
|
|
"tags",
|
|
|
|
"attachments",
|
2020-01-29 16:13:05 +01:00
|
|
|
"polls",
|
2020-02-13 18:24:51 +01:00
|
|
|
"polls__options",
|
2019-11-04 14:56:01 +01:00
|
|
|
)
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
2016-09-18 16:00:31 +02:00
|
|
|
|
|
|
|
|
2019-04-23 16:57:35 +02:00
|
|
|
class Assignment(RESTModelMixin, AgendaItemWithListOfSpeakersMixin, models.Model):
|
2016-02-11 22:58:32 +01:00
|
|
|
"""
|
|
|
|
Model for assignments.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2016-02-11 22:58:32 +01:00
|
|
|
access_permissions = AssignmentAccessPermissions()
|
2019-01-06 16:22:33 +01:00
|
|
|
can_see_permission = "assignments.can_see"
|
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 = (
|
2019-01-06 16:22:33 +01:00
|
|
|
(PHASE_SEARCH, "Searching for candidates"),
|
|
|
|
(PHASE_VOTING, "Voting"),
|
|
|
|
(PHASE_FINISHED, "Finished"),
|
2011-07-31 10:46:29 +02:00
|
|
|
)
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
title = models.CharField(max_length=100)
|
2015-01-25 15:10:34 +01:00
|
|
|
"""
|
|
|
|
Title of the assignment.
|
|
|
|
"""
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
description = models.TextField(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.
|
|
|
|
"""
|
|
|
|
|
2019-10-29 09:44:19 +01:00
|
|
|
default_poll_description = models.CharField(max_length=255, blank=True)
|
2015-01-25 15:10:34 +01:00
|
|
|
"""
|
|
|
|
Default text for the poll description.
|
|
|
|
"""
|
|
|
|
|
2019-01-06 16:22:33 +01:00
|
|
|
phase = models.IntegerField(choices=PHASES, default=PHASE_SEARCH)
|
2015-01-25 15:10:34 +01:00
|
|
|
"""
|
|
|
|
Phase in which the assignment is.
|
|
|
|
"""
|
|
|
|
|
|
|
|
related_users = models.ManyToManyField(
|
2019-01-06 16:22:33 +01:00
|
|
|
settings.AUTH_USER_MODEL, through="AssignmentRelatedUser"
|
|
|
|
)
|
2015-01-25 15:10:34 +01:00
|
|
|
"""
|
2020-02-24 16:55:07 +01:00
|
|
|
Users that are candidates.
|
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.
|
|
|
|
"""
|
|
|
|
|
2019-04-26 13:25:45 +02:00
|
|
|
attachments = models.ManyToManyField(Mediafile, blank=True)
|
|
|
|
"""
|
|
|
|
Mediafiles as attachments for this assignment.
|
|
|
|
"""
|
|
|
|
|
2019-11-12 09:20:10 +01:00
|
|
|
number_poll_candidates = models.BooleanField(default=False)
|
|
|
|
"""
|
|
|
|
Controls whether the candidates in polls for this assignment should be numbered or listed with bullet points.
|
|
|
|
"""
|
|
|
|
|
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 = (
|
2019-01-06 16:22:33 +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
|
|
|
)
|
2019-01-06 16:22:33 +01:00
|
|
|
ordering = ("title",)
|
2019-01-12 23:01:42 +01:00
|
|
|
verbose_name = "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
|
|
|
|
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
|
|
|
"""
|
2020-02-24 16:55:07 +01:00
|
|
|
return self.related_users.all()
|
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
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
def add_candidate(self, user):
|
2015-01-25 15:10:34 +01:00
|
|
|
"""
|
|
|
|
Adds the user as candidate.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
weight = (
|
|
|
|
self.assignment_related_users.aggregate(models.Max("weight"))["weight__max"]
|
|
|
|
or 0
|
|
|
|
)
|
2020-02-24 16:55:07 +01:00
|
|
|
defaults = {"weight": weight + 1}
|
2019-01-12 23:01:42 +01:00
|
|
|
self.assignment_related_users.update_or_create(user=user, defaults=defaults)
|
2012-07-03 00:05:48 +02:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
def remove_candidate(self, user):
|
2015-01-25 15:10:34 +01:00
|
|
|
"""
|
|
|
|
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):
|
2019-01-12 23:01:42 +01:00
|
|
|
raise ValueError(f"Invalid phase {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
|
|
|
|
2019-04-23 16:57:35 +02:00
|
|
|
def get_title_information(self):
|
2019-02-15 12:17:08 +01:00
|
|
|
return {"title": self.title}
|
2016-01-25 21:22:22 +01:00
|
|
|
|
2012-02-19 19:27:00 +01:00
|
|
|
|
2019-11-04 14:56:01 +01:00
|
|
|
class AssignmentVoteManager(BaseManager):
|
2019-10-29 12:58:37 +01:00
|
|
|
"""
|
2019-11-04 14:56:01 +01:00
|
|
|
Customized model manager to support our get_prefetched_queryset method.
|
2019-10-29 12:58:37 +01:00
|
|
|
"""
|
|
|
|
|
2019-11-04 14:56:01 +01:00
|
|
|
def get_prefetched_queryset(self, *args, **kwargs):
|
2019-10-29 12:58:37 +01:00
|
|
|
"""
|
|
|
|
Returns the normal queryset with all assignment votes. In the background we
|
|
|
|
join and prefetch all related models.
|
|
|
|
"""
|
2019-11-04 14:56:01 +01:00
|
|
|
return (
|
|
|
|
super()
|
|
|
|
.get_prefetched_queryset(*args, **kwargs)
|
|
|
|
.select_related("user", "option", "option__poll")
|
|
|
|
)
|
2019-10-29 12:58:37 +01:00
|
|
|
|
|
|
|
|
2015-01-17 14:25:05 +01:00
|
|
|
class AssignmentVote(RESTModelMixin, BaseVote):
|
2019-10-29 09:00:11 +01:00
|
|
|
access_permissions = AssignmentVoteAccessPermissions()
|
2019-10-29 12:58:37 +01:00
|
|
|
objects = AssignmentVoteManager()
|
|
|
|
|
2016-01-09 09:58:22 +01:00
|
|
|
option = models.ForeignKey(
|
2020-02-12 17:18:01 +01:00
|
|
|
"AssignmentOption", on_delete=CASCADE_AND_AUTOUPDATE, related_name="votes"
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
2012-07-13 11:16:06 +02:00
|
|
|
|
2015-12-10 00:20:59 +01:00
|
|
|
class Meta:
|
|
|
|
default_permissions = ()
|
|
|
|
|
2012-07-13 11:16:06 +02:00
|
|
|
|
2020-02-13 18:24:51 +01:00
|
|
|
class AssignmentOptionManager(BaseManager):
|
|
|
|
"""
|
|
|
|
Customized model manager to support our get_prefetched_queryset method.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def get_prefetched_queryset(self, *args, **kwargs):
|
|
|
|
"""
|
2020-03-11 10:22:03 +01:00
|
|
|
Returns the normal queryset. In the background we
|
2020-02-13 18:24:51 +01:00
|
|
|
join and prefetch all related models.
|
|
|
|
"""
|
|
|
|
return (
|
|
|
|
super()
|
|
|
|
.get_prefetched_queryset(*args, **kwargs)
|
|
|
|
.select_related("user", "poll")
|
2020-03-11 10:22:03 +01:00
|
|
|
.prefetch_related("votes")
|
2020-02-13 18:24:51 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2015-01-17 14:25:05 +01:00
|
|
|
class AssignmentOption(RESTModelMixin, BaseOption):
|
2019-11-12 18:30:26 +01:00
|
|
|
access_permissions = AssignmentOptionAccessPermissions()
|
2020-02-25 11:22:27 +01:00
|
|
|
can_see_permission = "assignments.can_see"
|
2020-02-13 18:24:51 +01:00
|
|
|
objects = AssignmentOptionManager()
|
2019-10-18 14:18:49 +02:00
|
|
|
vote_class = AssignmentVote
|
|
|
|
|
2016-01-09 09:58:22 +01:00
|
|
|
poll = models.ForeignKey(
|
2020-02-12 17:18:01 +01:00
|
|
|
"AssignmentPoll", on_delete=CASCADE_AND_AUTOUPDATE, related_name="options"
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
2019-10-18 14:18:49 +02:00
|
|
|
user = models.ForeignKey(
|
2019-01-19 14:02:13 +01:00
|
|
|
settings.AUTH_USER_MODEL, on_delete=SET_NULL_AND_AUTOUPDATE, null=True
|
|
|
|
)
|
2016-12-06 12:21:29 +01:00
|
|
|
weight = models.IntegerField(default=0)
|
|
|
|
|
2015-12-10 00:20:59 +01:00
|
|
|
class Meta:
|
|
|
|
default_permissions = ()
|
|
|
|
|
2015-01-17 14:25:05 +01:00
|
|
|
|
2019-11-04 14:56:01 +01:00
|
|
|
class AssignmentPollManager(BaseManager):
|
2019-10-29 12:58:37 +01:00
|
|
|
"""
|
2019-11-04 14:56:01 +01:00
|
|
|
Customized model manager to support our get_prefetched_queryset method.
|
2019-10-29 12:58:37 +01:00
|
|
|
"""
|
|
|
|
|
2019-11-04 14:56:01 +01:00
|
|
|
def get_prefetched_queryset(self, *args, **kwargs):
|
2019-10-29 12:58:37 +01:00
|
|
|
"""
|
|
|
|
Returns the normal queryset with all assignment polls. In the background we
|
|
|
|
join and prefetch all related models.
|
|
|
|
"""
|
|
|
|
return (
|
2019-11-04 14:56:01 +01:00
|
|
|
super()
|
|
|
|
.get_prefetched_queryset(*args, **kwargs)
|
2019-10-29 12:58:37 +01:00
|
|
|
.select_related("assignment")
|
2020-02-13 18:24:51 +01:00
|
|
|
.prefetch_related(
|
2020-03-11 10:22:03 +01:00
|
|
|
"options", "options__user", "options__votes", "voted", "groups"
|
2020-02-13 18:24:51 +01:00
|
|
|
)
|
2019-10-29 12:58:37 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
class AssignmentPoll(RESTModelMixin, BasePoll):
|
|
|
|
access_permissions = AssignmentPollAccessPermissions()
|
2020-02-25 11:22:27 +01:00
|
|
|
can_see_permission = "assignments.can_see"
|
2019-10-29 12:58:37 +01:00
|
|
|
objects = AssignmentPollManager()
|
|
|
|
|
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(
|
2020-02-12 17:18:01 +01:00
|
|
|
Assignment, on_delete=CASCADE_AND_AUTOUPDATE, related_name="polls"
|
2019-01-06 16:22:33 +01:00
|
|
|
)
|
2018-07-03 15:47:31 +02:00
|
|
|
|
2019-10-29 09:44:19 +01:00
|
|
|
description = models.CharField(max_length=255, blank=True)
|
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
POLLMETHOD_YN = "YN"
|
|
|
|
POLLMETHOD_YNA = "YNA"
|
|
|
|
POLLMETHOD_VOTES = "votes"
|
2020-02-24 16:55:07 +01:00
|
|
|
POLLMETHODS = (
|
|
|
|
(POLLMETHOD_VOTES, "Yes per candidate"),
|
|
|
|
(POLLMETHOD_YN, "Yes/No per candidate"),
|
|
|
|
(POLLMETHOD_YNA, "Yes/No/Abstain per candidate"),
|
|
|
|
)
|
2019-10-18 14:18:49 +02:00
|
|
|
pollmethod = models.CharField(max_length=5, choices=POLLMETHODS)
|
2015-12-10 00:20:59 +01:00
|
|
|
|
2019-10-29 09:44:19 +01:00
|
|
|
PERCENT_BASE_YN = "YN"
|
|
|
|
PERCENT_BASE_YNA = "YNA"
|
|
|
|
PERCENT_BASE_VOTES = "votes"
|
|
|
|
PERCENT_BASE_VALID = "valid"
|
|
|
|
PERCENT_BASE_CAST = "cast"
|
|
|
|
PERCENT_BASE_DISABLED = "disabled"
|
|
|
|
PERCENT_BASES = (
|
|
|
|
(PERCENT_BASE_YN, "Yes/No per candidate"),
|
|
|
|
(PERCENT_BASE_YNA, "Yes/No/Abstain per candidate"),
|
2020-03-15 23:56:49 +01:00
|
|
|
(PERCENT_BASE_VOTES, "Sum of votes including general No/Abstain"),
|
2019-10-29 09:44:19 +01:00
|
|
|
(PERCENT_BASE_VALID, "All valid ballots"),
|
|
|
|
(PERCENT_BASE_CAST, "All casted ballots"),
|
|
|
|
(PERCENT_BASE_DISABLED, "Disabled (no percents)"),
|
|
|
|
)
|
|
|
|
onehundred_percent_base = models.CharField(
|
|
|
|
max_length=8, blank=False, null=False, choices=PERCENT_BASES
|
|
|
|
)
|
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
global_abstain = models.BooleanField(default=True)
|
2020-03-11 10:22:03 +01:00
|
|
|
db_amount_global_abstain = models.DecimalField(
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
default=Decimal("0"),
|
|
|
|
validators=[MinValueValidator(Decimal("-2"))],
|
|
|
|
max_digits=15,
|
|
|
|
decimal_places=6,
|
|
|
|
)
|
2019-10-18 14:18:49 +02:00
|
|
|
global_no = models.BooleanField(default=True)
|
2020-03-11 10:22:03 +01:00
|
|
|
db_amount_global_no = models.DecimalField(
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
default=Decimal("0"),
|
|
|
|
validators=[MinValueValidator(Decimal("-2"))],
|
|
|
|
max_digits=15,
|
|
|
|
decimal_places=6,
|
|
|
|
)
|
2012-02-19 19:27:00 +01:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
votes_amount = models.IntegerField(default=1, validators=[MinValueValidator(1)])
|
|
|
|
""" For "votes" mode: The amount of votes a voter can give. """
|
2012-06-18 09:48:27 +02:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
allow_multiple_votes_per_candidate = models.BooleanField(default=False)
|
2014-01-11 17:07:47 +01:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
class Meta:
|
|
|
|
default_permissions = ()
|
2014-04-10 20:18:22 +02:00
|
|
|
|
2020-03-11 10:22:03 +01:00
|
|
|
def get_amount_global_abstain(self):
|
|
|
|
if not self.global_abstain:
|
|
|
|
return None
|
2020-03-12 15:42:41 +01:00
|
|
|
elif self.type == self.TYPE_ANALOG:
|
2020-03-11 10:22:03 +01:00
|
|
|
return self.db_amount_global_abstain
|
2020-03-12 15:42:41 +01:00
|
|
|
elif self.pollmethod == AssignmentPoll.POLLMETHOD_VOTES:
|
|
|
|
return sum(option.abstain for option in self.options.all())
|
2020-03-11 10:22:03 +01:00
|
|
|
else:
|
2019-10-29 09:44:19 +01:00
|
|
|
return None
|
|
|
|
|
2020-03-11 10:22:03 +01:00
|
|
|
def set_amount_global_abstain(self, value):
|
2020-03-12 15:42:41 +01:00
|
|
|
if self.type != self.TYPE_ANALOG:
|
|
|
|
raise ValueError("Do not set amount_global_abstain for non analog polls")
|
2020-03-11 10:22:03 +01:00
|
|
|
self.db_amount_global_abstain = value
|
|
|
|
|
|
|
|
amount_global_abstain = property(
|
|
|
|
get_amount_global_abstain, set_amount_global_abstain
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_amount_global_no(self):
|
|
|
|
if not self.global_no:
|
|
|
|
return None
|
2020-03-12 15:42:41 +01:00
|
|
|
elif self.type == self.TYPE_ANALOG:
|
2020-03-11 10:22:03 +01:00
|
|
|
return self.db_amount_global_no
|
2020-03-12 15:42:41 +01:00
|
|
|
elif self.pollmethod == AssignmentPoll.POLLMETHOD_VOTES:
|
|
|
|
return sum(option.no for option in self.options.all())
|
2020-03-11 10:22:03 +01:00
|
|
|
else:
|
2019-10-29 09:44:19 +01:00
|
|
|
return None
|
2020-03-11 10:22:03 +01:00
|
|
|
|
|
|
|
def set_amount_global_no(self, value):
|
2020-03-12 15:42:41 +01:00
|
|
|
if self.type != self.TYPE_ANALOG:
|
|
|
|
raise ValueError("Do not set amount_global_no for non analog polls")
|
2020-03-11 10:22:03 +01:00
|
|
|
self.db_amount_global_no = value
|
|
|
|
|
|
|
|
amount_global_no = property(get_amount_global_no, set_amount_global_no)
|
2019-10-29 09:44:19 +01:00
|
|
|
|
2020-02-13 18:24:51 +01:00
|
|
|
def create_options(self, skip_autoupdate=False):
|
2019-10-18 14:18:49 +02:00
|
|
|
related_users = AssignmentRelatedUser.objects.filter(
|
|
|
|
assignment__id=self.assignment.id
|
2020-02-24 16:55:07 +01:00
|
|
|
)
|
2019-11-12 18:30:26 +01:00
|
|
|
|
|
|
|
for related_user in related_users:
|
2020-02-13 18:24:51 +01:00
|
|
|
option = AssignmentOption(
|
2019-10-18 14:18:49 +02:00
|
|
|
user=related_user.user, weight=related_user.weight, poll=self
|
|
|
|
)
|
2020-02-13 18:24:51 +01:00
|
|
|
option.save(skip_autoupdate=skip_autoupdate)
|
2019-10-18 14:18:49 +02:00
|
|
|
|
|
|
|
# Add all candidates to list of speakers of related agenda item
|
2019-10-29 09:44:19 +01:00
|
|
|
if config["assignment_poll_add_candidates_to_list_of_speakers"]:
|
2019-10-18 14:18:49 +02:00
|
|
|
for related_user in related_users:
|
|
|
|
try:
|
|
|
|
Speaker.objects.add(
|
|
|
|
related_user.user,
|
|
|
|
self.assignment.list_of_speakers,
|
|
|
|
skip_autoupdate=True,
|
|
|
|
)
|
|
|
|
except OpenSlidesError:
|
|
|
|
# The Speaker is already on the list. Do nothing.
|
|
|
|
pass
|
2020-02-13 18:24:51 +01:00
|
|
|
if not skip_autoupdate:
|
|
|
|
inform_changed_data(self.assignment.list_of_speakers)
|
2020-03-11 10:22:03 +01:00
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
self.db_amount_global_abstain = Decimal(0)
|
|
|
|
self.db_amount_global_no = Decimal(0)
|
|
|
|
super().reset()
|