Removed backwards-compatibility for database template loader. Added VERSION information.
[philo.git] / admin / pages.py
index 52238da..f9e96c0 100644 (file)
@@ -1,11 +1,38 @@
+from django.conf import settings
 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.base import COLLAPSE_CLASSES, TreeAdmin
 from philo.admin.nodes import ViewAdmin
 from philo.models.pages import Page, Template, Contentlet, ContentReference
-from philo.forms import TemplateForm
+from philo.admin.forms.containers import *
+
+
+class ContentletInline(admin.StackedInline):
+       model = Contentlet
+       extra = 0
+       max_num = 0
+       formset = ContentletInlineFormSet
+       form = ContentletForm
+       can_delete = False
+       classes = ('collapse-open', 'collapse','open')
+       if 'grappelli' in settings.INSTALLED_APPS:
+               template = 'admin/philo/edit_inline/grappelli_tabular_container.html'
+       else:
+               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
+       classes = ('collapse-open', 'collapse','open')
+       if 'grappelli' in settings.INSTALLED_APPS:
+               template = 'admin/philo/edit_inline/grappelli_tabular_container.html'
+       else:
+               template = 'admin/philo/edit_inline/tabular_container.html'
 
 
 class PageAdmin(ViewAdmin):
@@ -18,83 +45,16 @@ class PageAdmin(ViewAdmin):
        list_display = ('title', 'template')
        list_filter = ('template',)
        search_fields = ['title', 'contentlets__content']
+       inlines = [ContentletInline, ContentReferenceInline] + ViewAdmin.inlines
        
-       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()
+       def response_add(self, request, obj, post_url_continue='../%s/'):
+               # Shamelessly cribbed from django/contrib/auth/admin.py:143
+               if '_addanother' not in request.POST and '_popup' not in request.POST:
+                       request.POST['_continue'] = 1
+               return super(PageAdmin, self).response_add(request, obj, post_url_continue)
 
 
-class TemplateAdmin(admin.ModelAdmin):
+class TemplateAdmin(TreeAdmin):
        prepopulated_fields = {'slug': ('name',)}
        fieldsets = (
                (None, {
@@ -115,7 +75,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)