models.py 3.77 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
Jamesie Pic's avatar
Jamesie Pic committed
30 31
from representatives.management.commands import (
        parltrack_import_representatives,)
Arnaud Fabre's avatar
Arnaud Fabre committed
32
from votes.models import MemopolVote
Arnaud Fabre's avatar
Arnaud Fabre committed
33
from core.utils import create_child_instance_from_parent
luxcem's avatar
luxcem committed
34

Arnaud Fabre's avatar
Arnaud Fabre committed
35

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

Arnaud Fabre's avatar
Arnaud Fabre committed
41 42
    def update_score(self):
        score = 0
Jamesie Pic's avatar
Jamesie Pic committed
43
        for vote in MemopolVote.objects.filter(representative=self):
Arnaud Fabre's avatar
Arnaud Fabre committed
44
            score += vote.absolute_score
Arnaud Fabre's avatar
Arnaud Fabre committed
45

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

Arnaud Fabre's avatar
Arnaud Fabre committed
49 50
    def update_all(self):
        self.update_score()
Arnaud Fabre's avatar
Arnaud Fabre committed
51 52 53 54

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

Arnaud Fabre's avatar
Arnaud Fabre committed
57 58 59 60
    def former_mandates(self):
        return self.mandates.filter(
            end_date__lte=datetime.now()
        )
Arnaud Fabre's avatar
Arnaud Fabre committed
61 62

    def votes_with_proposal(self):
Arnaud Fabre's avatar
Arnaud Fabre committed
63
        return MemopolVote.objects.select_related(
Arnaud Fabre's avatar
Arnaud Fabre committed
64 65
            'proposal',
            'proposal__recommendation'
Arnaud Fabre's avatar
Arnaud Fabre committed
66
        ).filter(representative=self)
luxcem's avatar
luxcem committed
67

Arnaud Fabre's avatar
Arnaud Fabre committed
68

Jamesie Pic's avatar
Jamesie Pic committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
def parltrack_representative_post_save(sender, representative, data, **kwargs):
    update = False
    try:
        memopol_representative = MemopolRepresentative.objects.get(
                representative_ptr=representative)
    except MemopolRepresentative.DoesNotExist:
        memopol_representative = MemopolRepresentative(
                representative_ptr=representative)

        # Please forgive the horror your are about to witness, but this is
        # really necessary. Django wants to update the parent model when we
        # save a child model.
        memopol_representative.__dict__.update(representative.__dict__)

    try:
        country = sorted(data.get('Constituencies', []),
                key=lambda c: c.get('end') if c is not None else 1
                )[-1]['country']
    except IndexError:
        pass
    else:
        if sender.cache.get('countries', None) is None:
            sender.cache['countries'] = {c.name: c.pk for c in
                    Country.objects.all()}
        country_id = sender.cache['countries'].get(country)

        if memopol_representative.country_id != country_id:
            memopol_representative.country_id = country_id
            update = True

    if sender.mep_cache['groups']:
        main_mandate = sorted(sender.mep_cache['groups'],
                key=lambda m: m.end_date)[-1]

        if  memopol_representative.main_mandate_id != main_mandate.pk:
            memopol_representative.main_mandate_id = main_mandate.pk
            update = True

    if update:
        memopol_representative.save()
parltrack_import_representatives.ParltrackImporter.representative_post_save.connect(parltrack_representative_post_save)