cf1c514fb2e3a5eb8b847b72b2779f7b9ddbd8da
[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 get_queryset(self, request):
22             return self.model.objects.filter(type=typ.db)
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         def formfield_for_dbfield(self, db_field, **kwargs):
34             field = super(EntryAdmin, self).formfield_for_dbfield(db_field, **kwargs)
35             if db_field.name == 'categories':
36                 field.widget.attrs['style'] = 'height: 10em'
37             return field
38
39         date_hierarchy = 'date'
40         readonly_fields = ('date', 'changed_at') + translated_fields(('published_at',))
41         if app_settings.PUBLISH_DATE_EDITABLE:
42             readonly_fields += ('first_published_at',)
43         _promo_if_necessary = ('promo',) if typ.promotable else ()
44
45         fieldsets = (
46             (None, {
47                 'fields': _promo_if_necessary + (
48                     'in_stream', 'author', 'author_email', 'canonical_url', 'image',
49                     'date', 'first_published_at', 'changed_at', 'gallery')
50                 }),
51         ) + tuple(
52             (ln, {'fields': (
53                 ('published_%s' % lc),
54                 'published_at_%s' % lc,
55                 'title_%s' % lc,
56                 'slug_%s' % lc,
57                 'lead_%s' % lc,
58                 'body_%s' % lc,
59                 'place_%s' % lc,
60                 'time_%s' % lc,
61                 )})
62             for lc, ln in app_settings.OBLIGATORY_LANGUAGES
63         ) + tuple(
64             (ln, {'fields': (
65                 ('needed_%s' % lc, 'published_%s' % lc),
66                 'published_at_%s' % lc,
67                 'title_%s' % lc,
68                 'slug_%s' % lc,
69                 'lead_%s' % lc,
70                 'body_%s' % lc,
71                 'place_%s' % lc,
72                 'time_%s' % lc,
73                 )})
74             for lc, ln in app_settings.OPTIONAL_LANGUAGES
75         )
76
77         if typ.categorized:
78             fieldsets += (
79                 (_('Categories'), {'fields': ('categories',)}),
80             )
81         prepopulated_fields = dict([
82                 ("slug_%s" % lang_code, ("title_%s" % lang_code,))
83                 for lang_code, lang_name in settings.LANGUAGES
84             ]) 
85
86         list_display = translated_fields(('title',), app_settings.OBLIGATORY_LANGUAGES) + \
87             ('date', 'author') + \
88             _promo_if_necessary + \
89             ('in_stream', 'first_published_at',) + \
90             translated_fields(('published_at',)) + \
91             translated_fields(('needed',), app_settings.OPTIONAL_LANGUAGES)
92         list_filter = _promo_if_necessary + \
93             translated_fields(('published',)) + \
94             translated_fields(('needed',), app_settings.OPTIONAL_LANGUAGES)
95         inlines = (AttachmentInline,)
96         search_fields = ('title_pl', 'title_en')
97     return EntryAdmin
98
99
100 for typ in app_settings.TYPES:
101     newmodel = filtered_model("Entry_%s" % typ.db, Entry, 'type', typ.db, typ.slug)
102     admin.site.register(newmodel, filtered_entry_admin(typ))
103
104
105 if app_settings.TAXONOMIES:
106     from migdal.models import Category
107
108     class CategoryAdmin(admin.ModelAdmin):
109         list_display = translated_fields(('title', 'slug')) + ('taxonomy',)
110         prepopulated_fields = dict([
111                 ("slug_%s" % lang_code, ("title_%s" % lang_code,))
112                 for lang_code, lang_name in settings.LANGUAGES
113             ]) 
114     admin.site.register(Category, CategoryAdmin)