models.py 4.28 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
27
from django.db.models.signals import post_save
from django.dispatch import receiver
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 representatives_votes.models import Vote
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
35
36
37
38
39
40
41
42
43
44
45
46
47

class MemopolRepresentative(models.Model):

    # We should link a memopol representative to a representative based
    # on the remote_id attribute
    parent_identifier = 'remote_id'
    child_parent_identifier = 'representative_remote_id'
    
    representative = models.OneToOneField(
        Representative,
        parent_link=True,
        related_name='extra',
        null=True,
        on_delete=models.SET_NULL
    )
Arnaud Fabre's avatar
Arnaud Fabre committed
48
49
50
51
52
53
54
    
    representative_remote_id = models.CharField(max_length=255, unique=True)
    country = models.ForeignKey(Country, null=True)
    score = models.IntegerField(default=0)
    
    def update_score(self):
        score = 0
Arnaud Fabre's avatar
Arnaud Fabre committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
        for vote in self.votes.all():
            proposal = vote.proposal
            try:
                if proposal.recommendation:
                    recommendation = proposal.recommendation
                    if ( vote.position != recommendation.recommendation
                         and (
                             vote.position == 'abstain' or
                             recommendation.recommendation == 'abstain' )):
                        score -= (recommendation.weight / 2)
                    elif vote.position != recommendation.recommendation:
                        score -= recommendation.weight
                    else:
                        score += recommendation.weight
            except Exception:
                pass

Arnaud Fabre's avatar
Arnaud Fabre committed
72
        self.score = score
luxcem's avatar
luxcem committed
73
74
75
        self.save()

    def update_country(self):
Arnaud Fabre's avatar
Arnaud Fabre committed
76
        # Create a country if it does'nt exist
luxcem's avatar
luxcem committed
77
78
        # The representative's country is the one associated
        # with the last 'country' mandate
Arnaud Fabre's avatar
Arnaud Fabre committed
79
80
81
82
83
84
85
86
87
88
89
        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
            self.save()
luxcem's avatar
luxcem committed
90

Arnaud Fabre's avatar
Arnaud Fabre committed
91
92
93
94
95
        except ObjectDoesNotExist:
            self.country = None
            self.save()


Arnaud Fabre's avatar
Arnaud Fabre committed
96
97
98
99
100
    @cached_property
    def votes(self):
        return Vote.objects.filter(
            representative_remote_id = self.remote_id
        )
Arnaud Fabre's avatar
Arnaud Fabre committed
101
102
103
104

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

Arnaud Fabre's avatar
Arnaud Fabre committed
107
108
109
110
111
112
113
114
115
116
    def former_mandates(self):
        return self.mandates.filter(
            end_date__lte=datetime.now()
        )
    
    def current_group_mandate(self):
        return self.mandates.get(
            end_date__gte=datetime.now(),
            group__kind='group'
        )
luxcem's avatar
luxcem committed
117

Arnaud Fabre's avatar
Arnaud Fabre committed
118
119
120
121
122
123
124
125
126
127
128
@receiver(post_save, sender=Representative)
def create_memopolrepresentative_from_representative(instance, **kwargs):
    # create_child_instance_from_parent(MemopolRepresentative, instance)
    pass


@receiver(post_save, sender=Mandate)
def update_memopolrepresentative_country(instance, created, **kwargs):
    return
    if not created:
        return
129

Arnaud Fabre's avatar
Arnaud Fabre committed
130
131
132
    # Update representative country
    if instance.group.kind == 'country' and instance.representative.extra.country == None:
        instance.representative.extra.update_country()
133