diff --git a/seminar/admin.py b/seminar/admin.py index 82df8fff..9ce11a93 100644 --- a/seminar/admin.py +++ b/seminar/admin.py @@ -347,7 +347,20 @@ class PohadkaAdminForm(forms.ModelForm): model = Pohadka exclude = [] 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): form = PohadkaAdminForm @@ -361,12 +374,6 @@ class PohadkaAdmin(VersionAdmin): return obj.uloha.cislo_zadani.rocnik.rocnik 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 = [ '__str__', 'get_rocnik', @@ -403,6 +410,38 @@ class ProblemAdminForm(forms.ModelForm): model = Problem 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): form = ProblemAdminForm fieldsets = [ @@ -415,15 +454,7 @@ class ProblemAdmin(VersionAdmin): view_on_site = Problem.verejne_url ordering = ['-timestamp'] - def get_readonly_fields(self, request, obj=None): - 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 + readonly_fields = ['timestamp', 'import_dakos_id'] def get_queryset(self, request): qs = super(ProblemAdmin, self).get_queryset(request)