lots of graphics
[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.contrib import admin
6 from django.utils.translation import ugettext_lazy as _
7 from migdal.models import Category, Entry, Attachment
8 from migdal import settings
9
10
11 def translated_fields(field_names, languages=settings.LANGUAGES):
12     return tuple("%s_%s" % (field_name, lang_code)
13                 for field_name in field_names
14                 for lang_code, lang_name in languages
15                 )
16
17
18 class AttachmentInline(admin.TabularInline):
19     model = Attachment
20     readonly_fields = ['url']
21
22
23 class EntryAdmin(admin.ModelAdmin):
24     fieldsets = (
25         (None, {'fields': (('type', 'promo'), 'author', 'author_email', 'image')}),
26     ) + tuple(
27         (ln, {'fields': (
28             ('published_%s' % lc),
29             'title_%s' % lc,
30             'slug_%s' % lc,
31             'lead_%s' % lc,
32             'body_%s' % lc,
33             )})
34         for lc, ln in settings.OBLIGATORY_LANGUAGES
35     ) + tuple(
36         (ln, {'fields': (
37             ('needed_%s' % lc, 'published_%s' % lc),
38             'title_%s' % lc,
39             'slug_%s' % lc,
40             'lead_%s' % lc,
41             'body_%s' % lc,
42             )})
43         for lc, ln in settings.OPTIONAL_LANGUAGES
44     ) + (
45         (_('Categories'), {'fields': ('categories',)}),
46     )
47     prepopulated_fields = dict([
48             ("slug_%s" % lang_code, ("title_%s" % lang_code,))
49             for lang_code, lang_name in settings.LANGUAGES
50         ]) 
51
52     list_display = translated_fields(('title',), settings.OBLIGATORY_LANGUAGES
53             ) + ('type', 'date', 'author'
54             ) + translated_fields(('published',)
55             ) + translated_fields(('needed',), settings.OPTIONAL_LANGUAGES)
56     list_filter = ('type',) + translated_fields(('published',)
57             ) + translated_fields(('needed',), settings.OPTIONAL_LANGUAGES)
58     inlines = (AttachmentInline,)
59
60
61 class CategoryAdmin(admin.ModelAdmin):
62     list_display = translated_fields(('title', 'slug'))
63     prepopulated_fields = dict([
64             ("slug_%s" % lang_code, ("title_%s" % lang_code,))
65             for lang_code, lang_name in settings.LANGUAGES
66         ]) 
67
68
69 admin.site.register(Entry, EntryAdmin)
70 admin.site.register(Category, CategoryAdmin)