initial commit
[django-migdal.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 fnpdjango.utils.models import filtered_model
11 from fnpdjango.utils.models.translation import translated_fields
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         def has_add_permission(self, request):
25             return request.user.has_perm('migdal.add_entry')
26
27         def has_change_permission(self, request, obj=None):
28             return request.user.has_perm('migdal.change_entry')
29
30         def has_delete_permission(self, request, obj=None):
31             return request.user.has_perm('migdal.delete_entry')
32
33         date_hierarchy = 'date'
34         readonly_fields = ('date', 'changed_at') + \
35             translated_fields(('published_at',))
36         _promo_if_necessary = ('promo',) if typ.promotable else ()
37
38         fieldsets = (
39             (None, {
40                 'fields': _promo_if_necessary + (
41                     'author', 'author_email', 'image', 'date', 'changed_at')
42                 }),
43         ) + tuple(
44             (ln, {'fields': (
45                 ('published_%s' % lc),
46                 'published_at_%s' % lc,
47                 'title_%s' % lc,
48                 'slug_%s' % lc,
49                 'lead_%s' % lc,
50                 'body_%s' % lc,
51                 )})
52             for lc, ln in app_settings.OBLIGATORY_LANGUAGES
53         ) + tuple(
54             (ln, {'fields': (
55                 ('needed_%s' % lc, 'published_%s' % lc),
56                 'published_at_%s' % lc,
57                 'title_%s' % lc,
58                 'slug_%s' % lc,
59                 'lead_%s' % lc,
60                 'body_%s' % lc,
61                 )})
62             for lc, ln in app_settings.OPTIONAL_LANGUAGES
63         )
64
65         if typ.categorized:
66             fieldsets += (
67                 (_('Categories'), {'fields': ('categories',)}),
68             )
69         prepopulated_fields = dict([
70                 ("slug_%s" % lang_code, ("title_%s" % lang_code,))
71                 for lang_code, lang_name in settings.LANGUAGES
72             ]) 
73
74         list_display = translated_fields(('title',),
75                             app_settings.OBLIGATORY_LANGUAGES) + \
76                 ('date', 'author') + \
77                 _promo_if_necessary + \
78                 translated_fields(('published_at',)) + \
79                 translated_fields(('needed',), app_settings.OPTIONAL_LANGUAGES)
80         list_filter = _promo_if_necessary + \
81                 translated_fields(('published',)) + \
82                 translated_fields(('needed',), app_settings.OPTIONAL_LANGUAGES)
83         inlines = (AttachmentInline,)
84         search_fields = ('title_pl', 'title_en')
85     return EntryAdmin
86
87
88 for typ in app_settings.TYPES:
89     newmodel = filtered_model("Entry_%s" % typ.db, Entry, 'type', typ.db, typ.slug)
90     admin.site.register(newmodel, filtered_entry_admin(typ))
91
92
93 if app_settings.TAXONOMIES:
94     from migdal.models import Category
95
96     class CategoryAdmin(admin.ModelAdmin):
97         list_display = translated_fields(('title', 'slug')) + ('taxonomy',)
98         prepopulated_fields = dict([
99                 ("slug_%s" % lang_code, ("title_%s" % lang_code,))
100                 for lang_code, lang_name in settings.LANGUAGES
101             ]) 
102     admin.site.register(Category, CategoryAdmin)