Added kwarg capability to embed tag. Adjusted reference substitution to account for...
[philo.git] / forms.py
index 506ab71..ced29b2 100644 (file)
--- a/forms.py
+++ b/forms.py
@@ -1,8 +1,12 @@
+from django import forms
+from django.contrib.admin.widgets import AdminTextareaWidget
 from django.core.exceptions import ValidationError, ObjectDoesNotExist
 from django.core.exceptions import ValidationError, ObjectDoesNotExist
+from django.db.models import Q
 from django.forms.models import model_to_dict, fields_for_model, ModelFormMetaclass, ModelForm, BaseInlineFormSet
 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 django.forms.models import model_to_dict, fields_for_model, ModelFormMetaclass, ModelForm, BaseInlineFormSet
 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
 from philo.models.fields import RelationshipField
 from philo.utils import fattr
 from philo.models import Entity, Template, Contentlet, ContentReference
 from philo.models.fields import RelationshipField
 from philo.utils import fattr
@@ -92,37 +96,6 @@ class EntityForm(EntityFormBase): # Would inherit from ModelForm directly if it
                return instance
 
 
                return instance
 
 
-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):
-                               extended_template = node.get_parent(Context())
-                               validate_template(extended_template)
-                       elif isinstance(node, loader_tags.IncludeNode):
-                               included_template = loader.get_template(node.template_name.resolve(Context()))
-                               validate_template(extended_template)
-               except Exception, e:
-                       raise ValidationError("Template code invalid. Error was: %s: %s" % (e.__class__.__name__, e))
-
-
-class TemplateForm(ModelForm):
-       def clean_code(self):
-               code = self.cleaned_data['code']
-               try:
-                       t = DjangoTemplate(code)
-               except Exception, e:
-                       raise ValidationError("Template code invalid. Error was: %s: %s" % (e.__class__.__name__, e))
-
-               validate_template(t)
-               return code
-
-       class Meta:
-               model = Template
-
-
 class ContainerForm(ModelForm):
        def __init__(self, *args, **kwargs):
                super(ContainerForm, self).__init__(*args, **kwargs)
 class ContainerForm(ModelForm):
        def __init__(self, *args, **kwargs):
                super(ContainerForm, self).__init__(*args, **kwargs)
@@ -130,12 +103,28 @@ class ContainerForm(ModelForm):
 
 
 class ContentletForm(ContainerForm):
 
 
 class ContentletForm(ContainerForm):
+       content = forms.CharField(required=False, widget=AdminTextareaWidget, label='Content')
+       
+       def should_delete(self):
+               return not bool(self.cleaned_data['content'])
+       
        class Meta:
                model = Contentlet
        class Meta:
                model = Contentlet
-               fields = ['name', 'content', 'dynamic']
+               fields = ['name', 'content']
 
 
 class ContentReferenceForm(ContainerForm):
 
 
 class ContentReferenceForm(ContainerForm):
+       def __init__(self, *args, **kwargs):
+               super(ContentReferenceForm, self).__init__(*args, **kwargs)
+               try:
+                       self.fields['content_id'].widget = ModelLookupWidget(self.instance.content_type)
+               except ObjectDoesNotExist:
+                       # This will happen when an empty form (which we will never use) gets instantiated.
+                       pass
+       
+       def should_delete(self):
+               return (self.cleaned_data['content_id'] is None)
+       
        class Meta:
                model = ContentReference
                fields = ['name', 'content_id']
        class Meta:
                model = ContentReference
                fields = ['name', 'content_id']
@@ -143,8 +132,8 @@ class ContentReferenceForm(ContainerForm):
 
 class ContainerInlineFormSet(BaseInlineFormSet):
        def __init__(self, containers, data=None, files=None, instance=None, save_as_new=False, prefix=None, queryset=None):
 
 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.
+               # 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
                
                # Start cribbed from BaseInline
                from django.db.models.fields.related import RelatedObject
@@ -173,16 +162,61 @@ class ContainerInlineFormSet(BaseInlineFormSet):
                        return self.management_form.cleaned_data[TOTAL_FORM_COUNT]
                else:
                        return self.initial_form_count() + self.extra
                        return self.management_form.cleaned_data[TOTAL_FORM_COUNT]
                else:
                        return self.initial_form_count() + self.extra
+       
+       def save_existing_objects(self, commit=True):
+               self.changed_objects = []
+               self.deleted_objects = []
+               if not self.get_queryset():
+                       return []
+
+               saved_instances = []
+               for form in self.initial_forms:
+                       pk_name = self._pk_field.name
+                       raw_pk_value = form._raw_value(pk_name)
+
+                       # clean() for different types of PK fields can sometimes return
+                       # the model instance, and sometimes the PK. Handle either.
+                       pk_value = form.fields[pk_name].clean(raw_pk_value)
+                       pk_value = getattr(pk_value, 'pk', pk_value)
+
+                       obj = self._existing_object(pk_value)
+                       if form.should_delete():
+                               self.deleted_objects.append(obj)
+                               obj.delete()
+                               continue
+                       if form.has_changed():
+                               self.changed_objects.append((obj, form.changed_data))
+                               saved_instances.append(self.save_existing(form, obj, commit=commit))
+                               if not commit:
+                                       self.saved_forms.append(form)
+               return saved_instances
+
+       def save_new_objects(self, commit=True):
+               self.new_objects = []
+               for form in self.extra_forms:
+                       if not form.has_changed():
+                               continue
+                       # If someone has marked an add form for deletion, don't save the
+                       # object.
+                       if form.should_delete():
+                               continue
+                       self.new_objects.append(self.save_new(form, commit=commit))
+                       if not commit:
+                               self.saved_forms.append(form)
+               return self.new_objects
 
 
 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()
 
 
 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
                else:
                        self.instance = instance
+               
+               try:
                        containers = list(self.instance.containers[0])
                        containers = list(self.instance.containers[0])
+               except ObjectDoesNotExist:
+                       containers = []
        
                super(ContentletInlineFormSet, self).__init__(containers, data, files, instance, save_as_new, prefix, queryset)
        
        
                super(ContentletInlineFormSet, self).__init__(containers, data, files, instance, save_as_new, prefix, queryset)
        
@@ -205,15 +239,23 @@ 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()
        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
                else:
                        self.instance = instance
+               
+               try:
                        containers = list(self.instance.containers[1])
                        containers = list(self.instance.containers[1])
+               except ObjectDoesNotExist:
+                       containers = []
        
                super(ContentReferenceInlineFormSet, self).__init__(containers, data, files, instance, save_as_new, prefix, queryset)
        
        def get_container_instances(self, containers, qs):
        
                super(ContentReferenceInlineFormSet, self).__init__(containers, data, files, instance, save_as_new, prefix, queryset)
        
        def get_container_instances(self, containers, qs):
-               qs = qs.filter(name__in=[c[0] for c in containers])
+               filter = Q()
+               
+               for name, ct in containers:
+                       filter |= Q(name=name, content_type=ct)
+               
+               qs = qs.filter(filter)
                container_instances = []
                for container in qs:
                        container_instances.append(container)
                container_instances = []
                for container in qs:
                        container_instances.append(container)