Contact forms app.
[prawokultury.git] / migdal / admin.py
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.
4 #
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
12
13
14 class AttachmentInline(admin.TabularInline):
15     model = Attachment
16     readonly_fields = ['url']
17
18
19 def filtered_entry_admin(typ):
20     class EntryAdmin(admin.ModelAdmin):
21         def queryset(self, request):
22             return self.model.objects.filter(type=typ)
23
24         date_hierarchy = 'date'
25         readonly_fields = ('date', 'changed_at') + \
26             translated_fields(('published_at',))
27         _promo_if_necessary = ('promo',) if typ.promotable else ()
28
29         fieldsets = (
30             (None, {
31                 'fields': _promo_if_necessary + (
32                     'author', 'author_email', 'image', 'date', 'changed_at')
33                 }),
34         ) + tuple(
35             (ln, {'fields': (
36                 ('published_%s' % lc),
37                 'published_at_%s' % lc,
38                 'title_%s' % lc,
39                 'slug_%s' % lc,
40                 'lead_%s' % lc,
41                 'body_%s' % lc,
42                 )})
43             for lc, ln in app_settings.OBLIGATORY_LANGUAGES
44         ) + tuple(
45             (ln, {'fields': (
46                 ('needed_%s' % lc, 'published_%s' % lc),
47                 'published_at_%s' % lc,
48                 'title_%s' % lc,
49                 'slug_%s' % lc,
50                 'lead_%s' % lc,
51                 'body_%s' % lc,
52                 )})
53             for lc, ln in app_settings.OPTIONAL_LANGUAGES
54         )
55
56         if typ.categorized:
57             fieldsets += (
58                 (_('Categories'), {'fields': ('categories',)}),
59             )
60         prepopulated_fields = dict([
61                 ("slug_%s" % lang_code, ("title_%s" % lang_code,))
62                 for lang_code, lang_name in settings.LANGUAGES
63             ]) 
64
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')
76     return EntryAdmin
77
78
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))
82
83
84 if app_settings.TAXONOMIES:
85     from migdal.models import Category
86
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
92             ]) 
93     admin.site.register(Category, CategoryAdmin)