models.py 2.86 KB
Newer Older
Arnaud Fabre's avatar
Arnaud Fabre committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# coding: utf-8

# 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>

from django.db import models
Arnaud Fabre's avatar
Arnaud Fabre committed
22 23 24
from django.utils.functional import cached_property
from django.db.models.signals import post_save
from django.dispatch import receiver
Arnaud Fabre's avatar
Arnaud Fabre committed
25 26 27

from representatives_votes.models import Vote, Proposal, Dossier
from legislature.models import MemopolRepresentative
Arnaud Fabre's avatar
Arnaud Fabre committed
28 29 30
from core.utils import create_child_instance_from_parent
from .tasks import update_representatives_score_for_proposal

Arnaud Fabre's avatar
Arnaud Fabre committed
31 32 33 34 35 36 37 38 39 40 41 42 43

class Recommendation(models.Model):
    SCORE_TABLE = {
        ('abstain', 'abstain'): 1,
        ('abstain', 'for'): -0.5,
        ('abstain', 'against'): -0.5,
    }

    VOTECHOICES = (
        ('abstain', 'abstain'),
        ('for', 'for'),
        ('against', 'against')
    )
Arnaud Fabre's avatar
Arnaud Fabre committed
44

Arnaud Fabre's avatar
Arnaud Fabre committed
45 46 47 48
    proposal = models.OneToOneField(
        Proposal,
        related_name='recommendation'
    )
Arnaud Fabre's avatar
Arnaud Fabre committed
49

Arnaud Fabre's avatar
Arnaud Fabre committed
50 51 52 53 54 55
    recommendation = models.CharField(max_length=10, choices=VOTECHOICES)
    title = models.CharField(max_length=1000, blank=True)
    description = models.TextField(blank=True)
    weight = models.IntegerField(default=0)


Arnaud Fabre's avatar
Arnaud Fabre committed
56 57 58 59 60
@receiver(post_save, sender=Recommendation)
def update_score(instance, **kwargs):
    update_representatives_score_for_proposal(instance.proposal)


Arnaud Fabre's avatar
Arnaud Fabre committed
61
class MemopolDossier(Dossier):
Arnaud Fabre's avatar
Arnaud Fabre committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    parent_identifier = 'reference'
    child_parent_identifier = 'dossier_reference'

    dossier = models.OneToOneField(
        Dossier,
        primary_key=True,
        parent_link=True,
        related_name='extra'
    )

    dossier_reference = models.CharField(max_length=200)
    name = models.CharField(max_length=1000, blank=True, default='')
    description = models.TextField(blank=True, default='')

    def save(self, *args, **kwargs):
        if not self.name:
            self.name = self.dossier.title
        return super(MemopolDossier, self).save(*args, **kwargs)


@receiver(post_save, sender=Dossier)
def create_memopolrepresentative_from_representative(instance, **kwargs):
    create_child_instance_from_parent(MemopolDossier, instance)

Arnaud Fabre's avatar
Arnaud Fabre committed
86 87 88 89 90

class MemopolVote(Vote):
    class Meta:
        proxy = True

Arnaud Fabre's avatar
Arnaud Fabre committed
91
    @cached_property
Arnaud Fabre's avatar
Arnaud Fabre committed
92 93 94 95
    def representative(self):
        return MemopolRepresentative.objects.get(
            remote_id = self.representative_remote_id
        )