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 contentlet_containers, contentreference_containers = template.containers
86 for container_name in contentlet_containers:
87 fieldsets.append((('Container: %s' % container_name), {
88 'fields': (('contentlet_container_content_%s' % container_name), ('contentlet_container_dynamic_%s' % container_name))
90 for container_name, container_content_type in contentreference_containers:
91 fieldsets.append((('Container: %s' % container_name), {
92 'fields': (('contentreference_container_%s' % container_name),)
96 def get_form(self, request, obj=None, **kwargs):
97 form = super(PageAdmin, self).get_form(request, obj, **kwargs)
98 if obj: # if no obj, creating a new page, thus no template set, thus no containers
100 template = page.template
101 contentlet_containers, contentreference_containers = template.containers
102 for container_name in contentlet_containers:
103 initial_content = None
104 initial_dynamic = False
106 contentlet = page.contentlets.get(name__exact=container_name)
107 initial_content = contentlet.content
108 initial_dynamic = contentlet.dynamic
109 except Contentlet.DoesNotExist:
111 form.base_fields[('contentlet_container_content_%s' % container_name)] = forms.CharField(label='Content', widget=forms.Textarea(), initial=initial_content, required=False)
112 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)
113 for container_name, container_content_type in contentreference_containers:
114 initial_content = None
116 initial_content = page.contentreferences.get(name__exact=container_name, content_type=container_content_type)
117 except ContentReference.DoesNotExist:
119 form.base_fields[('contentreference_container_%s' % container_name)] = forms.ModelChoiceField(label='References', initial=initial_content, required=False, queryset=container_content_type.model_class().objects.all())
122 def save_model(self, request, page, form, change):
124 template = page.template
125 contentlet_containers, contentreference_containers = template.containers
126 for container_name in contentlet_containers:
127 if (('contentlet_container_content_%s' % container_name) in form.cleaned_data) and (('contentlet_container_dynamic_%s' % container_name) in form.cleaned_data):
128 content = form.cleaned_data[('contentlet_container_content_%s' % container_name)]
129 dynamic = form.cleaned_data[('contentlet_container_dynamic_%s' % container_name)]
130 contentlet, created = page.contentlets.get_or_create(name=container_name, defaults={'content': content, 'dynamic': dynamic})
132 contentlet.content = content
133 contentlet.dynamic = dynamic
135 for container_name, container_content_type in contentreference_containers:
136 if ('contentreference_container_%s' % container_name) in form.cleaned_data:
137 content = form.cleaned_data[('contentreference_container_%s' % container_name)]
138 contentreference, created = page.contentreferences.get_or_create(name=container_name, defaults={'content': content})
140 contentreference.content = content
141 contentreference.save()
144 admin.site.register(Collection, CollectionAdmin)
145 admin.site.register(Redirect)
146 admin.site.register(File)
147 admin.site.register(Page, PageAdmin)
148 admin.site.register(Template, TemplateAdmin)