models.py 3.55 KB
Newer Older
Arnaud Fabre's avatar
Arnaud Fabre committed
1
# coding: utf-8
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>
20

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

Arnaud Fabre's avatar
Arnaud Fabre committed
23
from django.db import models
24

Jamesie Pic's avatar
Jamesie Pic committed
25 26 27
from representatives.contrib.parltrack.import_representatives import \
    representative_post_save
from representatives.models import Country, Mandate, Representative
Arnaud Fabre's avatar
Arnaud Fabre committed
28
from votes.models import MemopolVote
Jamesie Pic's avatar
Jamesie Pic committed
29 30 31


# from django.utils.functional import cached_property
32

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
Jamesie Pic's avatar
Jamesie Pic committed
41
        for vote in MemopolVote.objects.filter(representative=self):
Arnaud Fabre's avatar
Arnaud Fabre committed
42
            score += vote.absolute_score
43

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

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

    def active_mandates(self):
        return self.mandates.filter(
            end_date__gte=datetime.now()
53 54
        )

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

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

Arnaud Fabre's avatar
Arnaud Fabre committed
66

Jamesie Pic's avatar
Jamesie Pic committed
67
def mempol_representative(sender, representative, data, **kwargs):
Jamesie Pic's avatar
Jamesie Pic committed
68 69 70
    update = False
    try:
        memopol_representative = MemopolRepresentative.objects.get(
Jamesie Pic's avatar
Jamesie Pic committed
71
            representative_ptr=representative)
Jamesie Pic's avatar
Jamesie Pic committed
72 73
    except MemopolRepresentative.DoesNotExist:
        memopol_representative = MemopolRepresentative(
Jamesie Pic's avatar
Jamesie Pic committed
74
            representative_ptr=representative)
Jamesie Pic's avatar
Jamesie Pic committed
75 76 77 78 79 80 81 82

        # 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', []),
Jamesie Pic's avatar
Jamesie Pic committed
83 84
                         key=lambda c: c.get('end') if c is not None else 1
                         )[-1]['country']
Jamesie Pic's avatar
Jamesie Pic committed
85 86 87 88 89
    except IndexError:
        pass
    else:
        if sender.cache.get('countries', None) is None:
            sender.cache['countries'] = {c.name: c.pk for c in
Jamesie Pic's avatar
Jamesie Pic committed
90
                                         Country.objects.all()}
Jamesie Pic's avatar
Jamesie Pic committed
91 92 93 94 95 96 97 98
        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'],
Jamesie Pic's avatar
Jamesie Pic committed
99
                              key=lambda m: m.end_date)[-1]
Jamesie Pic's avatar
Jamesie Pic committed
100

Jamesie Pic's avatar
Jamesie Pic committed
101
        if memopol_representative.main_mandate_id != main_mandate.pk:
Jamesie Pic's avatar
Jamesie Pic committed
102 103 104 105 106
            memopol_representative.main_mandate_id = main_mandate.pk
            update = True

    if update:
        memopol_representative.save()
Jamesie Pic's avatar
Jamesie Pic committed
107
representative_post_save.connect(mempol_representative)