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


Sli's avatar
Sli committed
42
class Role(models.Model):
Sli's avatar
Sli committed
43
    """
Sli's avatar
Sli committed
44
    This class allows to create a new role avaliable for a candidature
Sli's avatar
Sli committed
45
    """
Sli's avatar
Sli committed
46
    election = models.ForeignKey(Election, related_name='role', verbose_name=_("election"))
Sli's avatar
Sli committed
47
48
49
50
51
52
53
    title = models.CharField(_('title'), max_length=255)
    description = models.TextField(_('description'), null=True, blank=True)

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


Sli's avatar
Sli committed
54
55
56
57
58
59
60
61
class List(models.Model):
    """
    To allow per list vote
    """
    title = models.CharField(_('title'), max_length=255)
    election = models.ForeignKey(Election, related_name='list', verbose_name=_("election"))


Sli's avatar
Sli committed
62
class Candidature(models.Model):
Sli's avatar
Sli committed
63
    """
Sli's avatar
Sli committed
64
    This class is a component of responsability
Sli's avatar
Sli committed
65
    """
Sli's avatar
Sli committed
66
    role = models.ForeignKey(Role, related_name='candidature', verbose_name=_("role"))
Sli's avatar
Sli committed
67
    user = models.ForeignKey(User, verbose_name=_('user'), related_name='candidate', blank=True)
Sli's avatar
Sli committed
68
    program = models.TextField(_('description'), null=True, blank=True)
Sli's avatar
Sli committed
69
    has_voted = models.ManyToManyField(User, verbose_name=_('has_voted'), related_name='has_voted')
Sli's avatar
Sli committed
70
    liste = models.ForeignKey(List, related_name='candidature', verbose_name=_('list'))
Sli's avatar
Sli committed
71
72
73
74
75
76


class Vote(models.Model):
    """
    This class allows to vote for candidates
    """
Sli's avatar
Sli committed
77
78
    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
79
80
81

    def __str__(self):
        return "Vote"