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.
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
14 class AttachmentInline(admin.TabularInline):
16 readonly_fields = ['url']
19 class PhotoInline(admin.TabularInline):
21 readonly_fields = ['url']
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)
29 def has_add_permission(self, request):
30 return request.user.has_perm('migdal.add_entry')
32 def has_change_permission(self, request, obj=None):
33 return request.user.has_perm('migdal.change_entry')
35 def has_delete_permission(self, request, obj=None):
36 return request.user.has_perm('migdal.delete_entry')
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'
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 ()
52 'fields': _promo_if_necessary + (
53 'in_stream', 'author', 'author_email', 'canonical_url', 'image',
54 'date', 'first_published_at', 'changed_at')
58 ('published_%s' % lc),
59 'published_at_%s' % lc,
67 for lc, ln in app_settings.OBLIGATORY_LANGUAGES
70 ('needed_%s' % lc, 'published_%s' % lc),
71 'published_at_%s' % lc,
79 for lc, ln in app_settings.OPTIONAL_LANGUAGES
84 (_('Categories'), {'fields': ('categories',)}),
86 prepopulated_fields = dict([
87 ("slug_%s" % lang_code, ("title_%s" % lang_code,))
88 for lang_code, lang_name in settings.LANGUAGES
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')
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))
110 if app_settings.TAXONOMIES:
111 from migdal.models import Category
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
119 admin.site.register(Category, CategoryAdmin)