models.py 1.54 KB
Newer Older
luxcem's avatar
luxcem committed
1
from django.db import models
luxcem's avatar
luxcem committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
import representatives
import datetime


class Representative(representatives.models.Representative):
    active = models.BooleanField(default=False)
    country = models.ForeignKey(representatives.models.Country, null=True)

    def active_mandates(self):
        return self.mandate_set.filter(active=True)

    def former_mandates(self):
        return self.mandate_set.filter(active=False)

    def current_group(self):
        return self.mandate_set.get(
            active=True,
            group__kind='group'
        )

    def update_active(self):
        # If a representative has at least one active manadate
        self.active = False
        for mandate in self.mandate_set.all():
            if mandate.active:
                self.active = True
                continue

        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
        country_mandate = self.mandate_set.filter(
            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()


class Mandate(representatives.models.Mandate):

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