models.py 2.01 KB
Newer Older
Sli's avatar
Sli committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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):
    """
    This class allow to create a new election
    """
    title = models.CharField(_('title'), max_length=255)
    description = models.TextField(_('description'), null=True, blank=True)
    start_date = models.DateTimeField(_('start date'), blank=False)
    end_date = models.DateTimeField(_('end date'), blank=False)
Sli's avatar
Sli committed
20
    electors = models.ManyToManyField(Subscriber, related_name='election', verbose_name=_("electors"), blank=True)
Sli's avatar
Sli committed
21
22
23
24
25
26
27
28
29

    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
30
31
32
    def has_voted(self, user):
        return self.electors.filter(id=user.id).exists()

Sli's avatar
Sli committed
33
34
35
36
37
38
39
    def get_results(self):
        pass


class Responsability(models.Model):
    """
    """
Sli's avatar
Sli committed
40
    election = models.ForeignKey(Election, related_name='responsability', verbose_name=_("election"))
Sli's avatar
Sli committed
41
42
43
44
45
46
47
48
49
50
51
    title = models.CharField(_('title'), max_length=255)
    description = models.TextField(_('description'), null=True, blank=True)
    blank_votes = models.IntegerField(_('blank votes'), default=0)

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


class Candidate(models.Model):
    """
    """
Sli's avatar
Sli committed
52
    responsability = models.ForeignKey(Responsability, related_name='candidate', verbose_name=_("responsability"))
Sli's avatar
Sli committed
53
54
55
56
57
58
    subscriber = models.ForeignKey(Subscriber, verbose_name=_('user'), related_name='candidate', blank=True)
    program = models.TextField(_('description'), null=True, blank=True)
    votes = models.IntegerField(_('votes'), default=0)

    def __str__(self):
        return ("%s : %s -> %s") % (self.election.title, self.title, self.subscriber.get_full_name())