1 from django.contrib import admin
2 from django.contrib.contenttypes import generic
3 from django.contrib.contenttypes.models import ContentType
4 from django import forms
5 from django.conf import settings
6 from django.utils.translation import ugettext as _
7 from django.utils.safestring import mark_safe
8 from django.utils.html import escape
9 from django.utils.text import truncate_words
13 class AttributeInline(generic.GenericTabularInline):
14 ct_field = 'entity_content_type'
15 ct_fk_field = 'entity_object_id'
18 classes = ('collapse-closed',)
22 class RelationshipInline(generic.GenericTabularInline):
23 ct_field = 'entity_content_type'
24 ct_fk_field = 'entity_object_id'
27 classes = ('collapse-closed',)
31 class EntityAdmin(admin.ModelAdmin):
32 inlines = [AttributeInline, RelationshipInline]
36 class CollectionMemberInline(admin.TabularInline):
37 fk_name = 'collection'
38 model = CollectionMember
40 classes = ('collapse-closed',)
44 class CollectionAdmin(admin.ModelAdmin):
45 inlines = [CollectionMemberInline]
48 class NodeAdmin(EntityAdmin):
52 class ModelLookupWidget(forms.TextInput):
55 def __init__(self, content_type, attrs=None):
56 self.content_type = content_type
57 super(ModelLookupWidget, self).__init__(attrs)
59 def render(self, name, value, attrs=None):
60 related_url = '../../../%s/%s/' % (self.content_type.app_label, self.content_type.model)
63 if not attrs.has_key('class'):
64 attrs['class'] = 'vForeignKeyRawIdAdminField'
65 output = super(ModelLookupWidget, self).render(name, value, attrs)
66 output += '<a href="%s" class="related-lookup" id="lookup_id_%s" onclick="return showRelatedObjectLookupPopup(this);">' % (related_url, name)
67 output += '<img src="%simg/admin/selector-search.gif" width="16" height="16" alt="%s" />' % (settings.ADMIN_MEDIA_PREFIX, _('Lookup'))
70 value_class = self.content_type.model_class()
72 value_object = value_class.objects.get(pk=value)
73 output += ' <strong>%s</strong>' % escape(truncate_words(value_object, 14))
74 except value_class.DoesNotExist:
76 return mark_safe(output)
79 class RedirectAdmin(NodeAdmin):
82 'fields': ('slug', 'target', 'status_code')
84 ('URL/Tree/Hierarchy', {
85 'classes': ('collapse', 'collapse-closed'),
91 class FileAdmin(NodeAdmin):
92 prepopulated_fields = {'slug': ('file',)}
95 'fields': ('file', 'slug', 'mimetype')
97 ('URL/Tree/Hierarchy', {
98 'classes': ('collapse', 'collapse-closed'),
104 class PageAdmin(NodeAdmin):
105 add_form_template = 'admin/philo/page/add_form.html'
106 prepopulated_fields = {'slug': ('title',)}
109 'fields': ('title', 'slug', 'template')
111 ('URL/Tree/Hierarchy', {
112 'classes': ('collapse', 'collapse-closed'),
113 'fields': ('parent',)
116 list_display = ('title', 'path', 'template')
117 list_filter = ('template',)
118 search_fields = ['title', 'slug', 'contentlets__content']
120 def get_fieldsets(self, request, obj=None, **kwargs):
121 fieldsets = list(self.fieldsets)
122 if obj: # if no obj, creating a new page, thus no template set, thus no containers
124 template = page.template
125 contentlet_containers, contentreference_containers = template.containers
126 for container_name in contentlet_containers:
127 fieldsets.append((('Container: %s' % container_name), {
128 'fields': (('contentlet_container_content_%s' % container_name), ('contentlet_container_dynamic_%s' % container_name))
130 for container_name, container_content_type in contentreference_containers:
131 fieldsets.append((('Container: %s' % container_name), {
132 'fields': (('contentreference_container_%s' % container_name),)
136 def get_form(self, request, obj=None, **kwargs):
137 form = super(PageAdmin, self).get_form(request, obj, **kwargs)
138 if obj: # if no obj, creating a new page, thus no template set, thus no containers
140 template = page.template
141 contentlet_containers, contentreference_containers = template.containers
142 for container_name in contentlet_containers:
143 initial_content = None
144 initial_dynamic = False
146 contentlet = page.contentlets.get(name__exact=container_name)
147 initial_content = contentlet.content
148 initial_dynamic = contentlet.dynamic
149 except Contentlet.DoesNotExist:
151 form.base_fields[('contentlet_container_content_%s' % container_name)] = forms.CharField(label='Content', widget=forms.Textarea(), initial=initial_content, required=False)
152 form.base_fields[('contentlet_container_dynamic_%s' % container_name)] = forms.BooleanField(label='Dynamic', help_text='Specify whether this content contains dynamic template code', initial=initial_dynamic, required=False)
153 for container_name, container_content_type in contentreference_containers:
154 initial_content = None
156 initial_content = page.contentreferences.get(name__exact=container_name, content_type=container_content_type).content.pk
157 except ContentReference.DoesNotExist:
159 form.base_fields[('contentreference_container_%s' % container_name)] = forms.ModelChoiceField(label='References', widget=ModelLookupWidget(container_content_type), initial=initial_content, required=False, queryset=container_content_type.model_class().objects.all())
162 def save_model(self, request, page, form, change):
164 template = page.template
165 contentlet_containers, contentreference_containers = template.containers
166 for container_name in contentlet_containers:
167 if (('contentlet_container_content_%s' % container_name) in form.cleaned_data) and (('contentlet_container_dynamic_%s' % container_name) in form.cleaned_data):
168 content = form.cleaned_data[('contentlet_container_content_%s' % container_name)]
169 dynamic = form.cleaned_data[('contentlet_container_dynamic_%s' % container_name)]
170 contentlet, created = page.contentlets.get_or_create(name=container_name, defaults={'content': content, 'dynamic': dynamic})
172 contentlet.content = content
173 contentlet.dynamic = dynamic
175 for container_name, container_content_type in contentreference_containers:
176 if ('contentreference_container_%s' % container_name) in form.cleaned_data:
177 content = form.cleaned_data[('contentreference_container_%s' % container_name)]
178 contentreference, created = page.contentreferences.get_or_create(name=container_name, defaults={'content': content})
180 contentreference.content = content
181 contentreference.save()
184 class TemplateAdmin(admin.ModelAdmin):
185 prepopulated_fields = {'slug': ('name',)}
188 'fields': ('parent', 'name', 'slug')
191 'classes': ('collapse', 'collapse-closed'),
192 'fields': ('documentation',)
198 'classes': ('collapse','collapse-closed'),
199 'fields': ('mimetype',)
206 admin.site.register(Collection, CollectionAdmin)
207 admin.site.register(Redirect, RedirectAdmin)
208 admin.site.register(File, FileAdmin)
209 admin.site.register(Page, PageAdmin)
210 admin.site.register(Template, TemplateAdmin)