1 # -*- coding: utf-8 -*-
2 # This file is part of PrawoKultury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from django.conf import settings
6 from django.contrib import admin
7 from django.utils.translation import ugettext_lazy as _
8 from migdal.models import Entry, Attachment
9 from migdal import app_settings
10 from migdal.helpers import translated_fields
11 from prawokultury.model_helpers import filtered_model
14 class AttachmentInline(admin.TabularInline):
16 readonly_fields = ['url']
19 def filtered_entry_admin(typ):
20 class EntryAdmin(admin.ModelAdmin):
21 def queryset(self, request):
22 return self.model.objects.filter(type=typ)
24 date_hierarchy = 'date'
25 readonly_fields = ('date', 'changed_at') + \
26 translated_fields(('published_at',))
27 _promo_if_necessary = ('promo',) if typ.promotable else ()
31 'fields': _promo_if_necessary + (
32 'author', 'author_email', 'image', 'date', 'changed_at')
36 ('published_%s' % lc),
37 'published_at_%s' % lc,
43 for lc, ln in app_settings.OBLIGATORY_LANGUAGES
46 ('needed_%s' % lc, 'published_%s' % lc),
47 'published_at_%s' % lc,
53 for lc, ln in app_settings.OPTIONAL_LANGUAGES
58 (_('Categories'), {'fields': ('categories',)}),
60 prepopulated_fields = dict([
61 ("slug_%s" % lang_code, ("title_%s" % lang_code,))
62 for lang_code, lang_name in settings.LANGUAGES
65 list_display = translated_fields(('title',),
66 app_settings.OBLIGATORY_LANGUAGES) + \
67 ('date', 'author') + \
68 _promo_if_necessary + \
69 translated_fields(('published_at',)) + \
70 translated_fields(('needed',), app_settings.OPTIONAL_LANGUAGES)
71 list_filter = _promo_if_necessary + \
72 translated_fields(('published',)) + \
73 translated_fields(('needed',), app_settings.OPTIONAL_LANGUAGES)
74 inlines = (AttachmentInline,)
75 search_fields = ('title_pl', 'title_en')
79 for typ in app_settings.TYPES:
80 newmodel = filtered_model("Entry_%s" % typ.db, Entry, 'type', typ.db, typ.slug)
81 admin.site.register(newmodel, filtered_entry_admin(typ))
84 if app_settings.TAXONOMIES:
85 from migdal.models import Category
87 class CategoryAdmin(admin.ModelAdmin):
88 list_display = translated_fields(('title', 'slug')) + ('taxonomy',)
89 prepopulated_fields = dict([
90 ("slug_%s" % lang_code, ("title_%s" % lang_code,))
91 for lang_code, lang_name in settings.LANGUAGES
93 admin.site.register(Category, CategoryAdmin)