models.py 3.05 KB
Newer Older
Jamesie Pic's avatar
Jamesie Pic committed
1
2
3
4
5
6
7
8
9
10
11
12
# coding: utf-8
from django.db import models
from django.db.models.signals import post_save

from representatives_votes.contrib.parltrack.import_votes import \
    vote_pre_import
from representatives.contrib.parltrack.import_representatives import \
    representative_pre_import
from representatives_votes.models import Dossier, Proposal, Vote
from representatives.models import Representative


13
14
15
16
17
18
19
20
21
22
23
24
class DossierScore(models.Model):
    id = models.CharField(max_length=255, primary_key=True)
    representative = models.ForeignKey(Representative,
        on_delete=models.DO_NOTHING)
    dossier = models.ForeignKey(Dossier, on_delete=models.DO_NOTHING)
    score = models.IntegerField(default=0)

    class Meta:
        managed = False
        db_table = 'representatives_recommendations_dossierscores'


25
26
27
28
29
30
31
32
33
34
35
36
37
38
class VoteScore(models.Model):
    proposal = models.ForeignKey(Proposal, related_name='votescores')

    representative = models.ForeignKey(
        Representative, related_name='votescores', null=True)
    position = models.CharField(max_length=10)
    score = models.IntegerField(default=0)

    class Meta:
        managed = False
        ordering = ['proposal__datetime']
        db_table = 'representatives_recommendations_votescores'


Jamesie Pic's avatar
Jamesie Pic committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
class RepresentativeScore(models.Model):
    representative = models.OneToOneField('representatives.representative',
        primary_key=True, related_name='score')
    score = models.IntegerField(default=0)


class Recommendation(models.Model):
    proposal = models.OneToOneField(
        Proposal,
        related_name='recommendation'
    )

    recommendation = models.CharField(max_length=10, choices=Vote.VOTECHOICES)
    title = models.CharField(max_length=1000, blank=True)
    description = models.TextField(blank=True)
    weight = models.IntegerField(default=0)

    class Meta:
        ordering = ['proposal__datetime']


def skip_votes(sender, vote_data=None, **kwargs):
    dossiers = getattr(sender, 'memopol_filters', None)

    if dossiers is None:
        sender.memopol_filters = dossiers = Dossier.objects.filter(
            proposals__recommendation__in=Recommendation.objects.all()
        ).values_list('reference', flat=True)

    if vote_data.get('epref', None) not in dossiers:
        return False
vote_pre_import.connect(skip_votes)


def skip_representatives(sender, representative_data=None, **kwargs):
    if not representative_data.get('active', False):
        return False
representative_pre_import.connect(skip_representatives)


def create_representative_vote_profile(sender, instance=None, created=None,
        **kwargs):

    if not created:
        return

    RepresentativeScore.objects.create(representative=instance)
post_save.connect(create_representative_vote_profile, sender=Representative)


def calculate_representative_score(representative):
    score = 0

    votes = representative.votes.exclude(
        proposal__recommendation=None
    ).select_related('proposal__recommendation')

96
    votes = VoteScore.objects.filter(pk__in=votes.values_list('pk'))
Jamesie Pic's avatar
Jamesie Pic committed
97
98

    for vote in votes:
99
        score += vote.score
Jamesie Pic's avatar
Jamesie Pic committed
100
101

    return score