From 1421438c9cd62ff76679f13ad240b3f1406acd5c Mon Sep 17 00:00:00 2001 From: Tomas Gavenciak Date: Tue, 24 Mar 2015 01:12:20 +0100 Subject: [PATCH] Migrace pro seminar, SQL VIEW (body), nove modely --- seminar/migrations/0001_initial.py | 200 ++++++++++++++++++++++ seminar/migrations/0002_add_body_views.py | 48 ++++++ seminar/migrations/__init__.py | 0 seminar/models.py | 65 +++++-- 4 files changed, 302 insertions(+), 11 deletions(-) create mode 100644 seminar/migrations/0001_initial.py create mode 100644 seminar/migrations/0002_add_body_views.py create mode 100644 seminar/migrations/__init__.py diff --git a/seminar/migrations/0001_initial.py b/seminar/migrations/0001_initial.py new file mode 100644 index 00000000..5a78fa02 --- /dev/null +++ b/seminar/migrations/0001_initial.py @@ -0,0 +1,200 @@ +# -*- 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 + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='Cislo', + fields=[ + ('id', models.AutoField(serialize=False, primary_key=True)), + ('cislo', models.CharField(help_text='V\u011bt\u0161inou jen "1", vyj\xedme\u010dn\u011b "7-8"', max_length=32, verbose_name='n\xe1zev \u010d\xedsla')), + ('datum_vydani', models.DateField(help_text='Datum vyd\xe1n\xed fin\xe1ln\xed verze', null=True, verbose_name='datum vyd\xe1n\xed', blank=True)), + ('datum_deadline', models.DateField(help_text='Datum pro p\u0159\xedjem \u0159e\u0161en\xed \xfaloh zadan\xfdch v tomto \u010d\xedsle', null=True, verbose_name='datum deadline', blank=True)), + ], + options={ + 'ordering': ['rocnik__rocnik', 'cislo'], + 'db_table': 'seminar_cisla', + 'verbose_name': '\u010c\xedslo', + 'verbose_name_plural': '\u010c\xedsla', + }, + bases=(models.Model,), + ), + migrations.CreateModel( + name='Nastaveni', + fields=[ + ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), + ('aktualni_cislo', models.ForeignKey(verbose_name='posledn\xed vydan\xe9 \u010d\xedslo', to='seminar.Cislo')), + ], + options={ + 'db_table': 'seminar_nastaveni', + 'verbose_name': 'Nastaven\xed semin\xe1\u0159e', + }, + bases=(models.Model,), + ), + migrations.CreateModel( + name='PrilohaReseni', + fields=[ + ('id', models.AutoField(serialize=False, primary_key=True)), + ('timestamp', models.DateTimeField(auto_now=True, verbose_name='vytvo\u0159eno')), + ('soubor', models.FileField(upload_to=seminar.models.generate_filename, verbose_name='soubor')), + ], + options={ + 'ordering': ['reseni', 'timestamp'], + 'db_table': 'seminar_priloha_reseni', + 'verbose_name': 'P\u0159\xedloha \u0159e\u0161en\xed', + 'verbose_name_plural': 'P\u0159\xedlohy \u0159e\u0161en\xed', + }, + bases=(models.Model,), + ), + migrations.CreateModel( + name='Problem', + fields=[ + ('id', models.AutoField(serialize=False, primary_key=True)), + ('nazev', models.CharField(max_length=256, verbose_name='n\xe1zev')), + ('typ', models.CharField(default=b'uloha', max_length=32, verbose_name='typ probl\xe9mu', choices=[(b'uloha', '\xdaloha'), (b'tema', 'T\xe9ma'), (b'serial', 'Seri\xe1l'), (b'org-clanek', 'Organiz\xe1torsk\xfd \u010dl\xe1nek'), (b'res-clanek', '\u0158esitelsk\xfd \u010dl\xe1nek')])), + ('stav', models.CharField(default=b'navrh', max_length=32, verbose_name='stav probl\xe9mu', choices=[(b'navrh', 'N\xe1vrh'), (b'zadany', 'Zadan\xfd'), (b'smazany', 'Smazan\xfd')])), + ('text_problemu_org', models.TextField(verbose_name='organiz\xe1torsk\xfd (neve\u0159ejn\xfd) text', blank=True)), + ('text_problemu', models.TextField(verbose_name='ve\u0159ejn\xfd text zad\xe1n\xed a \u0159e\u0161en\xed', blank=True)), + ('kod', models.CharField(default=b'', help_text='\u010c\xedslo/k\xf3d \xfalohy v \u010d\xedsle nebo k\xf3d t\xe9matu/\u010dl\xe1nku/seri\xe1lu v ro\u010dn\xedku', max_length=32, verbose_name='lok\xe1ln\xed k\xf3d', blank=True)), + ('body', models.IntegerField(null=True, verbose_name='maximum bod\u016f', blank=True)), + ('autor', models.ForeignKey(related_name='autor_uloh', verbose_name='autor probl\xe9mu', to=settings.AUTH_USER_MODEL)), + ('cislo_reseni', models.ForeignKey(related_name='resene_problemy', blank=True, to='seminar.Cislo', help_text='\u010c\xedslo s \u0159e\u0161en\xedm \xfalohy, jen pro \xfalohy', null=True, verbose_name='\u010d\xedslo \u0159e\u0161en\xed')), + ('cislo_zadani', models.ForeignKey(related_name='zadane_problemy', verbose_name='\u010d\xedslo zad\xe1n\xed', blank=True, to='seminar.Cislo', null=True)), + ('opravovatel', models.ForeignKey(related_name='opravovatel_uloh', verbose_name='opravovatel', blank=True, to=settings.AUTH_USER_MODEL, null=True)), + ], + options={ + 'db_table': 'seminar_problemy', + 'verbose_name': 'Probl\xe9m', + 'verbose_name_plural': 'Probl\xe9my', + }, + bases=(models.Model,), + ), + migrations.CreateModel( + name='Reseni', + fields=[ + ('id', models.AutoField(serialize=False, primary_key=True)), + ('body', models.IntegerField(null=True, verbose_name='body', blank=True)), + ('timestamp', models.DateTimeField(auto_now=True, verbose_name='vytvo\u0159eno')), + ('cislo_body', models.ForeignKey(related_name='bodovana_reseni', verbose_name='\u010d\xedslo pro body', blank=True, to='seminar.Cislo', null=True)), + ('problem', models.ForeignKey(related_name='reseni', verbose_name='probl\xe9m', to='seminar.Problem')), + ], + options={ + 'ordering': ['problem', 'resitel'], + 'db_table': 'seminar_reseni', + 'verbose_name': '\u0158e\u0161en\xed', + 'verbose_name_plural': '\u0158e\u0161en\xed', + }, + bases=(models.Model,), + ), + migrations.CreateModel( + name='Resitel', + fields=[ + ('id', models.AutoField(serialize=False, primary_key=True)), + ('jmeno', models.CharField(max_length=256, verbose_name='jm\xe9no')), + ('prijmeni', models.CharField(max_length=256, verbose_name='p\u0159\xedjmen\xed')), + ('pohlavi_muz', models.BooleanField(default=False, verbose_name='pohlav\xed (mu\u017e)')), + ('rok_maturity', models.IntegerField(verbose_name='rok maturity')), + ('email', models.EmailField(default=b'', max_length=256, verbose_name='e-mail', blank=True)), + ('telefon', models.CharField(default=b'', max_length=256, verbose_name='telefon', blank=True)), + ('datum_narozeni', models.DateField(null=True, verbose_name='datum narozen\xed', blank=True)), + ('datum_souhlasu_udaje', models.DateField(help_text='Datum souhlasu se zpracov\xe1n\xedm osobn\xedch \xfadaj\u016f', null=True, verbose_name='datum souhlasu (\xfadaje)', blank=True)), + ('datum_souhlasu_zasilani', models.DateField(help_text='Datum souhlasu se zas\xedl\xe1n\xedm MFF materi\xe1l\u016f', null=True, verbose_name='datum souhlasu (spam)', blank=True)), + ('datum_prihlaseni', models.DateField(default=django.utils.timezone.now, verbose_name='datum p\u0159ihl\xe1\u0161en\xed')), + ('zasilat', models.CharField(default=b'domu', max_length=32, verbose_name='kam zas\xedlat', choices=[(b'domu', 'Dom\u016f'), (b'do_skoly', 'Do \u0161koly'), (b'nikam', 'Nikam')])), + ('ulice', models.CharField(default=b'', max_length=256, verbose_name='ulice', blank=True)), + ('mesto', models.CharField(default=b'', max_length=256, verbose_name='m\u011bsto', blank=True)), + ('psc', models.CharField(default=b'', max_length=32, verbose_name='PS\u010c', blank=True)), + ('stat', django_countries.fields.CountryField(default=b'CZ', help_text='ISO 3166-1 k\xf3d zem\u011b velk\xfdmi p\xedsmeny (CZ, SK, ...)', max_length=2, verbose_name='st\xe1t')), + ], + options={ + 'ordering': ['prijmeni', 'jmeno'], + 'db_table': 'seminar_resitele', + 'verbose_name': '\u0158e\u0161itel', + 'verbose_name_plural': '\u0158e\u0161itel\xe9', + }, + bases=(models.Model,), + ), + migrations.CreateModel( + name='Rocnik', + fields=[ + ('id', models.AutoField(serialize=False, primary_key=True)), + ('prvni_rok', models.IntegerField(verbose_name='prvn\xed rok')), + ('rocnik', models.CharField(max_length=16, verbose_name='\u010d\xedslo ro\u010dn\xedku')), + ], + options={ + 'ordering': ['rocnik'], + 'db_table': 'seminar_rocniky', + 'verbose_name': 'Ro\u010dn\xedk', + 'verbose_name_plural': 'Ro\u010dn\xedky', + }, + bases=(models.Model,), + ), + migrations.CreateModel( + name='Skola', + fields=[ + ('id', models.AutoField(serialize=False, primary_key=True)), + ('aesop_id', models.CharField(default=b'', help_text='Aesopi ID typu "izo:..." nebo "aesop:..."', max_length=32, verbose_name='Aesop ID', blank=True)), + ('izo', models.CharField(help_text='IZO \u0161koly (jen \u010desk\xe9 \u0161koly)', max_length=32, verbose_name='IZO', blank=True)), + ('nazev', models.CharField(help_text='Cel\xfd n\xe1zev \u0161koly', max_length=256, verbose_name='n\xe1zev')), + ('kratky_nazev', models.CharField(help_text=b'Zkr\xc3\xa1cen\xc3\xbd n\xc3\xa1zev pro zobrazen\xc3\xad ve v\xc3\xbdsledkovce', max_length=256, verbose_name='zkr\xe1cen\xfd n\xe1zev', blank=True)), + ('ulice', models.CharField(max_length=256, verbose_name='ulice')), + ('mesto', models.CharField(max_length=256, verbose_name='m\u011bsto')), + ('psc', models.CharField(max_length=32, verbose_name='PS\u010c')), + ('stat', django_countries.fields.CountryField(default=b'CZ', help_text='ISO 3166-1 k\xf3d zeme velk\xfdmi p\xedsmeny (CZ, SK, ...)', max_length=2, verbose_name='st\xe1t')), + ], + options={ + 'db_table': 'seminar_skoly', + 'verbose_name': '\u0160kola', + 'verbose_name_plural': '\u0160koly', + }, + bases=(models.Model,), + ), + migrations.AddField( + model_name='resitel', + name='skola', + field=models.ForeignKey(verbose_name='\u0161kola', blank=True, to='seminar.Skola', null=True), + preserve_default=True, + ), + migrations.AddField( + model_name='resitel', + name='user', + field=models.OneToOneField(null=True, blank=True, to=settings.AUTH_USER_MODEL, verbose_name='u\u017eivatel'), + preserve_default=True, + ), + migrations.AddField( + model_name='reseni', + name='resitel', + field=models.ForeignKey(related_name='reseni', verbose_name='\u0159e\u0161itel', to='seminar.Resitel'), + preserve_default=True, + ), + migrations.AddField( + model_name='prilohareseni', + name='reseni', + field=models.ForeignKey(related_name='prilohy', verbose_name='\u0159e\u0161en\xed', to='seminar.Reseni'), + preserve_default=True, + ), + migrations.AddField( + model_name='nastaveni', + name='aktualni_rocnik', + field=models.ForeignKey(verbose_name='aktu\xe1ln\xed ro\u010dn\xedk', to='seminar.Rocnik'), + preserve_default=True, + ), + migrations.AddField( + model_name='cislo', + name='rocnik', + field=models.ForeignKey(verbose_name='ro\u010dn\xedk', to='seminar.Rocnik'), + preserve_default=True, + ), + ] diff --git a/seminar/migrations/0002_add_body_views.py b/seminar/migrations/0002_add_body_views.py new file mode 100644 index 00000000..d146419d --- /dev/null +++ b/seminar/migrations/0002_add_body_views.py @@ -0,0 +1,48 @@ +# -*- 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=""" +create view seminar_body_za_cislo as + select + sr.cislo_body_id || "-" || sr.resitel_id as id, + sr.cislo_body_id as cislo_id, + sr.resitel_id as resitel_id, + sum(sr.body) as body + from seminar_reseni as sr + group by sr.cislo_body_id, sr.resitel_id + order by body desc; + +create view seminar_body_k_cislu as + select + akt_c.id || "-" || min_bzc.resitel_id as id, + akt_c.id as cislo_id, + min_bzc.resitel_id as resitel_id, + akt_bzc.body as body, + sum(min_bzc.body) as body_celkem + from ((seminar_cisla as akt_c + inner join seminar_cisla as min_c on min_c.rocnik_id == akt_c.rocnik_id and akt_c.cislo >= min_c.cislo) + inner join seminar_body_za_cislo as min_bzc on min_c.id == min_bzc.cislo_id) + left outer join seminar_body_za_cislo as akt_bzc on akt_bzc.cislo_id == akt_c.id and akt_bzc.resitel_id == min_bzc.resitel_id + group by akt_c.id, min_bzc.resitel_id + order by body_celkem desc; +""" + +DROP_VIEWS=""" +drop view seminar_body_za_cislo; +drop view seminar_body_do_cisla; +""" + +class Migration(migrations.Migration): + + dependencies = [("seminar", "0001_initial")] + + operations = [ + migrations.RunSQL(sql=CREATE_VIEWS, reverse_sql=DROP_VIEWS) + ] + diff --git a/seminar/migrations/__init__.py b/seminar/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/seminar/models.py b/seminar/models.py index dc393344..9863ee4e 100644 --- a/seminar/models.py +++ b/seminar/models.py @@ -176,7 +176,7 @@ class Cislo(models.Model): rocnik = models.ForeignKey(Rocnik, verbose_name=u'ročník') cislo = models.CharField(u'název čísla', max_length=32, - help_text=u'Většinou jen "1", vyjímečně "7-8"') + help_text=u'Většinou jen "1", vyjímečně "7-8", lexikograficky určije pořadí v ročníku!') datum_vydani = models.DateField(u'datum vydání', blank=True, null=True, help_text=u'Datum vydání finální verze') @@ -276,9 +276,18 @@ class Reseni(models.Model): timestamp = models.DateTimeField(u'vytvořeno', auto_now=True) def __str__(self): - return force_unicode(u"%s: %s" % (self.resitel.plne_jmeno(), self.problem.nazev)) + return force_unicode(u"%s: %s (%sb)" % (self.resitel.plne_jmeno(), self.problem.nazev, self.body)) +# PrilohaReseni method +def generate_filename(self, filename): + clean = filename.replace('/','-').replace('\0', '') + datedir = datetime.datetime.now().strftime('%Y-%m') + fname = "%s_%s" % ( + datetime.datetime.now().strftime('%Y-%m-%d-%H:%M'), + clean) + return os.path.join(settings.SEMINAR_RESENI_DIRNAME, datedir, fname) + @python_2_unicode_compatible class PrilohaReseni(models.Model): @@ -295,21 +304,55 @@ class PrilohaReseni(models.Model): timestamp = models.DateTimeField(u'vytvořeno', auto_now=True) - def generate_filename(self, filename): - clean = filename.replace('/','-').replace('\0', '') - datedir = datetime.datetime.now().strftime('%Y-%m') - fname = "%s_%06d_%s" % ( - datetime.datetime.now().strftime('%Y-%m-%d-%H:%M'), - random.randint(0,999999), - clean) - return os.path.join(settings.SEMINAR_RESENI_DIRNAME, datedir, fname) - soubor = models.FileField(u'soubor', upload_to = generate_filename) def __str__(self): return force_unicode(self.soubor) +class VysledkyBase(models.Model): + + class Meta: + verbose_name = u'Řádek výsledkovky' + verbose_name_plural = u'Řádky výsledkovky' + ordering = ['body'] + abstract = True + managed = False + + dummy_id = models.CharField(u'dummy ID pro view', max_length=32, primary_key=True, db_column='id') + + cislo = models.ForeignKey(Cislo, verbose_name=u'číslo pro body', db_column='cislo_id') + + resitel = models.ForeignKey(Resitel, verbose_name=u'řešitel', db_column='resitel_id') + + body = models.IntegerField(u'body za číslo', db_column='body') + + +@python_2_unicode_compatible +class VysledkyZaCislo(VysledkyBase): + + class Meta: + db_table = 'seminar_body_za_cislo' + abstract = False + managed = False + + def __str__(self): + 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' + abstract = False + managed = False + + body_celkem = models.IntegerField(u'body celkem do čísla', db_column='body_celkem') + + def __str__(self): + return force_unicode(u"%s: %sb / %sb (do %s)" % (self.resitel.plne_jmeno(), self.body, self.body_celkem, str(self.cislo))) + + @python_2_unicode_compatible class Nastaveni(SingletonModel):