models.py 3.45 KB
Newer Older
Sli's avatar
Sli committed
1 2 3 4 5 6
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
Sli's avatar
Sli committed
7
from core.models import User, Group
Sli's avatar
Sli committed
8 9 10 11


class Election(models.Model):
    """
Sli's avatar
Sli committed
12
    This class allows to create a new election
Sli's avatar
Sli committed
13 14 15
    """
    title = models.CharField(_('title'), max_length=255)
    description = models.TextField(_('description'), null=True, blank=True)
Sli's avatar
Sli committed
16 17
    start_candidature = models.DateTimeField(_('start candidature'), blank=False)
    end_candidature = models.DateTimeField(_('end candidature'), blank=False)
Sli's avatar
Sli committed
18 19 20
    start_date = models.DateTimeField(_('start date'), blank=False)
    end_date = models.DateTimeField(_('end date'), blank=False)

21 22 23 24
    edit_groups = models.ManyToManyField(Group, related_name="editable_elections", verbose_name=_("edit group"), blank=True)
    view_groups = models.ManyToManyField(Group, related_name="viewable_elections", verbose_name=_("view group"), blank=True)
    vote_groups = models.ManyToManyField(Group, related_name="votable_elections", verbose_name=_("vote group"), blank=True)
    candidature_groups = models.ManyToManyField(Group, related_name="candidate_elections", verbose_name=_("candidature group"), blank=True)
Sli's avatar
Sli committed
25

Sli's avatar
Sli committed
26 27 28 29 30 31 32 33
    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)

Sli's avatar
Sli committed
34
    @property
Sli's avatar
Sli committed
35
    def is_candidature_active(self):
Sli's avatar
Sli committed
36
        now = timezone.now()
Sli's avatar
Sli committed
37
        return bool(now <= self.end_candidature and now >= self.start_candidature)
Sli's avatar
Sli committed
38

Sli's avatar
Sli committed
39
    def has_voted(self, user):
Sli's avatar
Sli committed
40 41
        return False
        # return self.has_voted.filter(id=user.id).exists()
Sli's avatar
Sli committed
42

Sli's avatar
Sli committed
43 44 45
    def get_results(self):
        pass

Sli's avatar
Sli committed
46
    # Permissions
Sli's avatar
Sli committed
47

Sli's avatar
Sli committed
48

Sli's avatar
Sli committed
49
class Role(models.Model):
Sli's avatar
Sli committed
50
    """
Sli's avatar
Sli committed
51
    This class allows to create a new role avaliable for a candidature
Sli's avatar
Sli committed
52
    """
Sli's avatar
Sli committed
53
    election = models.ForeignKey(Election, related_name='role', verbose_name=_("election"))
Sli's avatar
Sli committed
54 55
    title = models.CharField(_('title'), max_length=255)
    description = models.TextField(_('description'), null=True, blank=True)
Sli's avatar
Sli committed
56
    has_voted = models.ManyToManyField(User, verbose_name=('has voted'), related_name='has_voted')
Sli's avatar
Sli committed
57
    max_choice = models.IntegerField(_('max choice'), default=1)
Sli's avatar
Sli committed
58 59 60 61 62

    def __str__(self):
        return ("%s : %s") % (self.election.title, self.title)


Sli's avatar
Sli committed
63
class ElectionList(models.Model):
Sli's avatar
Sli committed
64 65 66 67
    """
    To allow per list vote
    """
    title = models.CharField(_('title'), max_length=255)
Sli's avatar
Sli committed
68
    election = models.ForeignKey(Election, related_name='election_list', verbose_name=_("election"))
Sli's avatar
Sli committed
69

70 71 72
    def __str__(self):
        return self.title

Sli's avatar
Sli committed
73

Sli's avatar
Sli committed
74
class Candidature(models.Model):
Sli's avatar
Sli committed
75
    """
Sli's avatar
Sli committed
76
    This class is a component of responsability
Sli's avatar
Sli committed
77
    """
Sli's avatar
Sli committed
78
    role = models.ForeignKey(Role, related_name='candidature', verbose_name=_("role"))
Sli's avatar
Sli committed
79
    user = models.ForeignKey(User, verbose_name=_('user'), related_name='candidate', blank=True)
Sli's avatar
Sli committed
80
    program = models.TextField(_('description'), null=True, blank=True)
Sli's avatar
Sli committed
81
    election_list = models.ForeignKey(ElectionList, related_name='candidature', verbose_name=_('election_list'))
Sli's avatar
Sli committed
82

83 84 85
    def __str__(self):
        return "%s : %s" % (self.role.title, self.user.username)

Sli's avatar
Sli committed
86 87 88 89 90

class Vote(models.Model):
    """
    This class allows to vote for candidates
    """
Sli's avatar
Sli committed
91 92
    role = models.ForeignKey(Role, related_name='vote', verbose_name=_("role"))
    candidature = models.ManyToManyField(Candidature, related_name='vote', verbose_name=_("candidature"))
Sli's avatar
Sli committed
93 94 95

    def __str__(self):
        return "Vote"