Commit 1c5e2598 authored by Nicolas Joyard's avatar Nicolas Joyard
Browse files

Replace RepresentativeScore with view-based model

parent 729406a2
......@@ -10,7 +10,3 @@ bin/update_dossiers
sleep 120
bin/update_votes
sleep 120
bin/update_scores
#!/bin/bash
set -ex
source ${OPENSHIFT_REPO_DIR}bin/lib.sh
[ -n "$OPENSHIFT_REPO_DIR" ] && cd $OPENSHIFT_REPO_DIR
./manage.py update_score
......@@ -1096,13 +1096,6 @@
"model": "representatives_votes.vote",
"pk": 25535
},
{
"fields": {
"score": -7
},
"model": "representatives_recommendations.representativescore",
"pk": 160
},
{
"fields": {
"proposal": 5744,
......
......@@ -24036,258 +24036,6 @@
"model": "representatives_votes.vote",
"pk": 25535
},
{
"fields": {
"score": 15
},
"model": "representatives_recommendations.representativescore",
"pk": 717
},
{
"fields": {
"score": 15
},
"model": "representatives_recommendations.representativescore",
"pk": 718
},
{
"fields": {
"score": -10
},
"model": "representatives_recommendations.representativescore",
"pk": 439
},
{
"fields": {
"score": 15
},
"model": "representatives_recommendations.representativescore",
"pk": 748
},
{
"fields": {
"score": -15
},
"model": "representatives_recommendations.representativescore",
"pk": 744
},
{
"fields": {
"score": -15
},
"model": "representatives_recommendations.representativescore",
"pk": 681
},
{
"fields": {
"score": 15
},
"model": "representatives_recommendations.representativescore",
"pk": 684
},
{
"fields": {
"score": 15
},
"model": "representatives_recommendations.representativescore",
"pk": 719
},
{
"fields": {
"score": 15
},
"model": "representatives_recommendations.representativescore",
"pk": 692
},
{
"fields": {
"score": -15
},
"model": "representatives_recommendations.representativescore",
"pk": 736
},
{
"fields": {
"score": -15
},
"model": "representatives_recommendations.representativescore",
"pk": 678
},
{
"fields": {
"score": 15
},
"model": "representatives_recommendations.representativescore",
"pk": 738
},
{
"fields": {
"score": 10
},
"model": "representatives_recommendations.representativescore",
"pk": 708
},
{
"fields": {
"score": -15
},
"model": "representatives_recommendations.representativescore",
"pk": 699
},
{
"fields": {
"score": 15
},
"model": "representatives_recommendations.representativescore",
"pk": 743
},
{
"fields": {
"score": -15
},
"model": "representatives_recommendations.representativescore",
"pk": 646
},
{
"fields": {
"score": -15
},
"model": "representatives_recommendations.representativescore",
"pk": 679
},
{
"fields": {
"score": -15
},
"model": "representatives_recommendations.representativescore",
"pk": 705
},
{
"fields": {
"score": 15
},
"model": "representatives_recommendations.representativescore",
"pk": 655
},
{
"fields": {
"score": 15
},
"model": "representatives_recommendations.representativescore",
"pk": 746
},
{
"fields": {
"score": -7
},
"model": "representatives_recommendations.representativescore",
"pk": 160
},
{
"fields": {
"score": 15
},
"model": "representatives_recommendations.representativescore",
"pk": 642
},
{
"fields": {
"score": 15
},
"model": "representatives_recommendations.representativescore",
"pk": 645
},
{
"fields": {
"score": 15
},
"model": "representatives_recommendations.representativescore",
"pk": 650
},
{
"fields": {
"score": 15
},
"model": "representatives_recommendations.representativescore",
"pk": 670
},
{
"fields": {
"score": -15
},
"model": "representatives_recommendations.representativescore",
"pk": 747
},
{
"fields": {
"score": -15
},
"model": "representatives_recommendations.representativescore",
"pk": 644
},
{
"fields": {
"score": 15
},
"model": "representatives_recommendations.representativescore",
"pk": 750
},
{
"fields": {
"score": 5
},
"model": "representatives_recommendations.representativescore",
"pk": 282
},
{
"fields": {
"score": 3
},
"model": "representatives_recommendations.representativescore",
"pk": 647
},
{
"fields": {
"score": 15
},
"model": "representatives_recommendations.representativescore",
"pk": 651
},
{
"fields": {
"score": -15
},
"model": "representatives_recommendations.representativescore",
"pk": 653
},
{
"fields": {
"score": 15
},
"model": "representatives_recommendations.representativescore",
"pk": 661
},
{
"fields": {
"score": -15
},
"model": "representatives_recommendations.representativescore",
"pk": 666
},
{
"fields": {
"score": 15
},
"model": "representatives_recommendations.representativescore",
"pk": 697
},
{
"fields": {
"score": -7
},
"model": "representatives_recommendations.representativescore",
"pk": 614
},
{
"fields": {
"proposal": 5744,
......
from django.core.management.base import BaseCommand
from representatives_recommendations.models import (RepresentativeScore,
calculate_representative_score)
class Command(BaseCommand):
def handle(self, *args, **options):
for score in RepresentativeScore.objects.all():
score.score = calculate_representative_score(
score.representative)
score.save()
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('representatives_recommendations', '0004_dossierscore_rewrite'),
]
operations = [
migrations.AlterModelOptions(
name='representativescore',
options={
'managed': False,
'db_table': 'representatives_recommendations_representativescore',
},
),
migrations.RunSQL(
"""
DROP TABLE "representatives_recommendations_representativescore"
"""
),
migrations.RunSQL(
"""
CREATE VIEW "representatives_recommendations_representativescore"
AS SELECT
"representatives_representative"."id" as "representative_id",
COALESCE(SUM("representatives_recommendations_votescores"."score"), 0) AS "score"
FROM
"representatives_representative"
LEFT OUTER JOIN "representatives_recommendations_votescores"
ON "representatives_recommendations_votescores"."representative_id" = "representatives_representative"."id"
GROUP BY "representatives_representative"."id"
"""
)
]
# 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
......@@ -41,6 +40,10 @@ class RepresentativeScore(models.Model):
primary_key=True, related_name='score')
score = models.IntegerField(default=0)
class Meta:
managed = False
db_table = 'representatives_recommendations_representativescore'
class Recommendation(models.Model):
proposal = models.OneToOneField(
......@@ -74,28 +77,3 @@ 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')
votes = VoteScore.objects.filter(pk__in=votes.values_list('pk'))
for vote in votes:
score += vote.score
return score
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment