From 36409393a5b3cf09c6e6e808156e320c6cc7aa96 Mon Sep 17 00:00:00 2001 From: melinath Date: Wed, 18 Aug 2010 15:03:07 -0400 Subject: [PATCH] Initial new page admin commit. Working on faking the admin out through inlines instead of ModelAdmin methods. --- admin/pages.py | 95 +++----------- forms.py | 67 +++++++++- models/pages.py | 6 + .../philo/edit_inline/tabular_container.html | 122 ++++++++++++++++++ 4 files changed, 211 insertions(+), 79 deletions(-) create mode 100644 templates/admin/philo/edit_inline/tabular_container.html diff --git a/admin/pages.py b/admin/pages.py index 52238da..fe6f821 100644 --- a/admin/pages.py +++ b/admin/pages.py @@ -5,7 +5,25 @@ 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 TemplateForm, ContentletInlineFormSet, ContentReferenceInlineFormSet + + +class ContentletInline(admin.StackedInline): + model = Contentlet + extra = 0 + max_num = 0 + formset = ContentletInlineFormSet + can_delete = False + template = 'admin/philo/edit_inline/tabular_container.html' + + +class ContentReferenceInline(admin.StackedInline): + model = ContentReference + extra = 0 + max_num = 0 + formset = ContentReferenceInlineFormSet + 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): diff --git a/forms.py b/forms.py index 8050d39..5f9e7b1 100644 --- a/forms.py +++ b/forms.py @@ -1,5 +1,5 @@ -from django.core.exceptions import ValidationError -from django.forms.models import model_to_dict, fields_for_model, ModelFormMetaclass, ModelForm +from django.core.exceptions import ValidationError, ObjectDoesNotExist +from django.forms.models import model_to_dict, fields_for_model, ModelFormMetaclass, ModelForm, BaseInlineFormSet from django.template import loader, loader_tags, TemplateDoesNotExist, Context, Template as DjangoTemplate from django.utils.datastructures import SortedDict from philo.models import Entity, Template @@ -94,7 +94,7 @@ class EntityForm(EntityFormBase): # Would inherit from ModelForm directly if it def validate_template(template): """ Makes sure that the template and all included or extended templates are valid. - """ + """ for node in template.nodelist: try: if isinstance(node, loader_tags.ExtendsNode): @@ -119,4 +119,63 @@ class TemplateForm(ModelForm): return code class Meta: - model = Template \ No newline at end of file + model = Template + + +class ContainerInlineFormSet(BaseInlineFormSet): + def __init__(self, containers, data=None, files=None, instance=None, save_as_new=False, prefix=None, queryset=None): + # Unfortunately, I need to add some things to BaseInline between its __init__ and its super call, so + # a lot of this is repetition. + + # Start cribbed from BaseInline + from django.db.models.fields.related import RelatedObject + self.save_as_new = save_as_new + # is there a better way to get the object descriptor? + self.rel_name = RelatedObject(self.fk.rel.to, self.model, self.fk).get_accessor_name() + if self.fk.rel.field_name == self.fk.rel.to._meta.pk.name: + backlink_value = self.instance + else: + backlink_value = getattr(self.instance, self.fk.rel.field_name) + if queryset is None: + queryset = self.model._default_manager + qs = queryset.filter(**{self.fk.name: backlink_value}).filter(name__in=containers) + # End cribbed from BaseInline + + self.container_instances = [] + for container in qs: + self.container_instances.append(container) + containers.remove(container.name) + self.extra_containers = containers + self.extra = len(self.extra_containers) + + super(BaseInlineFormSet, self).__init__(data, files, prefix, qs) + + def _construct_form(self, i, **kwargs): + if i > self.initial_form_count(): # and not kwargs.get('instance'): + kwargs['instance'] = self.model(name=self.extra_containers[i - self.initial_form_count() - 1]) + + return super(ContainerInlineFormSet, self)._construct_form(i, **kwargs) + + +class ContentletInlineFormSet(ContainerInlineFormSet): + def __init__(self, data=None, files=None, instance=None, save_as_new=False, prefix=None, queryset=None): + if instance is None: + self.instance = self.fk.rel.to() + containers = [] + else: + self.instance = instance + containers = list(self.instance.containers[0]) + + super(ContentletInlineFormSet, self).__init__(containers, data, files, instance, save_as_new, prefix, queryset) + + +class ContentReferenceInlineFormSet(ContainerInlineFormSet): + def __init__(self, data=None, files=None, instance=None, save_as_new=False, prefix=None, queryset=None): + if instance is None: + self.instance = self.fk.rel.to() + containers = [] + else: + self.instance = instance + containers = list(self.instance.containers[1]) + + super(ContentReferenceInlineFormSet, self).__init__(containers, data, files, instance, save_as_new, prefix, queryset) \ No newline at end of file diff --git a/models/pages.py b/models/pages.py index 7476f18..4260761 100644 --- a/models/pages.py +++ b/models/pages.py @@ -98,6 +98,12 @@ class Page(View): template = models.ForeignKey(Template, related_name='pages') title = models.CharField(max_length=255) + def get_containers(self): + if not hasattr(self, '_containers'): + self._containers = self.template.containers + return self._containers + containers = property(get_containers) + def render_to_string(self, node=None, request=None, path=None, subpath=None, extra_context=None): context = {} context.update(extra_context or {}) diff --git a/templates/admin/philo/edit_inline/tabular_container.html b/templates/admin/philo/edit_inline/tabular_container.html new file mode 100644 index 0000000..b751f52 --- /dev/null +++ b/templates/admin/philo/edit_inline/tabular_container.html @@ -0,0 +1,122 @@ +{% load i18n adminmedia %} +
+ +
+ + -- 2.20.1