0003_votescore.py 2.06 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('representatives_recommendations', '0002_dossierscore'),
    ]

    operations = [
        migrations.CreateModel(
            name='VoteScore',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('position', models.CharField(max_length=10)),
                ('score', models.IntegerField(default=0)),
            ],
            options={
                'ordering': ['proposal__datetime'],
                'db_table': 'representatives_recommendations_votescores',
                'managed': False,
            },
        ),
        migrations.DeleteModel(
            name='ScoredVote',
        ),
        migrations.RunSQL(
            """
            CREATE VIEW "representatives_recommendations_votescores"
            AS SELECT
                "representatives_votes_vote"."id",
                "representatives_votes_vote"."position",
                "representatives_votes_vote"."proposal_id",
                "representatives_votes_vote"."representative_id",
                CASE WHEN "representatives_votes_vote"."position" = ("representatives_recommendations_recommendation"."recommendation")
                    THEN "representatives_recommendations_recommendation"."weight"
                    ELSE (0 - "representatives_recommendations_recommendation"."weight")
                END AS "score"
            FROM "representatives_votes_vote"
            INNER JOIN "representatives_votes_proposal"
                ON ( "representatives_votes_vote"."proposal_id" = "representatives_votes_proposal"."id" )
            LEFT OUTER JOIN "representatives_recommendations_recommendation"
                ON ( "representatives_votes_proposal"."id" = "representatives_recommendations_recommendation"."proposal_id" )
            WHERE "representatives_recommendations_recommendation"."id" IS NOT NULL
            """
        )
    ]