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

Arnaud Fabre's avatar
Arnaud Fabre committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# This file is part of memopol.
#
# memopol is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or any later version.
#
# memopol is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU General Affero Public
# License along with django-representatives.
# If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2015 Arnaud Fabre <af@laquadrature.net>
luxcem's avatar
luxcem committed
20

Arnaud Fabre's avatar
Arnaud Fabre committed
21
from datetime import datetime
luxcem's avatar
luxcem committed
22

Arnaud Fabre's avatar
Arnaud Fabre committed
23 24
from django.db import models
from django.core.exceptions import ObjectDoesNotExist
Arnaud Fabre's avatar
Arnaud Fabre committed
25 26
from django.db.models.signals import post_save
from django.dispatch import receiver
Arnaud Fabre's avatar
Arnaud Fabre committed
27
# from django.utils.functional import cached_property
luxcem's avatar
luxcem committed
28

Arnaud Fabre's avatar
Arnaud Fabre committed
29
from representatives.models import Representative, Mandate, Country
Arnaud Fabre's avatar
Arnaud Fabre committed
30
from votes.models import MemopolVote
Arnaud Fabre's avatar
Arnaud Fabre committed
31
from core.utils import create_child_instance_from_parent
luxcem's avatar
luxcem committed
32

Arnaud Fabre's avatar
Arnaud Fabre committed
33

34
class MemopolRepresentative(Representative):
Arnaud Fabre's avatar
Arnaud Fabre committed
35 36
    country = models.ForeignKey(Country, null=True)
    score = models.IntegerField(default=0)
37
    main_mandate = models.ForeignKey(Mandate, null=True, default=None)
Arnaud Fabre's avatar
Arnaud Fabre committed
38

Arnaud Fabre's avatar
Arnaud Fabre committed
39 40
    def update_score(self):
        score = 0
Arnaud Fabre's avatar
Arnaud Fabre committed
41
        for vote in self.votes.all():
Arnaud Fabre's avatar
Arnaud Fabre committed
42
            score += vote.absolute_score
Arnaud Fabre's avatar
Arnaud Fabre committed
43

Arnaud Fabre's avatar
Arnaud Fabre committed
44
        self.score = score
luxcem's avatar
luxcem committed
45 46 47
        self.save()

    def update_country(self):
Arnaud Fabre's avatar
Arnaud Fabre committed
48
        # Create a country if it does'nt exist
luxcem's avatar
luxcem committed
49 50
        # The representative's country is the one associated
        # with the last 'country' mandate
Arnaud Fabre's avatar
Arnaud Fabre committed
51 52 53 54 55 56 57 58 59 60 61 62
        try:
            country_mandate = self.mandates.filter(
                group__kind='country'
            ).order_by('-begin_date')[0:1].get()

            country, _ = Country.objects.get_or_create(
                name=country_mandate.group.name,
                code=country_mandate.group.abbreviation
            )
            self.country = country
        except ObjectDoesNotExist:
            self.country = None
Arnaud Fabre's avatar
Arnaud Fabre committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
        self.save()

    def update_main_mandate(self):
        try:
            self.main_mandate = self.mandates.get(
                end_date__gte=datetime.now(),
                group__kind='group'
            )
        except Mandate.DoesNotExist:
            self.main_mandate = None
        self.save()

    def update_all(self):
        self.update_country()
        self.update_score()
        self.update_main_mandate()
Arnaud Fabre's avatar
Arnaud Fabre committed
79 80 81 82

    def active_mandates(self):
        return self.mandates.filter(
            end_date__gte=datetime.now()
luxcem's avatar
luxcem committed
83 84
        )

Arnaud Fabre's avatar
Arnaud Fabre committed
85 86 87 88
    def former_mandates(self):
        return self.mandates.filter(
            end_date__lte=datetime.now()
        )
Arnaud Fabre's avatar
Arnaud Fabre committed
89 90

    def votes_with_proposal(self):
Arnaud Fabre's avatar
Arnaud Fabre committed
91
        return MemopolVote.objects.select_related(
Arnaud Fabre's avatar
Arnaud Fabre committed
92 93
            'proposal',
            'proposal__recommendation'
Arnaud Fabre's avatar
Arnaud Fabre committed
94
        ).filter(representative=self)
luxcem's avatar
luxcem committed
95

Arnaud Fabre's avatar
Arnaud Fabre committed
96

Arnaud Fabre's avatar
Arnaud Fabre committed
97 98
@receiver(post_save, sender=Representative)
def create_memopolrepresentative_from_representative(instance, **kwargs):
Arnaud Fabre's avatar
Arnaud Fabre committed
99
    memopol_representative = create_child_instance_from_parent(MemopolRepresentative, instance)
100
    memopol_representative.save()