Browse Source

Merge branch 'faze-tvorby-cisla'

remotes/origin/test
Matěj Kocián 8 years ago
parent
commit
72b99f2960
  1. 63
      seminar/admin.py

63
seminar/admin.py

@ -347,7 +347,20 @@ class PohadkaAdminForm(forms.ModelForm):
model = Pohadka model = Pohadka
exclude = [] exclude = []
autor = UserModelChoiceField(User.objects.filter(is_staff=True)) autor = UserModelChoiceField(User.objects.filter(is_staff=True))
uloha = forms.ModelChoiceField(Problem.objects.filter(typ=Problem.TYP_ULOHA)) uloha = forms.ModelChoiceField(
Problem.objects.filter(typ=Problem.TYP_ULOHA)
)
def __init__(self, *args, **kwargs):
super(PohadkaAdminForm, self).__init__(*args, **kwargs)
instance = getattr(self, 'instance', None)
# viz ProblemAdminForm.__init__
if instance and instance.pk:
if instance.uloha and instance.uloha.cislo_zadani:
if instance.uloha.cislo_zadani.faze != 'admin':
self.fields['text'].widget.attrs['readonly'] = True
class PohadkaAdmin(VersionAdmin): class PohadkaAdmin(VersionAdmin):
form = PohadkaAdminForm form = PohadkaAdminForm
@ -361,12 +374,6 @@ class PohadkaAdmin(VersionAdmin):
return obj.uloha.cislo_zadani.rocnik.rocnik return obj.uloha.cislo_zadani.rocnik.rocnik
get_rocnik.short_description = u'Ročník' get_rocnik.short_description = u'Ročník'
def get_readonly_fields(self, request, obj=None):
if not obj:
return []
if obj.uloha.cislo_zadani.faze != 'admin':
return ['text']
list_display = [ list_display = [
'__str__', '__str__',
'get_rocnik', 'get_rocnik',
@ -403,6 +410,38 @@ class ProblemAdminForm(forms.ModelForm):
model = Problem model = Problem
exclude = [] exclude = []
def __init__(self, *args, **kwargs):
super(ProblemAdminForm, self).__init__(*args, **kwargs)
instance = getattr(self, 'instance', None)
# Nedovol měnit název a zadání resp. řešení, pokud je cislo_zadani
# resp. cislo_reseni mimo fázi admin.
#
# Nastavení readonly fields sice vypadá lépe (nevygeneruje input tag),
# ale při ukládání změny vypíše admin nespecifikovanou chybu, která je
# způsobena zřejmě tím, že se neodešle žádná hodnota pro povinné pole
# nazev. Navíc by se smazalo nepovinné pole zadání.
#
# Toto řešení je z http://stackoverflow.com/a/325038/4786205.
#
# TODO Django 1.9: použít field s atributem disabled?
if instance and instance.pk:
if instance.cislo_zadani and instance.cislo_zadani.faze != 'admin':
# CKEditor neumí readonly ...
self.fields['text_zadani'] = forms.CharField(
widget=forms.Textarea,
required=False
)
for f in ['nazev', 'text_zadani', 'body']:
self.fields[f].widget.attrs['readonly'] = True
if instance.cislo_reseni and instance.cislo_reseni.faze != 'admin':
self.fields['text_reseni'] = forms.CharField(
widget=forms.Textarea,
required=False
)
self.fields['text_reseni'].widget.attrs['readonly'] = True
class ProblemAdmin(VersionAdmin): class ProblemAdmin(VersionAdmin):
form = ProblemAdminForm form = ProblemAdminForm
fieldsets = [ fieldsets = [
@ -415,15 +454,7 @@ class ProblemAdmin(VersionAdmin):
view_on_site = Problem.verejne_url view_on_site = Problem.verejne_url
ordering = ['-timestamp'] ordering = ['-timestamp']
def get_readonly_fields(self, request, obj=None): readonly_fields = ['timestamp', 'import_dakos_id']
readonly_fields = ['timestamp', 'import_dakos_id']
if not obj:
return readonly_fields
if obj.cislo_zadani and obj.cislo_zadani.faze != 'admin':
readonly_fields += ['nazev', 'text_zadani', 'body']
if obj.cislo_reseni and obj.cislo_reseni.faze != 'admin':
readonly_fields += ['text_reseni']
return readonly_fields
def get_queryset(self, request): def get_queryset(self, request):
qs = super(ProblemAdmin, self).get_queryset(request) qs = super(ProblemAdmin, self).get_queryset(request)

Loading…
Cancel
Save