models.py 3.49 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
40
41
    def get_results(self):
        pass

Sli's avatar
Sli committed
42
43
44
    def has_voted(self, user):
        return False

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

Sli's avatar
Sli committed
47

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

Sli's avatar
Sli committed
58
    def user_has_voted(self, user):
Sli's avatar
Sli committed
59
        return self.has_voted.filter(id=user.id).exists()
Sli's avatar
Sli committed
60

Sli's avatar
Sli committed
61
62
63
64
    def __str__(self):
        return ("%s : %s") % (self.election.title, self.title)


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

72
73
74
    def __str__(self):
        return self.title

Sli's avatar
Sli committed
75

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

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

Sli's avatar
Sli committed
88
89
90
91
92

class Vote(models.Model):
    """
    This class allows to vote for candidates
    """
Sli's avatar
Sli committed
93
94
    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
95
96
97

    def __str__(self):
        return "Vote"