models.py 3.33 KB
Newer Older
luxcem's avatar
luxcem committed
1
# coding: utf-8
luxcem's avatar
luxcem committed
2 3
from django.db import models

4
from representatives.models import Chamber, Representative, TimeStampedModel
5

luxcem's avatar
luxcem committed
6

7 8 9 10 11
class DossierManager(models.Manager):
    def get_by_natural_key(self, reference):
        return self.get(reference=reference)


Nicolas Joyard's avatar
Nicolas Joyard committed
12
class Dossier(TimeStampedModel):
13 14
    objects = DossierManager()

15
    title = models.CharField(max_length=1000)
16
    reference = models.CharField(max_length=200, unique=True)
Arnaud Fabre's avatar
Arnaud Fabre committed
17
    text = models.TextField(blank=True, default='')
luxcem's avatar
luxcem committed
18

Nicolas Joyard's avatar
Nicolas Joyard committed
19 20
    class Meta:
        unique_together = (('title', 'reference'))
21

22 23 24
    def __unicode__(self):
        return unicode(self.title)

25 26 27 28 29 30 31 32 33 34
    def chambers(self):
        """
        Return distinct chambers. You probably want to prefetch
        documents__chamber before calling that.
        """

        # Use sorted() because using order_by will hit the database no matter
        # what was prefetched
        return set(sorted([d.chamber for d in self.documents.all()]))

35 36 37
    def natural_key(self):
        return (self.reference,)

luxcem's avatar
luxcem committed
38

39 40
class Document(TimeStampedModel):
    dossier = models.ForeignKey(Dossier, related_name='documents')
41
    chamber = models.ForeignKey(Chamber, related_name='documents')
42 43 44 45 46
    title = models.CharField(max_length=1000)
    kind = models.CharField(max_length=255, blank=True, default='')
    link = models.URLField(max_length=1000)


47
class ProposalManager(models.Manager):
Nicolas Joyard's avatar
Nicolas Joyard committed
48
    def get_by_natural_key(self, title, dossier_nk):
49
        dossier = Dossier.objects.get_by_natural_key(dossier_nk)
Nicolas Joyard's avatar
Nicolas Joyard committed
50
        return self.get(title=title, dossier=dossier)
51 52


Nicolas Joyard's avatar
Nicolas Joyard committed
53
class Proposal(TimeStampedModel):
54 55
    objects = ProposalManager()

Arnaud Fabre's avatar
Arnaud Fabre committed
56
    dossier = models.ForeignKey(Dossier, related_name='proposals')
57
    title = models.CharField(max_length=1000, unique=True)
Arnaud Fabre's avatar
Arnaud Fabre committed
58 59
    description = models.TextField(blank=True, default='')
    reference = models.CharField(max_length=200, blank=True, null=True)
Jamesie Pic's avatar
Jamesie Pic committed
60
    datetime = models.DateTimeField(db_index=True)
Arnaud Fabre's avatar
Arnaud Fabre committed
61
    kind = models.CharField(max_length=200, blank=True, default='')
62 63 64
    total_abstain = models.IntegerField()
    total_against = models.IntegerField()
    total_for = models.IntegerField()
luxcem's avatar
luxcem committed
65

66 67 68
    representatives = models.ManyToManyField(
        Representative, through='Vote', related_name='proposals'
    )
69

70 71
    class Meta:
        ordering = ['datetime']
Nicolas Joyard's avatar
Nicolas Joyard committed
72 73 74
        unique_together = (('dossier', 'title', 'reference',
                            'kind', 'total_abstain', 'total_against',
                            'total_for'))
75

76 77 78 79 80 81
    @property
    def status(self):
        if self.total_for > self.total_against:
            return 'adopted'
        else:
            return 'rejected'
82

83 84
    def __unicode__(self):
        return unicode(self.title)
Arnaud Fabre's avatar
Arnaud Fabre committed
85

86
    def natural_key(self):
Nicolas Joyard's avatar
Nicolas Joyard committed
87
        return (self.title,) + self.dossier.natural_key()
88

luxcem's avatar
luxcem committed
89 90

class Vote(models.Model):
luxcem's avatar
luxcem committed
91 92 93 94 95 96
    VOTECHOICES = (
        ('abstain', 'abstain'),
        ('for', 'for'),
        ('against', 'against')
    )

Arnaud Fabre's avatar
Arnaud Fabre committed
97
    proposal = models.ForeignKey(Proposal, related_name='votes')
98 99 100

    representative = models.ForeignKey(
        Representative, related_name='votes', null=True)
101 102
    # Save representative name in case of we don't find the representative
    representative_name = models.CharField(max_length=200, blank=True)
luxcem's avatar
luxcem committed
103

luxcem's avatar
luxcem committed
104
    position = models.CharField(max_length=10, choices=VOTECHOICES)
105 106 107

    class Meta:
        ordering = ['proposal__datetime']
108
        unique_together = (('proposal', 'representative'))