1 from django.contrib import admin
2 from django import forms
3 from philo.admin import widgets
4 from philo.admin.base import COLLAPSE_CLASSES
5 from philo.admin.nodes import ViewAdmin
6 from philo.models.pages import Page, Template, Contentlet, ContentReference
9 class PageAdmin(ViewAdmin):
10 add_form_template = 'admin/philo/page/add_form.html'
13 'fields': ('title', 'template')
16 list_display = ('title', 'template')
17 list_filter = ('template',)
18 search_fields = ['title', 'contentlets__content']
20 def get_fieldsets(self, request, obj=None, **kwargs):
21 fieldsets = list(self.fieldsets)
22 if obj: # if no obj, creating a new page, thus no template set, thus no containers
23 template = obj.template
24 if template.documentation:
25 fieldsets.append(('Template Documentation', {
26 'description': template.documentation
28 contentlet_containers, contentreference_containers = template.containers
29 for container_name in contentlet_containers:
30 fieldsets.append((('Container: %s' % container_name), {
31 'fields': (('contentlet_container_content_%s' % container_name), ('contentlet_container_dynamic_%s' % container_name)),
32 'classes': ['monospace']
34 for container_name, container_content_type in contentreference_containers:
35 fieldsets.append((('Container: %s' % container_name), {
36 'fields': (('contentreference_container_%s' % container_name),)
40 def get_form(self, request, obj=None, **kwargs):
41 form = super(PageAdmin, self).get_form(request, obj, **kwargs)
42 if obj: # if no obj, creating a new page, thus no template set, thus no containers
44 template = page.template
45 contentlet_containers, contentreference_containers = template.containers
46 for container_name in contentlet_containers:
47 initial_content = None
48 initial_dynamic = False
50 contentlet = page.contentlets.get(name__exact=container_name)
51 initial_content = contentlet.content
52 initial_dynamic = contentlet.dynamic
53 except Contentlet.DoesNotExist:
55 form.base_fields[('contentlet_container_content_%s' % container_name)] = forms.CharField(label='Content', widget=forms.Textarea(), initial=initial_content, required=False)
56 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)
57 for container_name, container_content_type in contentreference_containers:
58 initial_content = None
60 initial_content = page.contentreferences.get(name__exact=container_name, content_type=container_content_type).content.pk
61 except (ContentReference.DoesNotExist, AttributeError):
63 form.base_fields[('contentreference_container_%s' % container_name)] = forms.ModelChoiceField(label='References', widget=widgets.ModelLookupWidget(container_content_type), initial=initial_content, required=False, queryset=container_content_type.model_class().objects.all())
66 def save_model(self, request, page, form, change):
68 template = page.template
69 contentlet_containers, contentreference_containers = template.containers
70 for container_name in contentlet_containers:
71 if (('contentlet_container_content_%s' % container_name) in form.cleaned_data) and (('contentlet_container_dynamic_%s' % container_name) in form.cleaned_data):
72 content = form.cleaned_data[('contentlet_container_content_%s' % container_name)]
73 dynamic = form.cleaned_data[('contentlet_container_dynamic_%s' % container_name)]
74 contentlet, created = page.contentlets.get_or_create(name=container_name, defaults={'content': content, 'dynamic': dynamic})
76 contentlet.content = content
77 contentlet.dynamic = dynamic
79 for container_name, container_content_type in contentreference_containers:
80 if ('contentreference_container_%s' % container_name) in form.cleaned_data:
81 content = form.cleaned_data[('contentreference_container_%s' % container_name)]
83 contentreference = page.contentreferences.get(name=container_name)
84 except ContentReference.DoesNotExist:
85 contentreference = ContentReference(name=container_name, page=page, content_type=container_content_type)
88 contentreference.delete()
90 if content is not None:
91 contentreference.content_id = content.id
92 contentreference.save()
95 class TemplateAdmin(admin.ModelAdmin):
96 prepopulated_fields = {'slug': ('name',)}
99 'fields': ('parent', 'name', 'slug')
102 'classes': COLLAPSE_CLASSES,
103 'fields': ('documentation',)
109 'classes': COLLAPSE_CLASSES,
110 'fields': ('mimetype',)
115 list_display = ('__unicode__', 'slug', 'get_path',)
118 admin.site.register(Page, PageAdmin)
119 admin.site.register(Template, TemplateAdmin)