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
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 def filtered_entry_admin(typ):
20 class EntryAdmin(admin.ModelAdmin):
21 def queryset(self, request):
22 return self.model.objects.filter(type=typ.db)
24 def has_add_permission(self, request):
25 return request.user.has_perm('migdal.add_entry')
27 def has_change_permission(self, request, obj=None):
28 return request.user.has_perm('migdal.change_entry')
30 def has_delete_permission(self, request, obj=None):
31 return request.user.has_perm('migdal.delete_entry')
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'
39 date_hierarchy = 'date'
40 readonly_fields = ('date', 'changed_at') + \
41 translated_fields(('published_at',))
42 _promo_if_necessary = ('promo',) if typ.promotable else ()
46 'fields': _promo_if_necessary + (
47 'author', 'author_email', 'image', 'date', 'changed_at')
51 ('published_%s' % lc),
52 'published_at_%s' % lc,
58 for lc, ln in app_settings.OBLIGATORY_LANGUAGES
61 ('needed_%s' % lc, 'published_%s' % lc),
62 'published_at_%s' % lc,
68 for lc, ln in app_settings.OPTIONAL_LANGUAGES
73 (_('Categories'), {'fields': ('categories',)}),
75 prepopulated_fields = dict([
76 ("slug_%s" % lang_code, ("title_%s" % lang_code,))
77 for lang_code, lang_name in settings.LANGUAGES
80 list_display = translated_fields(('title',),
81 app_settings.OBLIGATORY_LANGUAGES) + \
82 ('date', 'author') + \
83 _promo_if_necessary + \
84 translated_fields(('published_at',)) + \
85 translated_fields(('needed',), app_settings.OPTIONAL_LANGUAGES)
86 list_filter = _promo_if_necessary + \
87 translated_fields(('published',)) + \
88 translated_fields(('needed',), app_settings.OPTIONAL_LANGUAGES)
89 inlines = (AttachmentInline,)
90 search_fields = ('title_pl', 'title_en')
94 for typ in app_settings.TYPES:
95 newmodel = filtered_model("Entry_%s" % typ.db, Entry, 'type', typ.db, typ.slug)
96 admin.site.register(newmodel, filtered_entry_admin(typ))
99 if app_settings.TAXONOMIES:
100 from migdal.models import Category
102 class CategoryAdmin(admin.ModelAdmin):
103 list_display = translated_fields(('title', 'slug')) + ('taxonomy',)
104 prepopulated_fields = dict([
105 ("slug_%s" % lang_code, ("title_%s" % lang_code,))
106 for lang_code, lang_name in settings.LANGUAGES
108 admin.site.register(Category, CategoryAdmin)