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