From a35c9671cdc55bfdd3babb4b64677342a5adc3de Mon Sep 17 00:00:00 2001 From: Tomas Gavenciak Date: Sun, 26 Jul 2015 15:21:44 +0200 Subject: [PATCH] DB views pro body za vsechny rocniky, prepis views na citelnejsi --- .../migrations/0028_add_body_celkem_views.py | 75 +++++++++++++++++++ seminar/models.py | 26 ++++--- 2 files changed, 89 insertions(+), 12 deletions(-) create mode 100644 seminar/migrations/0028_add_body_celkem_views.py diff --git a/seminar/migrations/0028_add_body_celkem_views.py b/seminar/migrations/0028_add_body_celkem_views.py new file mode 100644 index 00000000..0f6799f0 --- /dev/null +++ b/seminar/migrations/0028_add_body_celkem_views.py @@ -0,0 +1,75 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations +import django_countries.fields +import seminar.models +import django.utils.timezone +from django.conf import settings + +CREATE_VIEWS=""" +drop view seminar_body_k_cislu; +drop view seminar_body_za_cislo; + +create view seminar_body_za_cislo as + select + seminar_reseni.cislo_body_id || '-' || seminar_reseni.resitel_id as id, + seminar_reseni.cislo_body_id as cislo_id, + seminar_reseni.resitel_id as resitel_id, + seminar_cisla.cislo as cislo, + seminar_cisla.rocnik_id as rocnik_id, + seminar_rocniky.rocnik as rocnik, + seminar_cisla.verejna_vysledkovka as verejna_vysledkovka, + sum(seminar_reseni.body) as body + from + seminar_reseni + inner join seminar_cisla on seminar_reseni.cislo_body_id = seminar_cisla.id + inner join seminar_rocniky on seminar_cisla.rocnik_id = seminar_rocniky.id + group by seminar_reseni.cislo_body_id, seminar_reseni.resitel_id, seminar_cisla.cislo, + seminar_cisla.rocnik_id, seminar_rocniky.rocnik, seminar_cisla.verejna_vysledkovka + order by body desc; + +create view seminar_body_k_cislu_rocnik as + select + akt_body.id as id, + akt_body.cislo_id as cislo_id, + akt_body.resitel_id as resitel_id, + akt_body.body as body, + sum(min_body.body) as body_celkem + from + seminar_body_za_cislo as akt_body + inner join seminar_body_za_cislo as min_body on + (akt_body.resitel_id = min_body.resitel_id and + (akt_body.rocnik = min_body.rocnik and akt_body.cislo >= min_body.cislo) + ) + group by akt_body.id, akt_body.cislo_id, akt_body.resitel_id, akt_body.body + order by body_celkem desc; + +create view seminar_body_k_cislu_odjakziva as + select + akt_body.id as id, + akt_body.cislo_id as cislo_id, + akt_body.resitel_id as resitel_id, + akt_body.body as body, + sum(min_body.body) as body_celkem + from + seminar_body_za_cislo as akt_body + inner join seminar_body_za_cislo as min_body on + (akt_body.resitel_id = min_body.resitel_id and + ((akt_body.rocnik = min_body.rocnik and akt_body.cislo >= min_body.cislo) or + (akt_body.rocnik < min_body.rocnik) + ) + ) + group by akt_body.id, akt_body.cislo_id, akt_body.resitel_id, akt_body.body + order by body_celkem desc; + +""" + +class Migration(migrations.Migration): + + dependencies = [("seminar", "0027_export_flag_a_typ_akce")] + + operations = [ + migrations.RunSQL(sql=CREATE_VIEWS) + ] + diff --git a/seminar/models.py b/seminar/models.py index c93ac71d..8e941bf5 100644 --- a/seminar/models.py +++ b/seminar/models.py @@ -650,9 +650,9 @@ class VysledkyBase(SeminarModelBase): def __str__(self): return force_unicode(u"%s: %sb (%s)" % (self.resitel.plne_jmeno(), self.body, str(self.cislo))) - # NOTE: DB HOG (ale nepouzivany) + # NOTE: DB zatez pri vypisu (ale nepouzivany) + -@python_2_unicode_compatible class VysledkyZaCislo(VysledkyBase): class Meta: @@ -660,25 +660,27 @@ class VysledkyZaCislo(VysledkyBase): abstract = False managed = False - def __str__(self): - # NOTE: DB HOG (ale nepouzivany) - return force_unicode(u"%s: %sb (za %s)" % (self.resitel.plne_jmeno(), self.body, str(self.cislo))) - -@python_2_unicode_compatible class VysledkyKCislu(VysledkyBase): class Meta: - db_table = 'seminar_body_k_cislu' + db_table = 'seminar_body_k_cislu_rocnik' abstract = False managed = False body_celkem = models.DecimalField(max_digits=8, decimal_places=1, db_column='body_celkem', - verbose_name=u'body celkem do čísla') + verbose_name=u'body celkem do čísla (za ročník)') - def __str__(self): - # NOTE: DB HOG (ale nepouzivany) - return force_unicode(u"%s: %sb / %sb (do %s)" % (self.resitel.plne_jmeno(), self.body, self.body_celkem, str(self.cislo))) + +class VysledkyKCisluOdjakziva(VysledkyBase): + + class Meta: + db_table = 'seminar_body_k_cislu_odjakziva' + abstract = False + managed = False + + body_celkem = models.DecimalField(max_digits=8, decimal_places=1, db_column='body_celkem', + verbose_name=u'body celkem do čísla (i minulé ročníky)') @reversion.register(ignore_duplicate_revisions=True)