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
11 from django.core.exceptions import ValidationError, ObjectDoesNotExist
14 class AttributeInline(generic.GenericTabularInline):
15 ct_field = 'entity_content_type'
16 ct_fk_field = 'entity_object_id'
19 classes = ('collapse-closed',)
23 class RelationshipInline(generic.GenericTabularInline):
24 ct_field = 'entity_content_type'
25 ct_fk_field = 'entity_object_id'
28 classes = ('collapse-closed',)
32 class EntityAdmin(admin.ModelAdmin):
33 inlines = [AttributeInline, RelationshipInline]
37 class CollectionMemberInline(admin.TabularInline):
38 fk_name = 'collection'
39 model = CollectionMember
41 classes = ('collapse-closed',)
45 class CollectionAdmin(admin.ModelAdmin):
46 inlines = [CollectionMemberInline]
49 class TemplateAdmin(admin.ModelAdmin):
50 prepopulated_fields = {'slug': ('name',)}
53 'fields': ('parent', 'name', 'slug')
56 'classes': ('collapse', 'collapse-closed'),
57 'fields': ('documentation',)
63 'classes': ('collapse','collapse-closed'),
64 'fields': ('mimetype',)
69 list_display = ('__unicode__', 'slug', 'get_path',)
72 class ModelLookupWidget(forms.TextInput):
75 def __init__(self, content_type, attrs=None):
76 self.content_type = content_type
77 super(ModelLookupWidget, self).__init__(attrs)
79 def render(self, name, value, attrs=None):
80 related_url = '../../../%s/%s/' % (self.content_type.app_label, self.content_type.model)
83 if not attrs.has_key('class'):
84 attrs['class'] = 'vForeignKeyRawIdAdminField'
85 output = super(ModelLookupWidget, self).render(name, value, attrs)
86 output += '<a href="%s" class="related-lookup" id="lookup_id_%s" onclick="return showRelatedObjectLookupPopup(this);">' % (related_url, name)
87 output += '<img src="%simg/admin/selector-search.gif" width="16" height="16" alt="%s" />' % (settings.ADMIN_MEDIA_PREFIX, _('Lookup'))
90 value_class = self.content_type.model_class()
92 value_object = value_class.objects.get(pk=value)
93 output += ' <strong>%s</strong>' % escape(truncate_words(value_object, 14))
94 except value_class.DoesNotExist:
96 return mark_safe(output)
98 class NodeForm(forms.ModelForm):
100 def __init__(self, *args, **kwargs):
101 super(NodeForm, self).__init__(*args, **kwargs)
102 instance = self.instance
104 self.fields['parent'].queryset = instance.node_ptr.__class__.objects.exclude(id=instance.id)
105 except ObjectDoesNotExist:
109 cleaned_data = self.cleaned_data
110 parent = self.cleaned_data['parent']
111 self.instance.validate_parents(parent)
114 Node.objects.get(slug=self.cleaned_data['slug'], parent=parent)
115 raise ValidationError("A node with that path (parent and slug) already exists.")
116 except ObjectDoesNotExist:
121 class PageAdminForm(NodeForm):
125 class RedirectAdminForm(NodeForm):
129 class FileAdminForm(NodeForm):
133 class PageAdmin(EntityAdmin):
134 prepopulated_fields = {'slug': ('title',)}
137 'fields': ('title', 'template')
139 ('URL/Tree/Hierarchy', {
140 'classes': ('collapse', 'collapse-closed'),
141 'fields': ('parent', 'slug')
144 list_display = ('title', 'path', 'template')
145 list_filter = ('template',)
146 search_fields = ['title', 'slug', 'contentlets__content']
149 def get_fieldsets(self, request, obj=None, **kwargs):
150 fieldsets = list(self.fieldsets)
151 if obj: # if no obj, creating a new page, thus no template set, thus no containers
153 template = page.template
154 contentlet_containers, contentreference_containers = template.containers
155 for container_name in contentlet_containers:
156 fieldsets.append((('Container: %s' % container_name), {
157 'fields': (('contentlet_container_content_%s' % container_name), ('contentlet_container_dynamic_%s' % container_name))
159 for container_name, container_content_type in contentreference_containers:
160 fieldsets.append((('Container: %s' % container_name), {
161 'fields': (('contentreference_container_%s' % container_name),)
165 def get_form(self, request, obj=None, **kwargs):
166 form = super(PageAdmin, self).get_form(request, obj, **kwargs)
167 if obj: # if no obj, creating a new page, thus no template set, thus no containers
169 template = page.template
170 contentlet_containers, contentreference_containers = template.containers
171 for container_name in contentlet_containers:
172 initial_content = None
173 initial_dynamic = False
175 contentlet = page.contentlets.get(name__exact=container_name)
176 initial_content = contentlet.content
177 initial_dynamic = contentlet.dynamic
178 except Contentlet.DoesNotExist:
180 form.base_fields[('contentlet_container_content_%s' % container_name)] = forms.CharField(label='Content', widget=forms.Textarea(), initial=initial_content, required=False)
181 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)
182 for container_name, container_content_type in contentreference_containers:
183 initial_content = None
185 initial_content = page.contentreferences.get(name__exact=container_name, content_type=container_content_type).content.pk
186 except ContentReference.DoesNotExist:
188 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())
191 def save_model(self, request, page, form, change):
193 template = page.template
194 contentlet_containers, contentreference_containers = template.containers
195 for container_name in contentlet_containers:
196 if (('contentlet_container_content_%s' % container_name) in form.cleaned_data) and (('contentlet_container_dynamic_%s' % container_name) in form.cleaned_data):
197 content = form.cleaned_data[('contentlet_container_content_%s' % container_name)]
198 dynamic = form.cleaned_data[('contentlet_container_dynamic_%s' % container_name)]
199 contentlet, created = page.contentlets.get_or_create(name=container_name, defaults={'content': content, 'dynamic': dynamic})
201 contentlet.content = content
202 contentlet.dynamic = dynamic
204 for container_name, container_content_type in contentreference_containers:
205 if ('contentreference_container_%s' % container_name) in form.cleaned_data:
206 content = form.cleaned_data[('contentreference_container_%s' % container_name)]
207 contentreference, created = page.contentreferences.get_or_create(name=container_name, defaults={'content': content})
209 contentreference.content = content
210 contentreference.save()
212 class RedirectAdmin(admin.ModelAdmin):
213 list_display=('slug', 'target', 'path', 'status_code',)
214 list_filter=('status_code',)
215 form = RedirectAdminForm
217 class FileAdmin(admin.ModelAdmin):
219 list_display=('slug', 'mimetype', 'path', 'file',)
221 admin.site.register(Collection, CollectionAdmin)
222 admin.site.register(Redirect, RedirectAdmin)
223 admin.site.register(File, FileAdmin)
224 admin.site.register(Page, PageAdmin)
225 admin.site.register(Template, TemplateAdmin)