2016-12-05 19:18:03 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from django.utils import timezone
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
from datetime import timedelta
|
2016-12-19 19:30:19 +00:00
|
|
|
from core.models import User, Group
|
2016-12-05 19:18:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Election(models.Model):
|
|
|
|
"""
|
2016-12-07 22:55:05 +00:00
|
|
|
This class allows to create a new election
|
2016-12-05 19:18:03 +00:00
|
|
|
"""
|
|
|
|
title = models.CharField(_('title'), max_length=255)
|
|
|
|
description = models.TextField(_('description'), null=True, blank=True)
|
2016-12-13 21:11:06 +00:00
|
|
|
start_candidature = models.DateTimeField(_('start candidature'), blank=False)
|
|
|
|
end_candidature = models.DateTimeField(_('end candidature'), blank=False)
|
2016-12-05 19:18:03 +00:00
|
|
|
start_date = models.DateTimeField(_('start date'), blank=False)
|
|
|
|
end_date = models.DateTimeField(_('end date'), blank=False)
|
|
|
|
|
2016-12-22 00:57:17 +00:00
|
|
|
edit_groups = models.ManyToManyField(Group, related_name="editable_elections", verbose_name=_("edit groups"), blank=True)
|
|
|
|
view_groups = models.ManyToManyField(Group, related_name="viewable_elections", verbose_name=_("view groups"), blank=True)
|
|
|
|
vote_groups = models.ManyToManyField(Group, related_name="votable_elections", verbose_name=_("vote groups"), blank=True)
|
|
|
|
candidature_groups = models.ManyToManyField(Group, related_name="candidate_elections", verbose_name=_("candidature groups"), blank=True)
|
2016-12-19 19:30:19 +00:00
|
|
|
|
2016-12-05 19:18:03 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.title
|
|
|
|
|
|
|
|
@property
|
2016-12-23 00:04:26 +00:00
|
|
|
def is_vote_active(self):
|
2016-12-05 19:18:03 +00:00
|
|
|
now = timezone.now()
|
|
|
|
return bool(now <= self.end_date and now >= self.start_date)
|
|
|
|
|
2016-12-23 20:53:54 +00:00
|
|
|
@property
|
|
|
|
def is_vote_finished(self):
|
|
|
|
return bool(timezone.now() > self.end_date)
|
|
|
|
|
2016-12-07 22:55:05 +00:00
|
|
|
@property
|
2016-12-13 21:11:06 +00:00
|
|
|
def is_candidature_active(self):
|
2016-12-07 22:55:05 +00:00
|
|
|
now = timezone.now()
|
2016-12-13 21:11:06 +00:00
|
|
|
return bool(now <= self.end_candidature and now >= self.start_candidature)
|
2016-12-07 22:55:05 +00:00
|
|
|
|
2016-12-22 20:16:46 +00:00
|
|
|
def has_voted(self, user):
|
2016-12-23 21:58:54 +00:00
|
|
|
for role in self.roles.all():
|
2016-12-22 22:17:35 +00:00
|
|
|
if role.user_has_voted(user):
|
|
|
|
return True
|
|
|
|
return False
|
2016-12-22 20:16:46 +00:00
|
|
|
|
2016-12-22 21:00:02 +00:00
|
|
|
def can_candidate(self, user):
|
|
|
|
for group in self.candidature_groups.all():
|
|
|
|
if user.is_in_group(group):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2016-12-22 23:34:30 +00:00
|
|
|
def can_vote(self, user):
|
2016-12-23 00:04:26 +00:00
|
|
|
if not self.is_vote_active or self.has_voted(user):
|
|
|
|
return False
|
2016-12-22 23:34:30 +00:00
|
|
|
for group in self.vote_groups.all():
|
|
|
|
if user.is_in_group(group):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2016-12-23 16:13:46 +00:00
|
|
|
@property
|
2016-12-23 19:40:54 +00:00
|
|
|
def results(self):
|
2016-12-23 16:13:46 +00:00
|
|
|
results = {}
|
2016-12-23 21:58:54 +00:00
|
|
|
for role in self.roles.all():
|
2016-12-23 19:40:54 +00:00
|
|
|
results[role.title] = role.results
|
2016-12-23 16:13:46 +00:00
|
|
|
return results
|
|
|
|
|
2016-12-19 19:30:19 +00:00
|
|
|
# Permissions
|
2016-12-19 15:45:38 +00:00
|
|
|
|
2016-12-05 19:18:03 +00:00
|
|
|
|
2016-12-13 21:11:06 +00:00
|
|
|
class Role(models.Model):
|
2016-12-05 19:18:03 +00:00
|
|
|
"""
|
2016-12-13 21:11:06 +00:00
|
|
|
This class allows to create a new role avaliable for a candidature
|
2016-12-05 19:18:03 +00:00
|
|
|
"""
|
2016-12-23 21:58:54 +00:00
|
|
|
election = models.ForeignKey(Election, related_name='roles', verbose_name=_("election"))
|
2016-12-05 19:18:03 +00:00
|
|
|
title = models.CharField(_('title'), max_length=255)
|
|
|
|
description = models.TextField(_('description'), null=True, blank=True)
|
2016-12-19 02:54:57 +00:00
|
|
|
has_voted = models.ManyToManyField(User, verbose_name=('has voted'), related_name='has_voted')
|
2016-12-19 19:30:19 +00:00
|
|
|
max_choice = models.IntegerField(_('max choice'), default=1)
|
2016-12-05 19:18:03 +00:00
|
|
|
|
2016-12-21 23:15:16 +00:00
|
|
|
def user_has_voted(self, user):
|
2016-12-22 00:28:51 +00:00
|
|
|
return self.has_voted.filter(id=user.id).exists()
|
2016-12-21 23:15:16 +00:00
|
|
|
|
2016-12-23 16:13:46 +00:00
|
|
|
@property
|
2016-12-23 19:40:54 +00:00
|
|
|
def results(self):
|
2016-12-23 16:13:46 +00:00
|
|
|
results = {}
|
|
|
|
total_vote = self.has_voted.count() * self.max_choice
|
|
|
|
if total_vote == 0:
|
|
|
|
return None
|
|
|
|
non_blank = 0
|
2016-12-23 21:58:54 +00:00
|
|
|
for candidature in self.candidatures.all():
|
2016-12-23 16:13:46 +00:00
|
|
|
cand_results = {}
|
2016-12-23 21:58:54 +00:00
|
|
|
cand_results['vote'] = self.votes.filter(candidature=candidature).count()
|
2016-12-23 16:13:46 +00:00
|
|
|
cand_results['percent'] = cand_results['vote'] * 100 / total_vote
|
|
|
|
non_blank += cand_results['vote']
|
|
|
|
results[candidature.user.username] = cand_results
|
|
|
|
results['total vote'] = total_vote
|
|
|
|
results['blank vote'] = {'vote': total_vote - non_blank,
|
|
|
|
'percent': (total_vote - non_blank) * 100 / total_vote}
|
|
|
|
return results
|
|
|
|
|
2016-12-05 19:18:03 +00:00
|
|
|
def __str__(self):
|
|
|
|
return ("%s : %s") % (self.election.title, self.title)
|
|
|
|
|
|
|
|
|
2016-12-19 15:45:38 +00:00
|
|
|
class ElectionList(models.Model):
|
2016-12-14 17:10:15 +00:00
|
|
|
"""
|
|
|
|
To allow per list vote
|
|
|
|
"""
|
|
|
|
title = models.CharField(_('title'), max_length=255)
|
2016-12-23 21:58:54 +00:00
|
|
|
election = models.ForeignKey(Election, related_name='election_lists', verbose_name=_("election"))
|
2016-12-14 17:10:15 +00:00
|
|
|
|
2016-12-20 20:03:52 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.title
|
|
|
|
|
2016-12-14 17:10:15 +00:00
|
|
|
|
2016-12-13 21:11:06 +00:00
|
|
|
class Candidature(models.Model):
|
2016-12-05 19:18:03 +00:00
|
|
|
"""
|
2016-12-07 22:55:05 +00:00
|
|
|
This class is a component of responsability
|
2016-12-05 19:18:03 +00:00
|
|
|
"""
|
2016-12-23 21:58:54 +00:00
|
|
|
role = models.ForeignKey(Role, related_name='candidatures', verbose_name=_("role"))
|
|
|
|
user = models.ForeignKey(User, verbose_name=_('user'), related_name='candidates', blank=True)
|
2016-12-05 19:18:03 +00:00
|
|
|
program = models.TextField(_('description'), null=True, blank=True)
|
2016-12-23 21:58:54 +00:00
|
|
|
election_list = models.ForeignKey(ElectionList, related_name='candidatures', verbose_name=_('election_list'))
|
2016-12-07 22:55:05 +00:00
|
|
|
|
2016-12-21 16:33:16 +00:00
|
|
|
def __str__(self):
|
|
|
|
return "%s : %s" % (self.role.title, self.user.username)
|
|
|
|
|
2016-12-07 22:55:05 +00:00
|
|
|
|
|
|
|
class Vote(models.Model):
|
|
|
|
"""
|
|
|
|
This class allows to vote for candidates
|
|
|
|
"""
|
2016-12-23 21:58:54 +00:00
|
|
|
role = models.ForeignKey(Role, related_name='votes', verbose_name=_("role"))
|
|
|
|
candidature = models.ManyToManyField(Candidature, related_name='votes', verbose_name=_("candidature"))
|
2016-12-07 22:55:05 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "Vote"
|