From 234203c5706421cd4beb54cb3b710d70432ce369 Mon Sep 17 00:00:00 2001 From: Tomas Gavenciak Date: Wed, 27 May 2015 09:20:15 +0200 Subject: [PATCH] Rozdeleni problemu na navrhy a zadane --- seminar/admin.py | 43 +++++++++++++++++++++++++++++++++++++------ seminar/models.py | 6 ++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/seminar/admin.py b/seminar/admin.py index 0bb5deba..00f71be6 100644 --- a/seminar/admin.py +++ b/seminar/admin.py @@ -23,6 +23,19 @@ def field_labels(model, fieldname): f = [i for i in model._meta.fields if i.name == fieldname][0] return {'label': f.verbose_name.capitalize(), 'help_text': f.help_text, } +def create_modeladmin(modeladmin, model, name = None, verbose_name = None, verbose_name_plural = None): + + class Meta: + proxy = True + app_label = model._meta.app_label + Meta.verbose_name = verbose_name + Meta.verbose_name_plural = verbose_name_plural + + attrs = {'__module__': '', 'Meta': Meta} + newmodel = type(name, (model,), attrs) + admin.site.register(newmodel, modeladmin) + return modeladmin + ### INLINES class ResitelInline(admin.TabularInline): @@ -231,6 +244,7 @@ admin.site.register(Reseni, ReseniAdmin) from autocomplete_light.contrib.taggit_field import TaggitField, TaggitWidget #TODO: Autocomplete autor/opravovatel + class ProblemAdminForm(forms.ModelForm): text_zadani = forms.CharField(widget=CKEditorWidget(), **field_labels(Problem, 'text_zadani')) text_reseni = forms.CharField(widget=CKEditorWidget(), **field_labels(Problem, 'text_reseni')) @@ -248,22 +262,39 @@ class ProblemAdmin(reversion.VersionAdmin): (None, {'fields': ['text_zadani', 'text_reseni', 'text_org',]}), ] readonly_fields = ['timestamp', 'import_dakos_id'] - list_display = ['nazev', 'typ', 'kod', 'stav', 'autor', 'opravovatel', 'verejne', 'cislo_zadani', 'pocet_reseni'] list_select_related = True - list_filter = ['typ', 'stav', 'timestamp'] - search_fields = ['nazev', 'kod', 'text_zadani', 'text_reseni', 'text_org'] - inlines = [ReseniKProblemuInline] + search_fields = ['nazev', 'text_zadani', 'text_reseni', 'text_org'] view_on_site = Problem.verejne_url def get_queryset(self, request): qs = super(ProblemAdmin, self).get_queryset(request) - return qs.select_related('autor', 'opravovatel', 'cislo_zadani', 'cislo_reseni').annotate(pocet_reseni=Count('reseni')) + return qs.select_related('autor', 'opravovatel', 'cislo_zadani', 'cislo_reseni') def pocet_reseni(self, obj): return obj.pocet_reseni -admin.site.register(Problem, ProblemAdmin) +class ProblemNavrhAdmin(ProblemAdmin): + list_display = ['nazev', 'typ', 'stav', 'autor', 'timestamp'] + list_filter = ['typ', 'stav', 'timestamp'] + + def get_queryset(self, request): + qs = super(ProblemNavrhAdmin, self).get_queryset(request) + return qs.filter(stav__in=[Problem.STAV_NAVRH, Problem.STAV_SMAZANY]) + +create_modeladmin(ProblemNavrhAdmin, Problem, 'ProblemNavrh', verbose_name=u'Problém (návrh)', verbose_name_plural=u'Problémy (návrhy)') + +class ProblemZadanyAdmin(ProblemAdmin): + list_display = ['nazev', 'typ', 'autor', 'opravovatel', 'kod', 'cislo_zadani', 'pocet_reseni', 'verejne'] + list_filter = ['typ', 'cislo_zadani__rocnik'] + inlines = [ReseniKProblemuInline] + + def get_queryset(self, request): + qs = super(ProblemZadanyAdmin, self).get_queryset(request) + return qs.filter(stav=Problem.STAV_ZADANY).annotate(pocet_reseni=Count('reseni')) + +create_modeladmin(ProblemZadanyAdmin, Problem, 'ProblemZadany', verbose_name=u'Problém (zadaný)', verbose_name_plural=u'Problémy (zadané)') +#admin.site.register(Problem, ProblemAdmin) ### Soustredeni diff --git a/seminar/models.py b/seminar/models.py index 9050b109..9be4a1b8 100644 --- a/seminar/models.py +++ b/seminar/models.py @@ -396,6 +396,12 @@ class Problem(SeminarModelBase): def verejne_url(self): return reverse('seminar_problem', kwargs={'pk': self.id}) + def admin_url(self): + if self.stav == Problem.STAV_ZADANY: + return reverse('admin:seminar_problemzadany_change', args=(self.id, )) + else: + return reverse('admin:seminar_problemnavrh_change', args=(self.id, )) + @reversion.register(ignore_duplicate_revisions=True) @python_2_unicode_compatible