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


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
21
22
23
24
25
26
27
28
    start_date = models.DateTimeField(_('start date'), blank=False)
    end_date = models.DateTimeField(_('end date'), blank=False)

    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
29
    @property
Sli's avatar
Sli committed
30
    def is_candidature_active(self):
Sli's avatar
Sli committed
31
        now = timezone.now()
Sli's avatar
Sli committed
32
        return bool(now <= self.end_candidature and now >= self.start_candidature)
Sli's avatar
Sli committed
33

Sli's avatar
Sli committed
34
    def has_voted(self, user):
Sli's avatar
Sli committed
35
36
        return False
        # return self.has_voted.filter(id=user.id).exists()
Sli's avatar
Sli committed
37

Sli's avatar
Sli committed
38
39
40
    def get_results(self):
        pass

Sli's avatar
Sli committed
41
42
43
44
45
46
    def can_view(self, obj):
        return True

    def can_be_viewed_by(self, user):
        return True

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
57
58
59
60

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


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


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


class Vote(models.Model):
    """
    This class allows to vote for candidates
    """
Sli's avatar
Sli committed
83
84
    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
85
86
87

    def __str__(self):
        return "Vote"