From dc3c9da5bed2c1fb04d49df4a725e237ee0fc61e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Koci=C3=A1n?= Date: Thu, 24 Sep 2015 11:11:54 +0200 Subject: [PATCH] =?UTF-8?q?Admin:=20zobrazuje=20cel=C3=A9=20jm=C3=A9no,=20?= =?UTF-8?q?p=C5=99edvypl=C5=88uje,=20ukl=C3=A1d=C3=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- seminar/admin.py | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/seminar/admin.py b/seminar/admin.py index c28e54b9..28698611 100644 --- a/seminar/admin.py +++ b/seminar/admin.py @@ -14,28 +14,19 @@ from django.contrib.auth.models import User from seminar.models import Skola, Resitel, Rocnik, Cislo, Problem, Reseni, PrilohaReseni, Nastaveni, Soustredeni, Soustredeni_Ucastnici, Novinky, Organizator import autocomplete_light -uzivatele = [(user, u"{} ()".format(1)) for user in User.objects.filter(is_staff=True)] -print(uzivatele) class UserModelChoiceField(forms.ModelChoiceField): + u"""Vlastní ModelChoiceField pro uživatele. Zobrazí kromě loginu i jméno. + """ def label_from_instance(self, obj): return u"{} ({})".format(obj.get_full_name(), obj.username) -def predvypln_prihlaseneho(*pole): - u"""Dekorátor pro Adminy. Předvyplní do *polí přihlášeného uživatele. +def get_form_predvypln_autora(self, request, obj=None, *args, **kwargs): + u"""get_form fce pro Adminy. Předvyplňí přihlášeného uživatele jako autora. """ - def formfield_for_foreignkey(self, db_field, request, **kwargs): - if db_field.name in pole: - kwargs['initial'] = request.user.id - return super(self.__class__, self).formfield_for_foreignkey( - db_field, request, **kwargs - ) - - def decorator(orig_class): - orig_class.formfield_for_foreignkey = formfield_for_foreignkey - return orig_class - - return decorator + form = super(self.__class__, self).get_form(request, *args, **kwargs) + form.base_fields['autor'].initial = request.user.id + return form def make_set_action(atribut, hodnota, nazev): @@ -303,7 +294,7 @@ class ProblemAdminForm(forms.ModelForm): text_org = forms.CharField(widget=CKEditorWidget(), required=False, **field_labels(Problem, 'text_org')) zamereni = TaggitField(widget=TaggitWidget('TagAutocomplete'), required=False) autor = UserModelChoiceField(User.objects.filter(is_staff=True)) - opravovatel = UserModelChoiceField(User.objects.filter(is_staff=True)) + opravovatel = UserModelChoiceField(User.objects.filter(is_staff=True), required=False) class Meta: model = Problem exclude = [] @@ -327,7 +318,6 @@ class ProblemAdmin(reversion.VersionAdmin): def pocet_reseni(self, obj): return obj.pocet_reseni -@predvypln_prihlaseneho('autor') class ProblemNavrhAdmin(ProblemAdmin): list_display = ['nazev', 'typ', 'zamereni', 'stav', 'autor', 'timestamp'] list_filter = ['typ', 'zamereni', 'timestamp', 'stav'] @@ -336,9 +326,11 @@ class ProblemNavrhAdmin(ProblemAdmin): qs = super(ProblemNavrhAdmin, self).get_queryset(request) return qs.filter(stav__in=[Problem.STAV_NAVRH, Problem.STAV_SMAZANY]) + get_form = get_form_predvypln_autora + + create_modeladmin(ProblemNavrhAdmin, Problem, 'ProblemNavrh', verbose_name=u'Problém (návrh)', verbose_name_plural=u'Problémy (návrhy)') -@predvypln_prihlaseneho('autor') class ProblemZadanyAdmin(ProblemAdmin): list_display = ['nazev', 'typ', 'autor', 'opravovatel', 'kod', 'cislo_zadani', 'pocet_reseni', 'verejne'] list_filter = ['typ', 'cislo_zadani__cislo', 'cislo_zadani__rocnik'] @@ -348,6 +340,8 @@ class ProblemZadanyAdmin(ProblemAdmin): qs = super(ProblemZadanyAdmin, self).get_queryset(request) return qs.filter(stav=Problem.STAV_ZADANY).annotate(pocet_reseni=Count('reseni')) + get_form = get_form_predvypln_autora + create_modeladmin(ProblemZadanyAdmin, Problem, 'ProblemZadany', verbose_name=u'Problém (zadaný)', verbose_name_plural=u'Problémy (zadané)') #admin.site.register(Problem, ProblemAdmin) @@ -405,12 +399,13 @@ def zneverejnit_novinky(modeladmin, request, queryset): zneverejnit_novinky.short_description = 'Zneveřejnit vybrané novinky' -@predvypln_prihlaseneho('autor') class NovinkyAdmin(admin.ModelAdmin): form = NovinkyAdminForm list_display = ['datum', 'autor', 'text', 'zverejneno', 'obrazek'] actions = [zverejnit_novinky, zneverejnit_novinky] + get_form = get_form_predvypln_autora + admin.site.register(Novinky, NovinkyAdmin)