models.py 2 KB
Newer Older
luxcem's avatar
luxcem committed
1
from django.db import models
luxcem's avatar
luxcem committed
2 3 4 5
import representatives
import datetime


6
class MRepresentative(representatives.models.Representative):
Arnaud Fabre's avatar
Arnaud Fabre committed
7
    # active = models.BooleanField(default=False)
luxcem's avatar
luxcem committed
8 9 10
    country = models.ForeignKey(representatives.models.Country, null=True)

    def active_mandates(self):
11
        return self.mmandate_set.filter(active=True)
luxcem's avatar
luxcem committed
12 13

    def former_mandates(self):
14
        return self.mmandate_set.filter(active=False)
luxcem's avatar
luxcem committed
15

16 17
    def current_group_mandate(self):
        return self.mmandate_set.get(
luxcem's avatar
luxcem committed
18 19 20 21 22 23 24
            active=True,
            group__kind='group'
        )

    def update_active(self):
        # If a representative has at least one active manadate
        self.active = False
25
        for mandate in self.mmandate_set.all():
luxcem's avatar
luxcem committed
26 27
            if mandate.active:
                self.active = True
28
                break
luxcem's avatar
luxcem committed
29 30 31 32 33 34 35

        self.save()

    def update_country(self):
        # Create a country if it does not exist
        # The representative's country is the one associated
        # with the last 'country' mandate
36
        country_mandate = self.mmandate_set.filter(
luxcem's avatar
luxcem committed
37 38 39 40 41 42 43 44 45 46 47
            group__kind='country'
        ).order_by('-begin_date')[0:1].get()

        country, created = representatives.models.Country.objects.get_or_create(
            name=country_mandate.group.name,
            code=country_mandate.group.abbreviation
        )
        self.country = country
        self.save()


48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
class MGroup(representatives.models.Group):
    active = models.BooleanField(default=False)

    def update_active(self):
        self.active = False
        for mandate in self.mmandate_set.all():
            if mandate.active:
                self.active = True
                break
        self.save()


class MMandate(representatives.models.Mandate):
    active = models.BooleanField(default=False)
    mgroup = models.ForeignKey(MGroup)
    mrepresentative = models.ForeignKey(MRepresentative)
luxcem's avatar
luxcem committed
64 65 66 67 68

    def update_active(self):
        date = datetime.datetime.now().date()
        self.active = self.end_date > date
        self.save()