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
10 from philo.models import *
11 from django.core.exceptions import ValidationError, ObjectDoesNotExist
12 from validators import TreeParentValidator, TreePositionValidator
15 class AttributeInline(generic.GenericTabularInline):
16 ct_field = 'entity_content_type'
17 ct_fk_field = 'entity_object_id'
20 classes = ('collapse-closed',)
24 class RelationshipInline(generic.GenericTabularInline):
25 ct_field = 'entity_content_type'
26 ct_fk_field = 'entity_object_id'
29 classes = ('collapse-closed',)
33 class EntityAdmin(admin.ModelAdmin):
34 inlines = [AttributeInline, RelationshipInline]
38 class CollectionMemberInline(admin.TabularInline):
39 fk_name = 'collection'
40 model = CollectionMember
42 classes = ('collapse-closed',)
44 fields = ('member_content_type', 'member_object_id', 'index',)
47 class CollectionAdmin(admin.ModelAdmin):
48 inlines = [CollectionMemberInline]
49 list_display = ('name', 'description', 'get_count')
52 class TreeForm(forms.ModelForm):
53 def __init__(self, *args, **kwargs):
54 super(TreeForm, self).__init__(*args, **kwargs)
55 instance = self.instance
56 instance_class=self.get_instance_class()
58 if instance_class is not None:
60 self.fields['parent'].queryset = instance_class.objects.exclude(id=instance.id)
61 except ObjectDoesNotExist:
64 self.fields['parent'].validators = [TreeParentValidator(*self.get_validator_args())]
66 def get_instance_class(self):
67 return self.instance.__class__
69 def get_validator_args(self):
70 return [self.instance]
73 cleaned_data = self.cleaned_data
76 parent = cleaned_data['parent']
77 slug = cleaned_data['slug']
78 obj_class = self.get_instance_class()
79 tpv = TreePositionValidator(parent, slug, obj_class)
87 class TemplateAdmin(admin.ModelAdmin):
88 prepopulated_fields = {'slug': ('name',)}
91 'fields': ('parent', 'name', 'slug')
94 'classes': ('collapse', 'collapse-closed'),
95 'fields': ('documentation',)
101 'classes': ('collapse','collapse-closed'),
102 'fields': ('mimetype',)
107 list_display = ('__unicode__', 'slug', 'get_path',)
111 class ModelLookupWidget(forms.TextInput):
114 def __init__(self, content_type, attrs=None):
115 self.content_type = content_type
116 super(ModelLookupWidget, self).__init__(attrs)
118 def render(self, name, value, attrs=None):
119 related_url = '../../../%s/%s/' % (self.content_type.app_label, self.content_type.model)
122 if not attrs.has_key('class'):
123 attrs['class'] = 'vForeignKeyRawIdAdminField'
124 output = super(ModelLookupWidget, self).render(name, value, attrs)
125 output += '<a href="%s" class="related-lookup" id="lookup_id_%s" onclick="return showRelatedObjectLookupPopup(this);">' % (related_url, name)
126 output += '<img src="%simg/admin/selector-search.gif" width="16" height="16" alt="%s" />' % (settings.ADMIN_MEDIA_PREFIX, _('Lookup'))
129 value_class = self.content_type.model_class()
131 value_object = value_class.objects.get(pk=value)
132 output += ' <strong>%s</strong>' % escape(truncate_words(value_object, 14))
133 except value_class.DoesNotExist:
135 return mark_safe(output)
138 class NodeForm(TreeForm):
139 def get_instance_class(self):
142 def get_validator_args(self):
143 return [self.instance, 'instance']
146 class PageAdminForm(NodeForm):
151 class RedirectAdminForm(NodeForm):
156 class FileAdminForm(NodeForm):
161 class PageAdmin(EntityAdmin):
162 prepopulated_fields = {'slug': ('title',)}
165 'fields': ('title', 'template')
167 ('URL/Tree/Hierarchy', {
168 'classes': ('collapse', 'collapse-closed'),
169 'fields': ('parent', 'slug')
172 list_display = ('title', 'path', 'template')
173 list_filter = ('template',)
174 search_fields = ['title', 'slug', 'contentlets__content']
177 def get_fieldsets(self, request, obj=None, **kwargs):
178 fieldsets = list(self.fieldsets)
179 if obj: # if no obj, creating a new page, thus no template set, thus no containers
181 template = page.template
182 contentlet_containers, contentreference_containers = template.containers
183 for container_name in contentlet_containers:
184 fieldsets.append((('Container: %s' % container_name), {
185 'fields': (('contentlet_container_content_%s' % container_name), ('contentlet_container_dynamic_%s' % container_name))
187 for container_name, container_content_type in contentreference_containers:
188 fieldsets.append((('Container: %s' % container_name), {
189 'fields': (('contentreference_container_%s' % container_name),)
193 def get_form(self, request, obj=None, **kwargs):
194 form = super(PageAdmin, self).get_form(request, obj, **kwargs)
195 if obj: # if no obj, creating a new page, thus no template set, thus no containers
197 template = page.template
198 contentlet_containers, contentreference_containers = template.containers
199 for container_name in contentlet_containers:
200 initial_content = None
201 initial_dynamic = False
203 contentlet = page.contentlets.get(name__exact=container_name)
204 initial_content = contentlet.content
205 initial_dynamic = contentlet.dynamic
206 except Contentlet.DoesNotExist:
208 form.base_fields[('contentlet_container_content_%s' % container_name)] = forms.CharField(label='Content', widget=forms.Textarea(), initial=initial_content, required=False)
209 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)
210 for container_name, container_content_type in contentreference_containers:
211 initial_content = None
213 initial_content = page.contentreferences.get(name__exact=container_name, content_type=container_content_type).content.pk
214 except ContentReference.DoesNotExist:
216 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())
219 def save_model(self, request, page, form, change):
221 template = page.template
222 contentlet_containers, contentreference_containers = template.containers
223 for container_name in contentlet_containers:
224 if (('contentlet_container_content_%s' % container_name) in form.cleaned_data) and (('contentlet_container_dynamic_%s' % container_name) in form.cleaned_data):
225 content = form.cleaned_data[('contentlet_container_content_%s' % container_name)]
226 dynamic = form.cleaned_data[('contentlet_container_dynamic_%s' % container_name)]
227 contentlet, created = page.contentlets.get_or_create(name=container_name, defaults={'content': content, 'dynamic': dynamic})
229 contentlet.content = content
230 contentlet.dynamic = dynamic
232 for container_name, container_content_type in contentreference_containers:
233 if ('contentreference_container_%s' % container_name) in form.cleaned_data:
234 content = form.cleaned_data[('contentreference_container_%s' % container_name)]
237 contentreference, created = page.contentreferences.get_or_create(name=container_name, defaults={'content': content})
239 contentreference.content = content
240 contentreference.save()
243 class RedirectAdmin(admin.ModelAdmin):
244 list_display=('slug', 'target', 'path', 'status_code',)
245 list_filter=('status_code',)
246 form = RedirectAdminForm
249 class FileAdmin(admin.ModelAdmin):
251 list_display=('slug', 'mimetype', 'path', 'file',)
254 admin.site.register(Collection, CollectionAdmin)
255 admin.site.register(Redirect, RedirectAdmin)
256 admin.site.register(File, FileAdmin)
257 admin.site.register(Page, PageAdmin)
258 admin.site.register(Template, TemplateAdmin)