From 8b37035bf3b1ab98550d5031ace0e288f8631fb1 Mon Sep 17 00:00:00 2001 From: "Tomas \"Jethro\" Pokorny" Date: Tue, 2 Mar 2021 23:46:30 +0100 Subject: [PATCH] Admin | Nedovolit zverejnit cislo, kdyz nejou zverejnene problemy. --- seminar/admin.py | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/seminar/admin.py b/seminar/admin.py index 4da32e96..849ca696 100644 --- a/seminar/admin.py +++ b/seminar/admin.py @@ -1,7 +1,8 @@ from django.contrib import admin from django.contrib.auth.models import Group from django.db import models -from django.forms import widgets +from django.forms import widgets, ModelForm +from django.core.exceptions import ValidationError from polymorphic.admin import PolymorphicParentModelAdmin, PolymorphicChildModelAdmin, PolymorphicChildModelFilter from reversion.admin import VersionAdmin @@ -12,11 +13,43 @@ from solo.admin import SingletonModelAdmin # Todo: reversion import seminar.models as m +import seminar.treelib as tl admin.site.register(m.Skola) admin.site.register(m.Prijemce) admin.site.register(m.Rocnik) -admin.site.register(m.Cislo) + +class CisloForm(ModelForm): + class Meta: + model = m.Cislo + fields = '__all__' + + def clean(self): + print("Cleaning...") + print(self.cleaned_data) + if self.cleaned_data.get('verejne_db') == False: + return self.cleaned_data + cn = m.CisloNode.objects.get(cislo=self.instance) + for ch in tl.all_children(cn): + if isinstance(ch, m.TemaVCisleNode): + if ch.tema.stav not in \ + (m.Problem.STAV_ZADANY, m.Problem.STAV_VYRESENY): + raise ValidationError('Téma %(tema)s není zadané ani vyřešené', params={'tema':ch.tema}) + + if isinstance(ch, m.UlohaZadaniNode) or isinstance(ch, m.UlohaVzorakNode): + if ch.uloha.stav not in \ + (m.Problem.STAV_ZADANY, m.Problem.STAV_VYRESENY): + raise ValidationError('Úloha %(uloha)s není zadaná ani vyřešená', params={'uloha':ch.uloha}) + if isinstance(ch, m.ReseniNode): + for problem in ch.reseni.problem_set: + if problem not in \ + (m.Problem.STAV_ZADANY, m.Problem.STAV_VYRESENY): + raise ValidationError('Problém %s není zadaný ani vyřešený', code=problem) + return self.cleaned_data + +@admin.register(m.Cislo) +class CisloAdmin(admin.ModelAdmin): + form = CisloForm @admin.register(m.Osoba) class OsobaAdmin(admin.ModelAdmin):