models.py 5.79 KB
Newer Older
luxcem's avatar
luxcem committed
1 2
# coding: utf-8

Jamesie Pic's avatar
Jamesie Pic committed
3
from datetime import datetime
luxcem's avatar
test  
luxcem committed
4

Yohan Boniface's avatar
Yohan Boniface committed
5
from django.db import models
Nicolas Joyard's avatar
Nicolas Joyard committed
6
from django.utils.encoding import smart_unicode
7
from django.utils.functional import cached_property
luxcem's avatar
test  
luxcem committed
8

luxcem's avatar
luxcem committed
9 10 11 12 13 14

class TimeStampedModel(models.Model):
    """
    An abstract base class model that provides self-updating
    ``created`` and ``modified`` fields.
    """
15

luxcem's avatar
luxcem committed
16 17
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
18

luxcem's avatar
luxcem committed
19 20 21 22
    class Meta:
        abstract = True


Yohan Boniface's avatar
Yohan Boniface committed
23 24
class Country(models.Model):
    name = models.CharField(max_length=255)
25
    code = models.CharField(max_length=2, unique=True)
Yohan Boniface's avatar
Yohan Boniface committed
26

Bram's avatar
Bram committed
27
    def __unicode__(self):
luxcem's avatar
test  
luxcem committed
28
        return u'{} [{}]'.format(self.name, self.code)
Bram's avatar
Bram committed
29

Yohan Boniface's avatar
Yohan Boniface committed
30

Nicolas Joyard's avatar
Nicolas Joyard committed
31
class Representative(TimeStampedModel):
luxcem's avatar
test  
luxcem committed
32 33 34
    """
    Base model for representatives
    """
35

36
    slug = models.SlugField(max_length=100, unique=True)
luxcem's avatar
luxcem committed
37 38
    first_name = models.CharField(max_length=255, blank=True, default='')
    last_name = models.CharField(max_length=255, blank=True, default='')
Yohan Boniface's avatar
Yohan Boniface committed
39
    full_name = models.CharField(max_length=255)
luxcem's avatar
test  
luxcem committed
40 41 42 43 44
    GENDER = (
        (0, "N/A"),
        (1, "F"),
        (2, "M"),
    )
45
    gender = models.SmallIntegerField(choices=GENDER, default=0)
luxcem's avatar
luxcem committed
46
    birth_place = models.CharField(max_length=255, blank=True, default='')
Yohan Boniface's avatar
Yohan Boniface committed
47
    birth_date = models.DateField(blank=True, null=True)
luxcem's avatar
luxcem committed
48
    cv = models.TextField(blank=True, default='')
luxcem's avatar
luxcem committed
49
    photo = models.CharField(max_length=512, null=True)
50 51
    active = models.BooleanField(default=False)

luxcem's avatar
luxcem committed
52
    def __unicode__(self):
53
        return smart_unicode(self.full_name)
luxcem's avatar
test  
luxcem committed
54

luxcem's avatar
luxcem committed
55 56 57
    def gender_as_str(self):
        genders = {0: 'N/A', 1: 'F', 2: 'M'}
        return genders[self.gender]
luxcem's avatar
luxcem committed
58

59 60
    class Meta:
        ordering = ['last_name', 'first_name']
61

62
# Contact related models
63 64


luxcem's avatar
luxcem committed
65
class Contact(TimeStampedModel):
66
    representative = models.ForeignKey(Representative)
Yohan Boniface's avatar
Yohan Boniface committed
67 68 69 70 71 72 73

    class Meta:
        abstract = True


class Email(Contact):
    email = models.EmailField()
luxcem's avatar
luxcem committed
74 75
    kind = models.CharField(max_length=255, blank=True, default='')

Yohan Boniface's avatar
Yohan Boniface committed
76 77

class WebSite(Contact):
luxcem's avatar
luxcem committed
78 79
    url = models.CharField(max_length=2048, blank=True, default='')
    kind = models.CharField(max_length=255, blank=True, default='')
Yohan Boniface's avatar
Yohan Boniface committed
80 81 82 83


class Address(Contact):
    country = models.ForeignKey(Country)
luxcem's avatar
luxcem committed
84 85 86 87 88 89 90 91
    city = models.CharField(max_length=255, blank=True, default='')
    street = models.CharField(max_length=255, blank=True, default='')
    number = models.CharField(max_length=255, blank=True, default='')
    postcode = models.CharField(max_length=255, blank=True, default='')
    floor = models.CharField(max_length=255, blank=True, default='')
    office_number = models.CharField(max_length=255, blank=True, default='')
    kind = models.CharField(max_length=255, blank=True, default='')
    name = models.CharField(max_length=255, blank=True, default='')
92
    location = models.CharField(max_length=255, blank=True, default='')
luxcem's avatar
luxcem committed
93

Yohan Boniface's avatar
Yohan Boniface committed
94 95

class Phone(Contact):
luxcem's avatar
luxcem committed
96 97
    number = models.CharField(max_length=255, blank=True, default='')
    kind = models.CharField(max_length=255, blank=True, default='')
luxcem's avatar
luxcem committed
98
    address = models.ForeignKey(Address, null=True, related_name='phones')
99

Yohan Boniface's avatar
Yohan Boniface committed
100

Nicolas Joyard's avatar
Nicolas Joyard committed
101
class Chamber(models.Model):
Nicolas Joyard's avatar
Nicolas Joyard committed
102 103 104 105 106
    """
    A representative chamber
    """
    name = models.CharField(max_length=255)
    country = models.ForeignKey('Country', null=True, related_name='chambers')
107 108
    abbreviation = models.CharField(max_length=10, blank=True, default='',
        db_index=True)
Nicolas Joyard's avatar
Nicolas Joyard committed
109

110 111
    def __unicode__(self):
        return u'{} [{}]'.format(self.name, self.abbreviation)
Nicolas Joyard's avatar
Nicolas Joyard committed
112 113


Nicolas Joyard's avatar
Nicolas Joyard committed
114
class Group(TimeStampedModel):
115
    """
luxcem's avatar
luxcem committed
116
    An entity represented by a representative through a mandate
117
    """
Nicolas Joyard's avatar
Nicolas Joyard committed
118
    name = models.CharField(max_length=511, db_index=True)
119 120 121
    abbreviation = models.CharField(max_length=10, blank=True, default='',
        db_index=True)
    kind = models.CharField(max_length=255, db_index=True)
Nicolas Joyard's avatar
Nicolas Joyard committed
122
    chamber = models.ForeignKey(Chamber, null=True, related_name='groups')
123

luxcem's avatar
test  
luxcem committed
124 125 126 127
    @cached_property
    def active(self):
        return self.mandates.filter(end_date__gte=datetime.now()).exists()

128 129 130
    def __unicode__(self):
        return unicode(self.name)

131 132 133
    class Meta:
        ordering = ('name',)

134

Nicolas Joyard's avatar
Nicolas Joyard committed
135
class Constituency(TimeStampedModel):
136
    """
luxcem's avatar
luxcem committed
137
    An authority for which a representative has a mandate
138 139
    """
    name = models.CharField(max_length=255)
Jamesie Pic's avatar
Jamesie Pic committed
140 141
    country = models.ForeignKey('Country', null=True, blank=True,
        related_name='constituencies')
luxcem's avatar
luxcem committed
142

luxcem's avatar
test  
luxcem committed
143 144 145
    @cached_property
    def active(self):
        return self.mandates.filter(end_date__gte=datetime.now()).exists()
146 147

    def __unicode__(self):
luxcem's avatar
test  
luxcem committed
148
        return unicode(self.name)
149 150


Arnaud Fabre's avatar
Arnaud Fabre committed
151
class MandateManager(models.Manager):
Jamesie Pic's avatar
Jamesie Pic committed
152
    """ This satisfies repr(Mandate) """
Arnaud Fabre's avatar
Arnaud Fabre committed
153
    def get_queryset(self):
154 155 156 157 158 159 160
        return super(
            MandateManager,
            self).get_queryset().select_related(
            'group',
            'constituency')


Nicolas Joyard's avatar
Nicolas Joyard committed
161
class Mandate(TimeStampedModel):
Arnaud Fabre's avatar
Arnaud Fabre committed
162 163

    objects = MandateManager()
164

luxcem's avatar
luxcem committed
165
    group = models.ForeignKey(Group, null=True, related_name='mandates')
166 167
    constituency = models.ForeignKey(
        Constituency, null=True, related_name='mandates')
luxcem's avatar
luxcem committed
168
    representative = models.ForeignKey(Representative, related_name='mandates')
Yohan Boniface's avatar
Yohan Boniface committed
169
    role = models.CharField(
170
        max_length=255,
Yohan Boniface's avatar
Yohan Boniface committed
171
        blank=True,
luxcem's avatar
luxcem committed
172
        default='',
173
        help_text="Eg.: president of a political group"
Yohan Boniface's avatar
Yohan Boniface committed
174 175 176
    )
    begin_date = models.DateField(blank=True, null=True)
    end_date = models.DateField(blank=True, null=True)
luxcem's avatar
luxcem committed
177 178
    link = models.URLField()

luxcem's avatar
test  
luxcem committed
179 180 181 182 183
    @property
    def active(self):
        return self.end_date >= datetime.now().date()

    def __unicode__(self):
184 185
        t = u'Mandate : {representative},{role} {group} for {constituency}'
        return t.format(
luxcem's avatar
test  
luxcem committed
186
            representative=self.representative,
187 188 189
            role=(
                u' {} of'.format(
                    self.role) if self.role else u''),
luxcem's avatar
test  
luxcem committed
190
            constituency=self.constituency,
191
            group=self.group)
Jamesie Pic's avatar
Jamesie Pic committed
192 193 194

    class Meta:
        ordering = ('-end_date',)