diff --git a/seminar/admin.py b/seminar/admin.py index a40a31de..e88af140 100644 --- a/seminar/admin.py +++ b/seminar/admin.py @@ -14,6 +14,7 @@ import seminar.models as m admin.site.register(m.Rocnik) admin.site.register(m.Deadline) +admin.site.register(m.ZmrazenaVysledkovka) class DeadlineAdminInline(admin.TabularInline): diff --git a/seminar/migrations/0107_zmrazenavysledkovka.py b/seminar/migrations/0107_zmrazenavysledkovka.py new file mode 100644 index 00000000..e8cf3002 --- /dev/null +++ b/seminar/migrations/0107_zmrazenavysledkovka.py @@ -0,0 +1,26 @@ +# Generated by Django 3.2.15 on 2022-10-10 07:23 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('seminar', '0106_remove_cislo_verejna_vysledkovka'), + ] + + operations = [ + migrations.CreateModel( + name='ZmrazenaVysledkovka', + fields=[ + ('deadline', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, related_name='vysledkovka_v_deadlinu', serialize=False, to='seminar.deadline')), + ('html', models.TextField()), + ], + options={ + 'verbose_name': 'Zmražená výsledkovka', + 'verbose_name_plural': 'Zmražené výsledkovky', + 'db_table': 'seminar_vysledkovky', + }, + ), + ] diff --git a/seminar/models/tvorba.py b/seminar/models/tvorba.py index 3d03ec2e..93f4ea78 100644 --- a/seminar/models/tvorba.py +++ b/seminar/models/tvorba.py @@ -9,6 +9,7 @@ import logging from django.contrib.sites.shortcuts import get_current_site from django.db import models from django.db.models import Q +from django.template.loader import render_to_string from django.utils import timezone from django.conf import settings from django.urls import reverse @@ -321,6 +322,10 @@ class Deadline(SeminarModelBase): verbose_name_plural = 'Deadliny' ordering = ['deadline'] + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.__original_verejna_vysledkovka = self.verejna_vysledkovka + id = models.AutoField(primary_key=True) deadline = models.DateTimeField(blank=False, default=timezone.make_aware(datetime.datetime.combine(timezone.now(), datetime.time.max))) @@ -350,6 +355,40 @@ class Deadline(SeminarModelBase): def __str__(self): return self.CHOICES_MAP[self.typ] + " " + str(self.cislo) + def save(self, *args, **kwargs): + super().save(*args, **kwargs) + if self.verejna_vysledkovka and not self.__original_verejna_vysledkovka: + self.vygeneruj_vysledkovku() + + def vygeneruj_vysledkovku(self): + from vysledkovky.utils import VysledkovkaCisla + if hasattr(self, "vysledkovka_v_deadlinu"): + self.vysledkovka_v_deadlinu.delete() + vysledkovka = VysledkovkaCisla(self.cislo, jen_verejne=True, do_deadlinu=self) + if len(vysledkovka.radky_vysledkovky) != 0: + ZmrazenaVysledkovka.objects.create( + deadline=self, + html=render_to_string( + "vysledkovky/vysledkovka_cisla.html", + context={"vysledkovka": vysledkovka, "oznaceni_vysledkovky": self.id} + ) + ) + + +class ZmrazenaVysledkovka(SeminarModelBase): + class Meta: + db_table = 'seminar_vysledkovky' + verbose_name = 'Zmražená výsledkovka' + verbose_name_plural = 'Zmražené výsledkovky' + + deadline = models.OneToOneField( + Deadline, + on_delete=models.CASCADE, + primary_key=True, + related_name="vysledkovka_v_deadlinu" + ) + + html = models.TextField(null=False, blank=False) @reversion.register(ignore_duplicates=True) diff --git a/seminar/templates/seminar/archiv/cislo.html b/seminar/templates/seminar/archiv/cislo.html index ee37fee5..7daac388 100644 --- a/seminar/templates/seminar/archiv/cislo.html +++ b/seminar/templates/seminar/archiv/cislo.html @@ -69,7 +69,7 @@ {% for deadline, nadpis, vysledkovka in deadliny_s_vysledkovkami %} - {% if vysledkovka.radky_vysledkovky %} + {% if deadline.vysledkovka_v_deadlinu or vysledkovka.radky_vysledkovky %} {% if not deadline.verejna_vysledkovka %}