models.py 2.56 KB
Newer Older
Sli's avatar
Sli committed
1
2
3
4
5
6
7
8
9
10
11
12
13
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
from subscription.models import Subscriber
from subscription.views import get_subscriber


class Election(models.Model):
    """
Sli's avatar
Sli committed
14
    This class allows to create a new election
Sli's avatar
Sli committed
15
16
17
    """
    title = models.CharField(_('title'), max_length=255)
    description = models.TextField(_('description'), null=True, blank=True)
Sli's avatar
Sli committed
18
19
    start_proposal = models.DateTimeField(_('start proposal'), blank=False)
    end_proposal = models.DateTimeField(_('end proposal'), blank=False)
Sli's avatar
Sli committed
20
21
22
23
24
25
26
27
28
29
30
    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
31
32
33
34
35
    @property
    def is_proposal_active(self):
        now = timezone.now()
        return bool(now <= self.end_proposal and now >= self.start_proposal)

Sli's avatar
Sli committed
36
    def has_voted(self, user):
Sli's avatar
Sli committed
37
        return self.vote.filter(user__id=user.id).exists()
Sli's avatar
Sli committed
38

Sli's avatar
Sli committed
39
40
41
42
43
44
    def get_results(self):
        pass


class Responsability(models.Model):
    """
Sli's avatar
Sli committed
45
    This class allows to create a new responsability
Sli's avatar
Sli committed
46
    """
Sli's avatar
Sli committed
47
    election = models.ForeignKey(Election, related_name='responsability', verbose_name=_("election"))
Sli's avatar
Sli committed
48
49
50
51
52
53
54
55
56
    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)


class Candidate(models.Model):
    """
Sli's avatar
Sli committed
57
    This class is a component of responsability
Sli's avatar
Sli committed
58
    """
Sli's avatar
Sli committed
59
    responsability = models.ForeignKey(Responsability, related_name='candidate', verbose_name=_("responsability"))
Sli's avatar
Sli committed
60
    user = models.ForeignKey(User, verbose_name=_('user'), related_name='candidate', blank=True)
Sli's avatar
Sli committed
61
62
63
    program = models.TextField(_('description'), null=True, blank=True)

    def __str__(self):
Sli's avatar
Sli committed
64
65
66
67
68
69
70
71
72
73
74
75
76
        return ("%s : %s -> %s") % (self.election.title, self.title, self.user.get_full_name())


class Vote(models.Model):
    """
    This class allows to vote for candidates
    """
    election = models.ForeignKey(Election, related_name='vote', verbose_name=_("election"))
    candidate = models.ManyToManyField(Candidate, related_name='vote', verbose_name=_("candidate"))
    user = models.ForeignKey(User, related_name='vote', verbose_name=_("user"))

    def __str__(self):
        return "Vote"