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

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


10
11
12
13
14
15
16
17
18
19
20
21
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'


22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
36
37
38
39
40
class RepresentativeScore(models.Model):
    representative = models.OneToOneField('representatives.representative',
        primary_key=True, related_name='score')
    score = models.IntegerField(default=0)

41
42
43
44
    class Meta:
        managed = False
        db_table = 'representatives_recommendations_representativescore'

Jamesie Pic's avatar
Jamesie Pic committed
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

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)