X-Git-Url: http://git.ithinksw.org/philo.git/blobdiff_plain/6d5e8d5c70865b4e4181f411c66d1a9ec9c16ccd..1d3756407233b937d7491bde6dd4046fb8b4b6d6:/admin/pages.py diff --git a/admin/pages.py b/admin/pages.py index 52238da..234b9d8 100644 --- a/admin/pages.py +++ b/admin/pages.py @@ -1,11 +1,29 @@ from django.contrib import admin from django import forms -from django.template import Template as DjangoTemplate -from philo.admin import widgets from philo.admin.base import COLLAPSE_CLASSES from philo.admin.nodes import ViewAdmin from philo.models.pages import Page, Template, Contentlet, ContentReference -from philo.forms import TemplateForm +from philo.forms import ContentletInlineFormSet, ContentReferenceInlineFormSet, ContentletForm, ContentReferenceForm + + +class ContentletInline(admin.StackedInline): + model = Contentlet + extra = 0 + max_num = 0 + formset = ContentletInlineFormSet + form = ContentletForm + can_delete = False + template = 'admin/philo/edit_inline/tabular_container.html' + + +class ContentReferenceInline(admin.StackedInline): + model = ContentReference + extra = 0 + max_num = 0 + formset = ContentReferenceInlineFormSet + form = ContentReferenceForm + can_delete = False + template = 'admin/philo/edit_inline/tabular_container.html' class PageAdmin(ViewAdmin): @@ -18,80 +36,7 @@ class PageAdmin(ViewAdmin): list_display = ('title', 'template') list_filter = ('template',) search_fields = ['title', 'contentlets__content'] - - def get_fieldsets(self, request, obj=None, **kwargs): - fieldsets = list(self.fieldsets) - if obj: # if no obj, creating a new page, thus no template set, thus no containers - template = obj.template - if template.documentation: - fieldsets.append(('Template Documentation', { - 'description': template.documentation - })) - contentlet_containers, contentreference_containers = template.containers - for container_name in contentlet_containers: - fieldsets.append((('Container: %s' % container_name), { - 'fields': (('contentlet_container_content_%s' % container_name), ('contentlet_container_dynamic_%s' % container_name)), - 'classes': ['monospace'] - })) - for container_name, container_content_type in contentreference_containers: - fieldsets.append((('Container: %s' % container_name), { - 'fields': (('contentreference_container_%s' % container_name),) - })) - return fieldsets - - def get_form(self, request, obj=None, **kwargs): - form = super(PageAdmin, self).get_form(request, obj, **kwargs) - if obj: # if no obj, creating a new page, thus no template set, thus no containers - page = obj - template = page.template - contentlet_containers, contentreference_containers = template.containers - for container_name in contentlet_containers: - initial_content = None - initial_dynamic = False - try: - contentlet = page.contentlets.get(name__exact=container_name) - initial_content = contentlet.content - initial_dynamic = contentlet.dynamic - except Contentlet.DoesNotExist: - pass - form.base_fields[('contentlet_container_content_%s' % container_name)] = forms.CharField(label='Content', widget=forms.Textarea(), initial=initial_content, required=False) - 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) - for container_name, container_content_type in contentreference_containers: - initial_content = None - try: - initial_content = page.contentreferences.get(name__exact=container_name, content_type=container_content_type).content.pk - except (ContentReference.DoesNotExist, AttributeError): - pass - 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()) - return form - - def save_model(self, request, page, form, change): - page.save() - template = page.template - contentlet_containers, contentreference_containers = template.containers - for container_name in contentlet_containers: - if (('contentlet_container_content_%s' % container_name) in form.cleaned_data) and (('contentlet_container_dynamic_%s' % container_name) in form.cleaned_data): - content = form.cleaned_data[('contentlet_container_content_%s' % container_name)] - dynamic = form.cleaned_data[('contentlet_container_dynamic_%s' % container_name)] - contentlet, created = page.contentlets.get_or_create(name=container_name, defaults={'content': content, 'dynamic': dynamic}) - if not created: - contentlet.content = content - contentlet.dynamic = dynamic - contentlet.save() - for container_name, container_content_type in contentreference_containers: - if ('contentreference_container_%s' % container_name) in form.cleaned_data: - content = form.cleaned_data[('contentreference_container_%s' % container_name)] - try: - contentreference = page.contentreferences.get(name=container_name) - except ContentReference.DoesNotExist: - contentreference = ContentReference(name=container_name, page=page, content_type=container_content_type) - else: - if content == None: - contentreference.delete() - - if content is not None: - contentreference.content_id = content.id - contentreference.save() + inlines = [ContentletInline, ContentReferenceInline] + ViewAdmin.inlines class TemplateAdmin(admin.ModelAdmin): @@ -115,7 +60,6 @@ class TemplateAdmin(admin.ModelAdmin): save_on_top = True save_as = True list_display = ('__unicode__', 'slug', 'get_path',) - form = TemplateForm admin.site.register(Page, PageAdmin)