models.py 5 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 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)
Sli's avatar
Sli committed
25

Sli's avatar
Sli committed
26
27
28
29
    def __str__(self):
        return self.title

    @property
30
    def is_vote_active(self):
Sli's avatar
Sli committed
31
32
33
        now = timezone.now()
        return bool(now <= self.end_date and now >= self.start_date)

34
35
36
37
    @property
    def is_vote_finished(self):
        return bool(timezone.now() > self.end_date)

Sli's avatar
Sli committed
38
    @property
Sli's avatar
Sli committed
39
    def is_candidature_active(self):
Sli's avatar
Sli committed
40
        now = timezone.now()
Sli's avatar
Sli committed
41
        return bool(now <= self.end_candidature and now >= self.start_candidature)
Sli's avatar
Sli committed
42

43
    def has_voted(self, user):
Sli's avatar
Sli committed
44
        for role in self.roles.all():
Sli's avatar
Sli committed
45
46
47
            if role.user_has_voted(user):
                return True
        return False
48

49
50
51
52
53
54
    def can_candidate(self, user):
        for group in self.candidature_groups.all():
            if user.is_in_group(group):
                return True
        return False

Sli's avatar
Sli committed
55
    def can_vote(self, user):
56
57
        if not self.is_vote_active or self.has_voted(user):
            return False
Sli's avatar
Sli committed
58
59
60
61
62
        for group in self.vote_groups.all():
            if user.is_in_group(group):
                return True
        return False

Sli's avatar
Sli committed
63
    @property
64
    def results(self):
Sli's avatar
Sli committed
65
        results = {}
Sli's avatar
Sli committed
66
        for role in self.roles.all():
67
            results[role.title] = role.results
Sli's avatar
Sli committed
68
69
        return results

Sli's avatar
Sli committed
70
    # Permissions
Sli's avatar
Sli committed
71

Sli's avatar
Sli committed
72

Sli's avatar
Sli committed
73
class Role(models.Model):
Sli's avatar
Sli committed
74
    """
Sli's avatar
Sli committed
75
    This class allows to create a new role avaliable for a candidature
Sli's avatar
Sli committed
76
    """
Sli's avatar
Sli committed
77
    election = models.ForeignKey(Election, related_name='roles', verbose_name=_("election"))
Sli's avatar
Sli committed
78
79
    title = models.CharField(_('title'), max_length=255)
    description = models.TextField(_('description'), null=True, blank=True)
Sli's avatar
Sli committed
80
    has_voted = models.ManyToManyField(User, verbose_name=('has voted'), related_name='has_voted')
Sli's avatar
Sli committed
81
    max_choice = models.IntegerField(_('max choice'), default=1)
Sli's avatar
Sli committed
82

Sli's avatar
Sli committed
83
    def user_has_voted(self, user):
Sli's avatar
Sli committed
84
        return self.has_voted.filter(id=user.id).exists()
Sli's avatar
Sli committed
85

Sli's avatar
Sli committed
86
    @property
87
    def results(self):
Sli's avatar
Sli committed
88
89
90
91
92
        results = {}
        total_vote = self.has_voted.count() * self.max_choice
        if total_vote == 0:
            return None
        non_blank = 0
Sli's avatar
Sli committed
93
        for candidature in self.candidatures.all():
Sli's avatar
Sli committed
94
            cand_results = {}
Sli's avatar
Sli committed
95
            cand_results['vote'] = self.votes.filter(candidature=candidature).count()
Sli's avatar
Sli committed
96
97
98
99
100
101
102
103
            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

Sli's avatar
Sli committed
104
105
106
107
    def __str__(self):
        return ("%s : %s") % (self.election.title, self.title)


Sli's avatar
Sli committed
108
class ElectionList(models.Model):
Sli's avatar
Sli committed
109
110
111
112
    """
    To allow per list vote
    """
    title = models.CharField(_('title'), max_length=255)
Sli's avatar
Sli committed
113
    election = models.ForeignKey(Election, related_name='election_lists', verbose_name=_("election"))
Sli's avatar
Sli committed
114

115
116
117
    def __str__(self):
        return self.title

Sli's avatar
Sli committed
118

Sli's avatar
Sli committed
119
class Candidature(models.Model):
Sli's avatar
Sli committed
120
    """
Sli's avatar
Sli committed
121
    This class is a component of responsability
Sli's avatar
Sli committed
122
    """
Sli's avatar
Sli committed
123
124
    role = models.ForeignKey(Role, related_name='candidatures', verbose_name=_("role"))
    user = models.ForeignKey(User, verbose_name=_('user'), related_name='candidates', blank=True)
Sli's avatar
Sli committed
125
    program = models.TextField(_('description'), null=True, blank=True)
Sli's avatar
Sli committed
126
    election_list = models.ForeignKey(ElectionList, related_name='candidatures', verbose_name=_('election_list'))
Sli's avatar
Sli committed
127

128
129
130
    def __str__(self):
        return "%s : %s" % (self.role.title, self.user.username)

Sli's avatar
Sli committed
131
132
133
134
135

class Vote(models.Model):
    """
    This class allows to vote for candidates
    """
Sli's avatar
Sli committed
136
137
    role = models.ForeignKey(Role, related_name='votes', verbose_name=_("role"))
    candidature = models.ManyToManyField(Candidature, related_name='votes', verbose_name=_("candidature"))
Sli's avatar
Sli committed
138
139
140

    def __str__(self):
        return "Vote"