From 342c791700daae6da923b13d2ce12a46e16d8338 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=C3=A1=C5=A1=20Havelka?= Date: Sat, 1 Oct 2022 11:25:53 +0200 Subject: [PATCH] Model pro deadline --- deploy_v2/admin_org_prava.json | 15 ++++++ seminar/admin.py | 9 ++++ seminar/migrations/0103_deadline.py | 72 +++++++++++++++++++++++++++++ seminar/models/tvorba.py | 39 ++++++++++++++++ 4 files changed, 135 insertions(+) create mode 100644 seminar/migrations/0103_deadline.py diff --git a/deploy_v2/admin_org_prava.json b/deploy_v2/admin_org_prava.json index f1b6445d..2d07cf83 100644 --- a/deploy_v2/admin_org_prava.json +++ b/deploy_v2/admin_org_prava.json @@ -633,5 +633,20 @@ "codename": "view_fotkaurlvazba", "ct_app_label": "header_fotky", "ct_model": "fotkaurlvazba" + }, + { + "codename": "add_deadline", + "ct_app_label": "seminar", + "ct_model": "deadline" + }, + { + "codename": "change_deadline", + "ct_app_label": "seminar", + "ct_model": "deadline" + }, + { + "codename": "view_deadline", + "ct_app_label": "seminar", + "ct_model": "deadline" } ] diff --git a/seminar/admin.py b/seminar/admin.py index 49464771..3008b3bd 100644 --- a/seminar/admin.py +++ b/seminar/admin.py @@ -15,6 +15,14 @@ import seminar.models as m admin.site.register(m.Rocnik) +admin.site.register(m.Deadline) + + +class DeadlineAdminInline(admin.TabularInline): + model = m.Deadline + extra = 0 + + class CisloForm(ModelForm): class Meta: model = m.Cislo @@ -65,6 +73,7 @@ class CisloForm(ModelForm): class CisloAdmin(admin.ModelAdmin): form = CisloForm actions = ['force_publish'] + inlines = (DeadlineAdminInline,) def force_publish(self,request,queryset): for cislo in queryset: diff --git a/seminar/migrations/0103_deadline.py b/seminar/migrations/0103_deadline.py new file mode 100644 index 00000000..b79f6bd3 --- /dev/null +++ b/seminar/migrations/0103_deadline.py @@ -0,0 +1,72 @@ +# Generated by Django 3.2.15 on 2022-10-01 08:44 + +import datetime +from django.db import migrations, models +import django.db.models.deletion +from django.utils import timezone + +import seminar.models as m + + +def vytvor_deadliny(apps, schema_editor): + Cislo = apps.get_model('seminar', 'Cislo') + Deadline = apps.get_model('seminar', 'Deadline') + + for cislo in Cislo.objects.all(): + if cislo.datum_deadline_soustredeni and cislo.datum_deadline_soustredeni == cislo.datum_preddeadline: + d = Deadline() + d.cislo = cislo + d.typ = m.Deadline.TYP_PRVNI_A_SOUS + d.deadline = timezone.make_aware(datetime.datetime.combine(cislo.datum_deadline_soustredeni, datetime.time.max)) + d.verejna_vysledkovka = cislo.verejna_vysledkovka + d.save() + else: + if cislo.datum_deadline_soustredeni: + d = Deadline() + d.cislo = cislo + d.typ = m.Deadline.TYP_SOUS + d.deadline = timezone.make_aware(datetime.datetime.combine(cislo.datum_deadline_soustredeni, datetime.time.max)) + d.verejna_vysledkovka = cislo.verejna_vysledkovka + d.save() + if cislo.datum_preddeadline: + d = Deadline() + d.cislo = cislo + d.typ = m.Deadline.TYP_PRVNI + d.deadline = timezone.make_aware(datetime.datetime.combine(cislo.datum_preddeadline, datetime.time.max)) + d.verejna_vysledkovka = cislo.verejna_vysledkovka + d.save() + + if cislo.datum_deadline: + d = Deadline() + d.cislo = cislo + d.typ = m.Deadline.TYP_CISLA + d.deadline = timezone.make_aware(datetime.datetime.combine(cislo.datum_deadline, datetime.time.max)) + d.verejna_vysledkovka = cislo.verejna_vysledkovka + d.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('seminar', '0102_osoba_jak_se_dozvedeli'), + ] + + operations = [ + migrations.CreateModel( + name='Deadline', + fields=[ + ('id', models.AutoField(primary_key=True, serialize=False)), + ('deadline', models.DateTimeField(default=timezone.make_aware(datetime.datetime.combine(timezone.now(), datetime.time.max)))), + ('typ', models.CharField(choices=[('cisla', 'Deadline celého čísla'), ('prvni', 'První deadline'), ('prvniasous', 'Sousový a první deadline'), ('sous', 'Sousový deadline')], max_length=32, verbose_name='typ deadlinu')), + ('verejna_vysledkovka', models.BooleanField(db_column='verejna_vysledkovka', default=False, verbose_name='veřejná výsledkovka')), + ('cislo', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='deadline_v_cisle', to='seminar.cislo', verbose_name='deadline v čísle')), + ], + options={ + 'verbose_name': 'Deadline', + 'verbose_name_plural': 'Deadliny', + 'db_table': 'seminar_deadliny', + 'ordering': ['deadline'], + }, + ), + migrations.RunPython(vytvor_deadliny, migrations.RunPython.noop), + ] diff --git a/seminar/models/tvorba.py b/seminar/models/tvorba.py index e34b7681..7da4139c 100644 --- a/seminar/models/tvorba.py +++ b/seminar/models/tvorba.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +import datetime import os import subprocess import pathlib @@ -328,6 +329,44 @@ class Cislo(SeminarModelBase): raise ValidationError({'datum_deadline_soustredeni': "Soustřeďkový deadline musí předcházet finálnímu deadlinu"}) +class Deadline(SeminarModelBase): + class Meta: + db_table = 'seminar_deadliny' + verbose_name = 'Deadline' + verbose_name_plural = 'Deadliny' + ordering = ['deadline'] + + id = models.AutoField(primary_key=True) + + deadline = models.DateTimeField(blank=False, default=timezone.make_aware(datetime.datetime.combine(timezone.now(), datetime.time.max))) + + cislo = models.ForeignKey(Cislo, verbose_name='deadline v čísle', + related_name='deadline_v_cisle', blank=False, + on_delete=models.CASCADE) + + TYP_CISLA = 'cisla' + TYP_PRVNI_A_SOUS = 'prvniasous' + TYP_PRVNI = 'prvni' + TYP_SOUS = 'sous' + TYP_CHOICES = [ + (TYP_CISLA, 'Deadline celého čísla'), + (TYP_PRVNI, 'První deadline'), + (TYP_PRVNI_A_SOUS, 'Sousový a první deadline'), + (TYP_SOUS, 'Sousový deadline'), + ] + CHOICES_MAP = dict(TYP_CHOICES) + typ = models.CharField('typ deadlinu', max_length=32, + choices=TYP_CHOICES, blank=False) + + verejna_vysledkovka = models.BooleanField('veřejná výsledkovka', + db_column='verejna_vysledkovka', + default=False) + + def __str__(self): + return self.CHOICES_MAP[self.typ] + " " + str(self.cislo) + + + @reversion.register(ignore_duplicates=True) # Pozor na následující řádek. *Nekrmit, asi kouše!* class Problem(SeminarModelBase,PolymorphicModel):