models.py 2.49 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
        return self.has_voted.filter(id=user.id).exists()
Sli's avatar
Sli committed
36

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


Sli's avatar
Sli committed
41
class Role(models.Model):
Sli's avatar
Sli committed
42
    """
Sli's avatar
Sli committed
43
    This class allows to create a new role avaliable for a candidature
Sli's avatar
Sli committed
44
    """
Sli's avatar
Sli committed
45
    election = models.ForeignKey(Election, related_name='role', verbose_name=_("election"))
Sli's avatar
Sli committed
46
47
48
49
50
51
52
    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
53
class Candidature(models.Model):
Sli's avatar
Sli committed
54
    """
Sli's avatar
Sli committed
55
    This class is a component of responsability
Sli's avatar
Sli committed
56
    """
Sli's avatar
Sli committed
57
    role = models.ForeignKey(Role, related_name='candidature', verbose_name=_("role"))
Sli's avatar
Sli committed
58
    user = models.ForeignKey(User, verbose_name=_('user'), related_name='candidate', blank=True)
Sli's avatar
Sli committed
59
    program = models.TextField(_('description'), null=True, blank=True)
Sli's avatar
Sli committed
60
    has_voted = models.ManyToManyField(User, verbose_name=_('has_voted'), related_name='has_voted')
Sli's avatar
Sli committed
61

Sli's avatar
Sli committed
62
63
64
65
66

class List(models.Model):
    """
    To allow per list vote
    """
Sli's avatar
Sli committed
67
    title = models.CharField(_('title'), max_length=255)
Sli's avatar
Sli committed
68
69
70
71
72
73


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

    def __str__(self):
        return "Vote"