2014-11-17 12:05:46 +01:00
from django . contrib import admin
2021-02-16 22:00:42 +01:00
from django . contrib . auth . models import Group
2021-01-19 21:49:50 +01:00
from django . db import models
2021-03-02 23:46:30 +01:00
from django . forms import widgets , ModelForm
from django . core . exceptions import ValidationError
2015-06-25 11:01:39 +02:00
2019-08-13 23:17:56 +02:00
from polymorphic . admin import PolymorphicParentModelAdmin , PolymorphicChildModelAdmin , PolymorphicChildModelFilter
2020-01-08 21:44:50 +01:00
from reversion . admin import VersionAdmin
from django_reverse_admin import ReverseModelAdmin
2020-09-04 17:29:22 +02:00
from solo . admin import SingletonModelAdmin
2021-03-09 22:51:36 +01:00
from django . utils . safestring import mark_safe
2019-08-13 23:17:56 +02:00
2021-09-05 09:49:09 +02:00
from seminar . views . vysledkovka import hlavni_problem
# TODO Přesunout do utils
2021-01-19 21:49:50 +01:00
2019-08-14 01:17:05 +02:00
# Todo: reversion
2019-06-20 21:45:35 +02:00
import seminar . models as m
2021-03-02 23:46:30 +01:00
import seminar . treelib as tl
2019-06-20 21:45:35 +02:00
admin . site . register ( m . Skola )
admin . site . register ( m . Prijemce )
admin . site . register ( m . Rocnik )
2021-03-02 23:46:30 +01:00
class CisloForm ( ModelForm ) :
class Meta :
model = m . Cislo
fields = ' __all__ '
2021-03-09 22:51:36 +01:00
2021-03-02 23:46:30 +01:00
def clean ( self ) :
if self . cleaned_data . get ( ' verejne_db ' ) == False :
return self . cleaned_data
2021-09-05 10:18:02 +02:00
# cn = m.CisloNode.objects.get(cislo=self.instance)
# errors = []
# 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):
# errors.append(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):
# errors.append(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):
# errors.append(ValidationError('Problém %s není zadaný ani vyřešený', code=problem))
# if errors:
# errors.append(ValidationError(mark_safe('<b>Pokud chceš učinit všechny problémy, co nejsou zadané ani vyřešené, zadanými a číslo zveřejnit, můžeš to udělat pomocí akce v <a href="/admin/seminar/cislo">seznamu čísel</a></b>')))
# raise ValidationError(errors)
2021-03-09 22:51:36 +01:00
errors = [ ]
2021-09-05 10:18:02 +02:00
for ch in m . Uloha . objects . filter ( cislo_zadani = self . instance ) :
if ch . stav not in ( m . Problem . STAV_ZADANY , m . Problem . STAV_VYRESENY ) :
errors . append (
ValidationError ( ' Úloha %(uloha)s není zadaná ani vyřešená ' , params = { ' uloha ' : ch } ) )
2021-03-09 22:51:36 +01:00
if errors :
2021-09-05 10:18:02 +02:00
errors . append ( ValidationError ( mark_safe (
' <b>Pokud chceš učinit všechny problémy, co nejsou zadané ani vyřešené, zadanými a číslo zveřejnit, můžeš to udělat pomocí akce v <a href= " /admin/seminar/cislo " >seznamu čísel</a></b> ' ) ) )
2021-03-09 22:51:36 +01:00
raise ValidationError ( errors )
2021-09-05 10:18:02 +02:00
2021-03-02 23:46:30 +01:00
return self . cleaned_data
2021-03-09 22:51:36 +01:00
2021-03-02 23:46:30 +01:00
@admin.register ( m . Cislo )
class CisloAdmin ( admin . ModelAdmin ) :
form = CisloForm
2021-03-09 22:51:36 +01:00
actions = [ ' force_publish ' ]
def force_publish ( self , request , queryset ) :
for cislo in queryset :
2021-09-05 09:49:09 +02:00
# cn = m.CisloNode.objects.get(cislo=cislo)
# 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):
# ch.tema.stav = m.Problem.STAV_ZADANY
# ch.tema.save()
#
# if isinstance(ch, m.UlohaZadaniNode) or isinstance(ch, m.UlohaVzorakNode):
# if ch.uloha.stav not in (m.Problem.STAV_ZADANY, m.Problem.STAV_VYRESENY):
# ch.uloha.stav = m.Problem.STAV_ZADANY
# ch.uloha.save()
# if isinstance(ch, m.ReseniNode):
# for problem in ch.reseni.problem_set:
# if problem not in (m.Problem.STAV_ZADANY, m.Problem.STAV_VYRESENY):
# problem.stav = m.Problem.STAV_ZADANY
# problem.save()
for ch in m . Uloha . objects . filter ( cislo_zadani = cislo ) :
if ch . stav not in ( m . Problem . STAV_ZADANY , m . Problem . STAV_VYRESENY ) :
ch . stav = m . Problem . STAV_ZADANY
ch . save ( )
hp = hlavni_problem ( ch )
if hp . stav not in ( m . Problem . STAV_ZADANY , m . Problem . STAV_VYRESENY ) :
hp . stav = m . Problem . STAV_ZADANY
hp . save ( )
# TODO Řešení, vzoráky?
# TODO Konfera/Článek?
2021-03-09 22:51:36 +01:00
cislo . verejne_db = True
cislo . save ( )
force_publish . short_description = ' Zveřejnit vybraná čísla a všechny návrhy úloh v nich učinit zadanými '
2019-08-13 23:17:56 +02:00
2020-01-16 01:26:08 +01:00
@admin.register ( m . Osoba )
class OsobaAdmin ( admin . ModelAdmin ) :
2020-12-23 00:02:11 +01:00
actions = [ ' synchronizuj_maily ' , ' udelej_orgem ' ]
2020-01-16 01:26:08 +01:00
def synchronizuj_maily ( self , request , queryset ) :
for o in queryset :
if o . user is not None :
u = o . user
u . email = o . email
u . save ( )
self . message_user ( request , " E-maily synchronizovány. " )
synchronizuj_maily . short_description = " Synchronizuj vybraným osobám e-maily do uživatelů "
2020-12-23 00:02:11 +01:00
def udelej_orgem ( self , request , queryset ) :
2021-02-16 22:00:42 +01:00
org_group = Group . objects . get ( name = ' org ' )
2020-12-23 00:02:11 +01:00
print ( queryset )
for o in queryset :
user = o . user
2021-02-16 22:00:42 +01:00
print ( user )
user . groups . add ( org_group )
2020-12-23 00:02:11 +01:00
user . is_staff = True
user . save ( )
org = m . Organizator . objects . create ( osoba = o )
org . save ( )
udelej_orgem . short_description = " Udělej vybraných osob organizátory "
2021-01-19 21:49:50 +01:00
@admin.register ( m . Organizator )
class OrganizatorAdmin ( admin . ModelAdmin ) :
2021-02-23 21:09:17 +01:00
search_fields = [ ' osoba__jmeno ' , ' osoba__prijmeni ' , ' osoba__prezdivka ' ]
2021-09-16 12:35:29 +02:00
class OsobaInline ( admin . TabularInline ) :
model = m . Osoba
2021-01-19 21:49:50 +01:00
@admin.register ( m . Resitel )
2021-09-16 12:35:29 +02:00
class ResitelAdmin ( ReverseModelAdmin ) :
2021-02-23 21:09:17 +01:00
search_fields = [ ' osoba__jmeno ' , ' osoba__prijmeni ' , ' osoba__prezdivka ' ]
2021-01-19 21:49:50 +01:00
ordering = ( ' osoba__jmeno ' , ' osoba__prijmeni ' )
2021-09-16 12:35:29 +02:00
inline_type = ' stacked '
inline_reverse = [ ' osoba ' ]
2020-12-23 00:02:11 +01:00
2019-08-14 01:34:50 +02:00
@admin.register ( m . Problem )
class ProblemAdmin ( PolymorphicParentModelAdmin ) :
base_model = m . Problem
child_models = [
m . Tema ,
m . Clanek ,
m . Uloha ,
2020-04-01 23:28:21 +02:00
m . Konfera ,
2019-08-14 01:34:50 +02:00
]
2021-02-09 22:03:00 +01:00
# Pokud chceme orezavat na aktualni rocnik, musime do modelu pridat odkaz na rocnik. Zatim bere vse.
search_fields = [ ' nazev ' ]
2019-08-13 23:17:56 +02:00
2021-02-23 21:09:17 +01:00
# V ProblemAdmin to nejde, protoze se to nepropise do deti
class ProblemAdminMixin ( object ) :
show_in_index = True
autocomplete_fields = [ ' nadproblem ' , ' autor ' , ' garant ' ]
filter_horizontal = [ ' opravovatele ' ]
2019-08-14 01:34:50 +02:00
@admin.register ( m . Tema )
2021-02-23 21:09:17 +01:00
class TemaAdmin ( ProblemAdminMixin , PolymorphicChildModelAdmin ) :
2019-08-14 01:34:50 +02:00
base_model = m . Tema
@admin.register ( m . Clanek )
2021-02-23 21:09:17 +01:00
class ClanekAdmin ( ProblemAdminMixin , PolymorphicChildModelAdmin ) :
2019-08-14 01:34:50 +02:00
base_model = m . Clanek
@admin.register ( m . Uloha )
2021-02-23 21:09:17 +01:00
class UlohaAdmin ( ProblemAdminMixin , PolymorphicChildModelAdmin ) :
2019-08-14 01:34:50 +02:00
base_model = m . Uloha
2020-04-01 23:28:21 +02:00
@admin.register ( m . Konfera )
2021-02-23 21:09:17 +01:00
class KonferaAdmin ( ProblemAdminMixin , PolymorphicChildModelAdmin ) :
2020-04-01 23:28:21 +02:00
base_model = m . Konfera
2021-01-19 21:49:50 +01:00
2020-01-08 21:44:50 +01:00
class TextAdminInline ( admin . TabularInline ) :
model = m . Text
2021-01-19 21:49:50 +01:00
formfield_overrides = {
models . TextField : { ' widget ' : widgets . TextInput }
}
2020-01-08 21:44:50 +01:00
exclude = [ ' text_zkraceny_set ' , ' text_zkraceny ' ]
2021-01-19 21:49:50 +01:00
2019-08-14 01:34:50 +02:00
admin . site . register ( m . Text )
2020-01-08 21:44:50 +01:00
class ResitelInline ( admin . TabularInline ) :
model = m . Resitel
extra = 1
2021-01-19 21:49:50 +01:00
class SoustredeniUcastniciInline ( admin . TabularInline ) :
model = m . Soustredeni_Ucastnici
extra = 1
fields = [ ' resitel ' , ' poznamka ' ]
autocomplete_fields = [ ' resitel ' ]
ordering = [ ' resitel__osoba__jmeno ' , ' resitel__osoba__prijmeni ' ]
formfield_overrides = {
models . TextField : { ' widget ' : widgets . TextInput }
}
def get_queryset ( self , request ) :
qs = super ( ) . get_queryset ( request )
return qs . select_related ( ' resitel ' , ' soustredeni ' )
class SoustredeniOrganizatoriInline ( admin . TabularInline ) :
model = m . Soustredeni . organizatori . through
extra = 1
fields = [ ' organizator ' , ' poznamka ' ]
autocomplete_fields = [ ' organizator ' ]
ordering = [ ' organizator__osoba__jmeno ' , ' organizator__prijmeni ' ]
formfield_overrides = {
models . TextField : { ' widget ' : widgets . TextInput }
}
def get_queryset ( self , request ) :
qs = super ( ) . get_queryset ( request )
return qs . select_related ( ' organizator ' , ' soustredeni ' )
@admin.register ( m . Soustredeni )
class SoustredeniAdmin ( admin . ModelAdmin ) :
model = m . Soustredeni
inline_type = ' tabular '
inlines = [ SoustredeniUcastniciInline , SoustredeniOrganizatoriInline ]
2020-01-08 21:44:50 +01:00
class PrilohaReseniInline ( admin . TabularInline ) :
model = m . PrilohaReseni
extra = 1
2019-06-20 21:45:35 +02:00
admin . site . register ( m . PrilohaReseni )
2020-01-08 21:44:50 +01:00
class Reseni_ResiteleInline ( admin . TabularInline ) :
model = m . Reseni_Resitele
2021-01-19 21:49:50 +01:00
2020-01-08 21:44:50 +01:00
@admin.register ( m . Reseni )
class ReseniAdmin ( ReverseModelAdmin ) :
base_model = m . Reseni
inline_type = ' tabular '
2021-09-10 01:19:37 +02:00
# inline_reverse = ['text_cely','resitele'] TODO vrátit zpět a zrychlit dotaz
inline_reverse = [ ' resitele ' ]
2020-01-08 21:44:50 +01:00
exclude = [ ' text_zkraceny ' , ' text_zkraceny_set ' ]
inlines = [ PrilohaReseniInline ]
# FAIL in template
# inlines = [PrilohaReseniInline,Reseni_ResiteleInline]
admin . site . register ( m . Hodnoceni )
2019-06-20 21:45:35 +02:00
admin . site . register ( m . Pohadka )
admin . site . register ( m . Obrazek )
2019-08-13 23:17:56 +02:00
# Polymorfismus pro stromy
# TODO: Inlines podle https://django-polymorphic.readthedocs.io/en/stable/admin.html
@admin.register ( m . TreeNode )
class TreeNodeAdmin ( PolymorphicParentModelAdmin ) :
base_model = m . TreeNode
child_models = [
m . RocnikNode ,
m . CisloNode ,
m . MezicisloNode ,
m . TemaVCisleNode ,
m . UlohaZadaniNode ,
m . PohadkaNode ,
m . UlohaVzorakNode ,
m . TextNode ,
2020-05-07 00:51:10 +02:00
m . CastNode ,
m . OrgTextNode ,
2019-08-13 23:17:56 +02:00
]
2020-01-16 01:26:08 +01:00
actions = [ ' aktualizuj_nazvy ' ]
# XXX: nejspíš je to totální DB HOG, nechcete to použít moc často.
def aktualizuj_nazvy ( self , request , queryset ) :
newqs = queryset . get_real_instances ( )
for tn in newqs :
tn . aktualizuj_nazev ( )
tn . save ( )
self . message_user ( request , " Názvy aktualizovány. " )
aktualizuj_nazvy . short_description = " Aktualizuj vybraným TreeNodům názvy "
2019-08-13 23:17:56 +02:00
@admin.register ( m . RocnikNode )
class RocnikNodeAdmin ( PolymorphicChildModelAdmin ) :
base_model = m . RocnikNode
show_in_index = True
@admin.register ( m . CisloNode )
class CisloNodeAdmin ( PolymorphicChildModelAdmin ) :
base_model = m . CisloNode
show_in_index = True
@admin.register ( m . MezicisloNode )
class MezicisloNodeAdmin ( PolymorphicChildModelAdmin ) :
base_model = m . MezicisloNode
show_in_index = True
@admin.register ( m . TemaVCisleNode )
class TemaVCisleNodeAdmin ( PolymorphicChildModelAdmin ) :
base_model = m . TemaVCisleNode
show_in_index = True
@admin.register ( m . UlohaZadaniNode )
class UlohaZadaniNodeAdmin ( PolymorphicChildModelAdmin ) :
base_model = m . UlohaZadaniNode
show_in_index = True
@admin.register ( m . PohadkaNode )
class PohadkaNodeAdmin ( PolymorphicChildModelAdmin ) :
base_model = m . PohadkaNode
show_in_index = True
@admin.register ( m . UlohaVzorakNode )
class UlohaVzorakNodeAdmin ( PolymorphicChildModelAdmin ) :
base_model = m . UlohaVzorakNode
show_in_index = True
@admin.register ( m . TextNode )
class TextNodeAdmin ( PolymorphicChildModelAdmin ) :
base_model = m . TextNode
show_in_index = True
2020-05-07 00:51:10 +02:00
@admin.register ( m . CastNode )
class TextNodeAdmin ( PolymorphicChildModelAdmin ) :
base_model = m . CastNode
show_in_index = True
2020-05-30 23:52:30 +02:00
fields = ( ' nadpis ' , )
2020-05-07 00:51:10 +02:00
@admin.register ( m . OrgTextNode )
class TextNodeAdmin ( PolymorphicChildModelAdmin ) :
base_model = m . OrgTextNode
show_in_index = True
2019-08-13 23:17:56 +02:00
2020-09-04 17:29:22 +02:00
admin . site . register ( m . Nastaveni , SingletonModelAdmin )
2019-06-20 21:45:35 +02:00
admin . site . register ( m . Novinky )