models.py 2.37 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
from representatives.models import Representative
Arnaud Fabre's avatar
Arnaud Fabre committed
27 28
from representatives_votes.models import Vote, Proposal, Dossier
from legislature.models import MemopolRepresentative
Arnaud Fabre's avatar
Arnaud Fabre committed
29 30
from core.utils import create_child_instance_from_parent

Arnaud Fabre's avatar
Arnaud Fabre committed
31

Arnaud Fabre's avatar
Arnaud Fabre committed
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
    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)

55 56
    class Meta:
        ordering = ['proposal__datetime']
Arnaud Fabre's avatar
Arnaud Fabre committed
57 58


Arnaud Fabre's avatar
Arnaud Fabre committed
59
class MemopolDossier(Dossier):
Arnaud Fabre's avatar
Arnaud Fabre committed
60 61 62 63 64 65
    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:
66
            self.name = self.dossier_ptr.title
Arnaud Fabre's avatar
Arnaud Fabre committed
67 68
        return super(MemopolDossier, self).save(*args, **kwargs)

Arnaud Fabre's avatar
Arnaud Fabre committed
69 70
    def __unicode__(self):
        return self.name
Arnaud Fabre's avatar
Arnaud Fabre committed
71 72 73 74

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