From 4801b61705f5f41ffadc8217393ba19445de18fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Koci=C3=A1n?= Date: Wed, 23 Sep 2015 20:38:10 +0200 Subject: [PATCH] =?UTF-8?q?Admin:=20p=C5=99edvypln=C4=9Bn=C3=AD=20autora?= =?UTF-8?q?=20u=20probl=C3=A9m=C5=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- seminar/admin.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/seminar/admin.py b/seminar/admin.py index 808d838a..1902c74c 100644 --- a/seminar/admin.py +++ b/seminar/admin.py @@ -13,6 +13,23 @@ from seminar.models import Skola, Resitel, Rocnik, Cislo, Problem, Reseni, Prilo import autocomplete_light +def predvypln_prihlaseneho(*pole): + u"""Dekorátor pro Adminy. Předvyplní do *polí přihlášeného uživatele. + """ + 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 + + def make_set_action(atribut, hodnota, nazev): u""" Pomocnik pro rychle vytvareni hromadnych admin akci ktere jen nastavuji @@ -301,6 +318,7 @@ 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'] @@ -311,6 +329,7 @@ class ProblemNavrhAdmin(ProblemAdmin): 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'] @@ -375,19 +394,12 @@ 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] - # předvyplnění přihlášeného uživatele jako autora novinky - def formfield_for_foreignkey(self, db_field, request, **kwargs): - if db_field.name == 'autor': - kwargs['initial'] = request.user.id - return super(NovinkyAdmin, self).formfield_for_foreignkey( - db_field, request, **kwargs - ) - admin.site.register(Novinky, NovinkyAdmin)