models.py 4.86 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
    voters = models.ManyToManyField(User, verbose_name=('voters'), related_name='voted_elections')
Sli's avatar
Sli committed
26

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

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

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

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

44
45
46
47
48
49
    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
50
    def can_vote(self, user):
51
52
        if not self.is_vote_active or self.has_voted(user):
            return False
Sli's avatar
Sli committed
53
54
55
56
57
        for group in self.vote_groups.all():
            if user.is_in_group(group):
                return True
        return False

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

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

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

Sli's avatar
Sli committed
71

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

Sli's avatar
Sli committed
81
    def results(self, total_vote):
Sli's avatar
Sli committed
82
        results = {}
Sli's avatar
Sli committed
83
        total_vote *= self.max_choice
Sli's avatar
Sli committed
84
85
86
        if total_vote == 0:
            return None
        non_blank = 0
Sli's avatar
Sli committed
87
        for candidature in self.candidatures.all():
Sli's avatar
Sli committed
88
            cand_results = {}
Sli's avatar
Sli committed
89
            cand_results['vote'] = self.votes.filter(candidature=candidature).count()
Sli's avatar
Sli committed
90
91
92
93
94
95
96
97
            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
98
99
100
101
    def __str__(self):
        return ("%s : %s") % (self.election.title, self.title)


Sli's avatar
Sli committed
102
class ElectionList(models.Model):
Sli's avatar
Sli committed
103
104
105
106
    """
    To allow per list vote
    """
    title = models.CharField(_('title'), max_length=255)
Sli's avatar
Sli committed
107
    election = models.ForeignKey(Election, related_name='election_lists', verbose_name=_("election"))
Sli's avatar
Sli committed
108

109
110
111
    def __str__(self):
        return self.title

Sli's avatar
Sli committed
112

Sli's avatar
Sli committed
113
class Candidature(models.Model):
Sli's avatar
Sli committed
114
    """
Sli's avatar
Sli committed
115
    This class is a component of responsability
Sli's avatar
Sli committed
116
    """
Sli's avatar
Sli committed
117
118
    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
119
    program = models.TextField(_('description'), null=True, blank=True)
Sli's avatar
Sli committed
120
    election_list = models.ForeignKey(ElectionList, related_name='candidatures', verbose_name=_('election_list'))
Sli's avatar
Sli committed
121

122
123
124
    def __str__(self):
        return "%s : %s" % (self.role.title, self.user.username)

Sli's avatar
Sli committed
125
126
127
128
129

class Vote(models.Model):
    """
    This class allows to vote for candidates
    """
Sli's avatar
Sli committed
130
131
    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
132
133
134

    def __str__(self):
        return "Vote"