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
8 class AttributeInline(generic.GenericTabularInline):
9 ct_field = 'entity_content_type'
10 ct_fk_field = 'entity_object_id'
13 classes = ('collapse-closed',)
17 class RelationshipInline(generic.GenericTabularInline):
18 ct_field = 'entity_content_type'
19 ct_fk_field = 'entity_object_id'
22 classes = ('collapse-closed',)
26 class EntityAdmin(admin.ModelAdmin):
27 inlines = [AttributeInline, RelationshipInline]
31 class CollectionMemberInline(admin.TabularInline):
32 fk_name = 'collection'
33 model = CollectionMember
35 classes = ('collapse-closed',)
39 class CollectionAdmin(admin.ModelAdmin):
40 inlines = [CollectionMemberInline]
43 class TemplateAdmin(admin.ModelAdmin):
44 prepopulated_fields = {'slug': ('name',)}
47 'fields': ('parent', 'name', 'slug')
50 'classes': ('collapse', 'collapse-closed'),
51 'fields': ('documentation',)
57 'classes': ('collapse','collapse-closed'),
58 'fields': ('mimetype',)
65 class PageAdmin(EntityAdmin):
66 prepopulated_fields = {'slug': ('title',)}
69 'fields': ('title', 'template')
71 ('URL/Tree/Hierarchy', {
72 'classes': ('collapse', 'collapse-closed'),
73 'fields': ('parent', 'slug')
76 list_display = ('title', 'path', 'template')
77 list_filter = ('template',)
78 search_fields = ['title', 'slug', 'contentlets__content']
80 def get_fieldsets(self, request, obj=None, **kwargs):
81 fieldsets = list(self.fieldsets)
82 if obj: # if no obj, creating a new page, thus no template set, thus no containers
84 template = page.template
85 containers = template.containers
86 if len(containers) > 0:
87 for container in containers:
88 fieldsets.append((('Container: %s' % container), {
89 'fields': (('container_content_%s' % container), ('container_dynamic_%s' % container))
93 def get_form(self, request, obj=None, **kwargs):
94 form = super(PageAdmin, self).get_form(request, obj, **kwargs)
95 if obj: # if no obj, creating a new page, thus no template set, thus no containers
97 template = page.template
98 containers = template.containers
99 for container in containers:
100 initial_content = None
101 initial_dynamic = False
103 contentlet = page.contentlets.get(name__exact=container)
104 initial_content = contentlet.content
105 initial_dynamic = contentlet.dynamic
106 except Contentlet.DoesNotExist:
108 form.base_fields[('container_content_%s' % container)] = forms.CharField(label='Content', widget=forms.Textarea(), initial=initial_content, required=False)
109 form.base_fields[('container_dynamic_%s' % container)] = forms.BooleanField(label='Dynamic', help_text='Specify whether this content contains dynamic template code', initial=initial_dynamic, required=False)
112 def save_model(self, request, page, form, change):
115 template = page.template
116 containers = template.containers
117 for container in containers:
118 if (("container_content_%s" % container) in form.cleaned_data) and (("container_dynamic_%s" % container) in form.cleaned_data):
119 content = form.cleaned_data[('container_content_%s' % container)]
120 dynamic = form.cleaned_data[('container_dynamic_%s' % container)]
121 contentlet, created = page.contentlets.get_or_create(name=container, defaults={'content': content, 'dynamic': dynamic})
123 contentlet.content = content
124 contentlet.dynamic = dynamic
128 admin.site.register(Collection, CollectionAdmin)
129 admin.site.register(Page, PageAdmin)
130 admin.site.register(Template, TemplateAdmin)