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
|
|
|
|
from core.models import User
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.title
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_active(self):
|
|
|
|
now = timezone.now()
|
|
|
|
return bool(now <= self.end_date and now >= self.start_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-05 22:49:13 +00:00
|
|
|
def has_voted(self, user):
|
2016-12-13 21:11:06 +00:00
|
|
|
return self.has_voted.filter(id=user.id).exists()
|
2016-12-05 22:49:13 +00:00
|
|
|
|
2016-12-05 19:18:03 +00:00
|
|
|
def get_results(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
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-13 21:11:06 +00:00
|
|
|
election = models.ForeignKey(Election, related_name='role', 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)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return ("%s : %s") % (self.election.title, self.title)
|
|
|
|
|
|
|
|
|
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-13 21:11:06 +00:00
|
|
|
role = models.ForeignKey(Role, related_name='candidature', verbose_name=_("role"))
|
2016-12-07 22:55:05 +00:00
|
|
|
user = models.ForeignKey(User, verbose_name=_('user'), related_name='candidate', blank=True)
|
2016-12-05 19:18:03 +00:00
|
|
|
program = models.TextField(_('description'), null=True, blank=True)
|
2016-12-13 21:11:06 +00:00
|
|
|
has_voted = models.ManyToManyField(User, verbose_name=_('has_voted'), related_name='has_voted')
|
2016-12-05 19:18:03 +00:00
|
|
|
|
2016-12-13 21:11:06 +00:00
|
|
|
|
|
|
|
class List(models.Model):
|
|
|
|
"""
|
|
|
|
To allow per list vote
|
|
|
|
"""
|
2016-12-13 21:22:19 +00:00
|
|
|
title = models.CharField(_('title'), max_length=255)
|
2016-12-07 22:55:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Vote(models.Model):
|
|
|
|
"""
|
|
|
|
This class allows to vote for candidates
|
|
|
|
"""
|
2016-12-13 21:11:06 +00:00
|
|
|
role = models.ForeignKey(Role, related_name='vote', verbose_name=_("role"))
|
|
|
|
candidature = models.ManyToManyField(Candidature, related_name='vote', verbose_name=_("candidature"))
|
2016-12-07 22:55:05 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "Vote"
|