X-Git-Url: http://git.ithinksw.org/philo.git/blobdiff_plain/9592f8df376c7dacacd49bb7587ee494ad339035..ee56a2ca0d6c5d08ee42a9452de4353e8f2643ba:/forms.py?ds=sidebyside diff --git a/forms.py b/forms.py index a1785fb..b404f0d 100644 --- a/forms.py +++ b/forms.py @@ -9,7 +9,7 @@ from django.forms.formsets import TOTAL_FORM_COUNT from django.template import loader, loader_tags, TemplateDoesNotExist, Context, Template as DjangoTemplate from django.utils.datastructures import SortedDict from philo.admin.widgets import ModelLookupWidget -from philo.models import Entity, Template, Contentlet, ContentReference, Attribute +from philo.models import Entity, Template, Contentlet, ContentReference, Attribute, Node, NodeNavigationOverride from philo.utils import fattr @@ -337,4 +337,57 @@ class ContentReferenceInlineFormSet(ContainerInlineFormSet): name, content_type = self.extra_containers[i - self.initial_form_count() - 1] kwargs['instance'] = self.model(name=name, content_type=content_type) - return super(ContentReferenceInlineFormSet, self)._construct_form(i, **kwargs) \ No newline at end of file + return super(ContentReferenceInlineFormSet, self)._construct_form(i, **kwargs) + + +class NodeWithOverrideForm(forms.ModelForm): + title = NodeNavigationOverride._meta.get_field('title').formfield() + url = NodeNavigationOverride._meta.get_field('url').formfield() + child_navigation = NodeNavigationOverride._meta.get_field('child_navigation').formfield(required=False) + + def __init__(self, *args, **kwargs): + super(NodeWithOverrideForm, self).__init__(*args, **kwargs) + if self.instance.pk: + self._override = override = self.get_override(self.instance) + self.initial.update({ + 'title': override.title, + 'url': override.url, + 'child_navigation': override.child_navigation_json + }) + + def get_override(self, instance): + try: + return NodeNavigationOverride.objects.get(parent=self.instance.parent, child=self.instance) + except NodeNavigationOverride.DoesNotExist: + override = NodeNavigationOverride(parent=self.instance.parent, child=self.instance) + override.child_navigation = None + return override + + def save(self, commit=True): + obj = super(NodeWithOverrideForm, self).save(commit) + cleaned_data = self.cleaned_data + override = self.get_override(obj) + + # Override information should only be set if there was no previous override or if the + # information was just manually set - i.e. was not equal to the data on the cached override. + if not override.pk or cleaned_data['title'] != self._override.title or cleaned_data['url'] != self._override.url or cleaned_data['child_navigation'] != self._override.child_navigation: + override.title = self.cleaned_data['title'] + override.url = self.cleaned_data['url'] + override.child_navigation = self.cleaned_data['child_navigation'] + override.save() + return obj + + class Meta: + model = Node + + +class NodeOverrideInlineFormSet(BaseInlineFormSet): + def __init__(self, data=None, files=None, instance=None, save_as_new=False, prefix=None, queryset=None): + if queryset is None: + queryset = self.model._default_manager + queryset = queryset.filter(parent=instance, child__parent=instance) + super(NodeOverrideInlineFormSet, self).__init__(data, files, instance, save_as_new, prefix, queryset) + + def add_fields(self, form, index): + super(NodeOverrideInlineFormSet, self).add_fields(form, index) + form.fields['child'].queryset = self.instance.children.all() \ No newline at end of file