2018-08-22 17:34:16 +02:00
|
|
|
from decimal import Decimal
|
2018-08-22 22:00:08 +02:00
|
|
|
from typing import Optional, Type
|
2014-04-10 20:18:22 +02:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
from django.conf import settings
|
2018-08-22 17:34:16 +02:00
|
|
|
from django.core.validators import MinValueValidator
|
2011-07-31 10:46:29 +02:00
|
|
|
from django.db import models
|
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
from ..utils.autoupdate import inform_deleted_data
|
|
|
|
from ..utils.models import SET_NULL_AND_AUTOUPDATE
|
2012-07-03 00:05:48 +02:00
|
|
|
|
2012-07-13 11:16:06 +02:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
class BaseVote(models.Model):
|
|
|
|
"""
|
|
|
|
All subclasses must have option attribute with the related name "votes"
|
2012-07-13 11:16:06 +02:00
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
weight = models.DecimalField(
|
|
|
|
default=Decimal("1"),
|
|
|
|
validators=[MinValueValidator(Decimal("-2"))],
|
|
|
|
max_digits=15,
|
|
|
|
decimal_places=6,
|
|
|
|
)
|
|
|
|
value = models.CharField(max_length=1, choices=(("Y", "Y"), ("N", "N"), ("A", "A")))
|
|
|
|
user = models.ForeignKey(
|
|
|
|
settings.AUTH_USER_MODEL,
|
|
|
|
default=None,
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
on_delete=SET_NULL_AND_AUTOUPDATE,
|
|
|
|
)
|
2013-12-02 22:29:11 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
def get_root_rest_element(self):
|
|
|
|
return self.option.get_root_rest_element()
|
2013-12-02 22:29:11 +01:00
|
|
|
|
2011-09-08 12:30:34 +02:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
class BaseOption(models.Model):
|
2012-07-13 11:16:06 +02:00
|
|
|
"""
|
2019-10-18 14:18:49 +02:00
|
|
|
All subclasses must have poll attribute with the related name "options"
|
2012-07-13 11:16:06 +02:00
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
vote_class: Optional[Type["BaseVote"]] = None
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2013-12-02 22:29:11 +01:00
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
@property
|
|
|
|
def yes(self) -> Decimal:
|
|
|
|
return self.sum_weight("Y")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def no(self) -> Decimal:
|
|
|
|
return self.sum_weight("N")
|
|
|
|
|
|
|
|
@property
|
|
|
|
def abstain(self) -> Decimal:
|
|
|
|
return self.sum_weight("A")
|
|
|
|
|
|
|
|
def sum_weight(self, value):
|
|
|
|
# We could do this in a nice .aggregate(Sum...) querystatement,
|
|
|
|
# but these might be expensive DB queries, because they are not preloaded.
|
|
|
|
# With this in-logic-counting, we operate inmemory.
|
|
|
|
weight_sum = Decimal(0)
|
|
|
|
for vote in self.votes.all():
|
|
|
|
if vote.value == value:
|
|
|
|
weight_sum += vote.weight
|
|
|
|
return weight_sum
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_vote_class(cls):
|
|
|
|
if cls.vote_class is None:
|
|
|
|
raise NotImplementedError(
|
|
|
|
f"The option class {cls} has to have an attribute vote_class."
|
|
|
|
)
|
|
|
|
return cls.vote_class
|
2013-12-02 22:29:11 +01:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
def get_root_rest_element(self):
|
|
|
|
return self.poll.get_root_rest_element()
|
2013-12-02 22:29:11 +01:00
|
|
|
|
2012-02-19 19:27:00 +01:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
class BasePoll(models.Model):
|
|
|
|
option_class: Optional[Type["BaseOption"]] = None
|
|
|
|
|
|
|
|
STATE_CREATED = 1
|
|
|
|
STATE_STARTED = 2
|
|
|
|
STATE_FINISHED = 3
|
|
|
|
STATE_PUBLISHED = 4
|
|
|
|
STATES = (
|
|
|
|
(STATE_CREATED, "Created"),
|
|
|
|
(STATE_STARTED, "Started"),
|
|
|
|
(STATE_FINISHED, "Finished"),
|
|
|
|
(STATE_PUBLISHED, "Published"),
|
|
|
|
)
|
|
|
|
state = models.IntegerField(choices=STATES, default=STATE_CREATED)
|
|
|
|
|
|
|
|
TYPE_ANALOG = "analog"
|
|
|
|
TYPE_NAMED = "named"
|
|
|
|
TYPE_PSEUDOANONYMOUS = "pseudoanonymous"
|
|
|
|
TYPES = (
|
|
|
|
(TYPE_ANALOG, "Analog"),
|
|
|
|
(TYPE_NAMED, "Named"),
|
|
|
|
(TYPE_PSEUDOANONYMOUS, "Pseudoanonymous"),
|
|
|
|
)
|
|
|
|
type = models.CharField(max_length=64, blank=False, null=False, choices=TYPES)
|
2012-02-19 17:31:17 +01:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
title = models.CharField(max_length=255, blank=True, null=False)
|
|
|
|
groups = models.ManyToManyField(settings.AUTH_GROUP_MODEL, blank=True)
|
|
|
|
voted = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True)
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
db_votesvalid = models.DecimalField(
|
2019-01-06 16:22:33 +01:00
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
validators=[MinValueValidator(Decimal("-2"))],
|
|
|
|
max_digits=15,
|
|
|
|
decimal_places=6,
|
|
|
|
)
|
2019-10-18 14:18:49 +02:00
|
|
|
db_votesinvalid = models.DecimalField(
|
2019-01-06 16:22:33 +01:00
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
validators=[MinValueValidator(Decimal("-2"))],
|
|
|
|
max_digits=15,
|
|
|
|
decimal_places=6,
|
|
|
|
)
|
2019-10-18 14:18:49 +02:00
|
|
|
db_votescast = models.DecimalField(
|
2019-01-06 16:22:33 +01:00
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
validators=[MinValueValidator(Decimal("-2"))],
|
|
|
|
max_digits=15,
|
|
|
|
decimal_places=6,
|
|
|
|
)
|
2013-12-02 22:29:11 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
2012-02-19 17:31:17 +01:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
def get_votesvalid(self):
|
|
|
|
if self.type == self.TYPE_ANALOG:
|
|
|
|
return self.db_votesvalid
|
|
|
|
else:
|
|
|
|
return Decimal(self.count_users_voted())
|
2012-03-03 11:16:10 +01:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
def set_votesvalid(self, value):
|
|
|
|
if self.type != self.TYPE_ANALOG:
|
|
|
|
raise ValueError("Do not set votesvalid for non analog polls")
|
|
|
|
self.db_votesvalid = value
|
2013-12-02 22:29:11 +01:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
votesvalid = property(get_votesvalid, set_votesvalid)
|
2012-03-03 11:16:10 +01:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
def get_votesinvalid(self):
|
|
|
|
if self.type == self.TYPE_ANALOG:
|
|
|
|
return self.db_votesinvalid
|
|
|
|
else:
|
|
|
|
return Decimal(0)
|
2012-03-03 11:16:10 +01:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
def set_votesinvalid(self, value):
|
|
|
|
if self.type != self.TYPE_ANALOG:
|
|
|
|
raise ValueError("Do not set votesinvalid for non analog polls")
|
|
|
|
self.db_votesinvalid = value
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
votesinvalid = property(get_votesinvalid, set_votesinvalid)
|
2011-07-31 10:46:29 +02:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
def get_votescast(self):
|
|
|
|
if self.type == self.TYPE_ANALOG:
|
|
|
|
return self.db_votescast
|
|
|
|
else:
|
|
|
|
return Decimal(self.count_users_voted())
|
2013-12-02 22:29:11 +01:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
def set_votescast(self, value):
|
|
|
|
if self.type != self.TYPE_ANALOG:
|
|
|
|
raise ValueError("Do not set votescast for non analog polls")
|
|
|
|
self.db_votescast = value
|
2012-02-15 13:44:55 +01:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
votescast = property(get_votescast, set_votescast)
|
2012-04-17 17:35:50 +02:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
def count_users_voted(self):
|
|
|
|
return self.voted.all().count()
|
2012-02-14 16:31:21 +01:00
|
|
|
|
|
|
|
def get_options(self):
|
2012-04-17 17:35:50 +02:00
|
|
|
"""
|
2013-12-02 22:29:11 +01:00
|
|
|
Returns the option objects for the poll.
|
2012-04-17 17:35:50 +02:00
|
|
|
"""
|
2012-02-14 16:31:21 +01:00
|
|
|
return self.get_option_class().objects.filter(poll=self)
|
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
def create_options(self):
|
|
|
|
""" Should be called after creation of this model. """
|
|
|
|
raise NotImplementedError()
|
2012-02-14 16:31:21 +01:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
@classmethod
|
|
|
|
def get_option_class(cls):
|
|
|
|
if cls.option_class is None:
|
|
|
|
raise NotImplementedError(
|
|
|
|
f"The poll class {cls} has to have an attribute option_class."
|
|
|
|
)
|
|
|
|
return cls.option_class
|
2012-02-14 16:31:21 +01:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
@classmethod
|
|
|
|
def get_vote_class(cls):
|
|
|
|
return cls.get_option_class().get_vote_class()
|
2012-07-13 11:16:06 +02:00
|
|
|
|
|
|
|
def get_votes(self):
|
|
|
|
"""
|
2013-12-02 22:29:11 +01:00
|
|
|
Return a QuerySet with all vote objects related to this poll.
|
2019-10-18 14:18:49 +02:00
|
|
|
|
|
|
|
TODO: This might be a performance issue when used in properties that are serialized.
|
2012-07-13 11:16:06 +02:00
|
|
|
"""
|
2012-07-16 14:29:30 +02:00
|
|
|
return self.get_vote_class().objects.filter(option__poll__id=self.id)
|
2012-07-13 11:16:06 +02:00
|
|
|
|
2019-10-18 14:18:49 +02:00
|
|
|
def pseudoanonymize(self):
|
|
|
|
for vote in self.get_votes():
|
|
|
|
vote.user = None
|
|
|
|
vote.save()
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
self.voted.clear()
|
|
|
|
|
|
|
|
# Delete votes
|
|
|
|
votes = self.get_votes()
|
|
|
|
votes_id = [vote.id for vote in votes]
|
|
|
|
votes.delete()
|
|
|
|
collection = self.get_vote_class().get_collection_string()
|
|
|
|
inform_deleted_data((collection, id) for id in votes_id)
|
|
|
|
|
|
|
|
# Reset state
|
|
|
|
self.state = BasePoll.STATE_CREATED
|
|
|
|
if self.type == self.TYPE_ANALOG:
|
|
|
|
self.votesvalid = None
|
|
|
|
self.votesinvalid = None
|
|
|
|
self.votescast = None
|
|
|
|
self.save()
|