0002_themescore.py 3.86 KB
Newer Older
Nicolas Joyard's avatar
Nicolas Joyard committed
1 2 3 4 5 6 7 8 9 10
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('memopol_themes', '0001_initial'),
Nicolas Joyard's avatar
Nicolas Joyard committed
11
        ('representatives_positions', '0006_positionscore'),
Nicolas Joyard's avatar
Nicolas Joyard committed
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 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
    ]

    operations = [
        migrations.CreateModel(
            name='ThemeScore',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('score', models.FloatField(default=0)),
            ],
            options={
                'ordering': ['theme__slug'],
                'db_table': 'memopol_themes_themescore',
                'managed': False,
            },
        ),

        migrations.RunSQL(
            """
            CREATE VIEW "memopol_themes_themescore"
            AS SELECT
                "representatives_representative"."id" AS "representative_id",
                "memopol_themes_theme"."id" AS "theme_id",
                SUM(COALESCE("scoresource"."score", 0)) AS "score"
            FROM
                "representatives_representative"
                LEFT OUTER JOIN "memopol_themes_theme"
                    ON 1=1
                LEFT OUTER JOIN (
                    SELECT
                        "representatives_recommendations_dossierscores"."representative_id" AS "representative_id",
                        "memopol_themes_theme"."id" AS "theme_id",
                        "representatives_recommendations_dossierscores"."score" AS "score"
                    FROM
                        "representatives_recommendations_dossierscores"
                        INNER JOIN "memopol_themes_theme_dossiers"
                            ON "memopol_themes_theme_dossiers"."dossier_id" = "representatives_recommendations_dossierscores"."dossier_id"
                        INNER JOIN "memopol_themes_theme"
                            ON "memopol_themes_theme"."id" = "memopol_themes_theme_dossiers"."theme_id"
                    UNION ALL
                    SELECT
                        "representatives_recommendations_votescores"."representative_id" AS "representative_id",
                        "memopol_themes_theme"."id" AS "theme_id",
                        "representatives_recommendations_votescores"."score" AS "score"
                    FROM
                        "representatives_recommendations_votescores"
                        INNER JOIN "memopol_themes_theme_proposals"
                            ON "memopol_themes_theme_proposals"."proposal_id" = "representatives_recommendations_votescores"."proposal_id"
                        INNER JOIN "memopol_themes_theme"
                            ON "memopol_themes_theme"."id" = "memopol_themes_theme_proposals"."theme_id"
                    UNION ALL
                    SELECT
                        "representatives_positions_positionscore"."representative_id" AS "representative_id",
                        "memopol_themes_theme"."id" AS "theme_id",
                        "representatives_positions_positionscore"."score" AS "score"
                    FROM
                        "representatives_positions_positionscore"
                        INNER JOIN "memopol_themes_theme_positions"
                            ON "memopol_themes_theme_positions"."position_id" = "representatives_positions_positionscore"."position_id"
                        INNER JOIN "memopol_themes_theme"
                            ON "memopol_themes_theme"."id" = "memopol_themes_theme_positions"."theme_id"
                ) "scoresource"
                    ON "scoresource"."theme_id" = "memopol_themes_theme"."id"
                    AND "scoresource"."representative_id" = "representatives_representative"."id"
            GROUP BY
                "representatives_representative"."id",
                "memopol_themes_theme"."id"
            """
        ),
    ]