models.py 5.15 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

Sli's avatar
Sli committed
44
45
46
47
    @property
    def is_vote_editable(self):
        return bool(timezone.now() <= self.end_candidature)

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

Sli's avatar
Sli committed
62
63
64
    def has_voted(self, user):
        return self.voters.filter(id=user.id).exists()

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

Sli's avatar
Sli committed
73
    # Permissions
Sli's avatar
Sli committed
74

Sli's avatar
Sli committed
75

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

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

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


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

117
118
119
    def __str__(self):
        return self.title

Sli's avatar
Sli committed
120

Sli's avatar
Sli committed
121
class Candidature(models.Model):
Sli's avatar
Sli committed
122
    """
Sli's avatar
Sli committed
123
    This class is a component of responsability
Sli's avatar
Sli committed
124
    """
Sli's avatar
Sli committed
125
126
    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
127
    program = models.TextField(_('description'), null=True, blank=True)
Sli's avatar
Sli committed
128
    election_list = models.ForeignKey(ElectionList, related_name='candidatures', verbose_name=_('election list'))
Sli's avatar
Sli committed
129

Sli's avatar
Sli committed
130
    def can_be_edited_by(self, user):
131
        return (user == self.user) or user.can_edit(self.role.election)
Sli's avatar
Sli committed
132

133
134
135
    def __str__(self):
        return "%s : %s" % (self.role.title, self.user.username)

Sli's avatar
Sli committed
136
137
138
139
140

class Vote(models.Model):
    """
    This class allows to vote for candidates
    """
Sli's avatar
Sli committed
141
142
    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
143
144
145

    def __str__(self):
        return "Vote"