1 from django.core.exceptions import ValidationError, ObjectDoesNotExist
2 from django.forms.models import model_to_dict, fields_for_model, ModelFormMetaclass, ModelForm, BaseInlineFormSet
3 from django.forms.formsets import TOTAL_FORM_COUNT
4 from django.template import loader, loader_tags, TemplateDoesNotExist, Context, Template as DjangoTemplate
5 from django.utils.datastructures import SortedDict
6 from philo.models import Entity, Template, Contentlet, ContentReference
7 from philo.models.fields import RelationshipField
8 from philo.utils import fattr
11 __all__ = ('EntityForm', )
14 def proxy_fields_for_entity_model(entity_model, fields=None, exclude=None, widgets=None, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)):
17 opts = entity_model._entity_meta
18 for f in opts.proxy_fields:
19 if fields and not f.name in fields:
21 if exclude and f.name in exclude:
23 if widgets and f.name in widgets:
24 kwargs = {'widget': widgets[f.name]}
27 formfield = formfield_callback(f, **kwargs)
29 field_list.append((f.name, formfield))
31 ignored.append(f.name)
32 field_dict = SortedDict(field_list)
34 field_dict = SortedDict(
35 [(f, field_dict.get(f)) for f in fields
36 if ((not exclude) or (exclude and f not in exclude)) and (f not in ignored) and (f in field_dict)]
41 # BEGIN HACK - This will not be required after http://code.djangoproject.com/ticket/14082 has been resolved
43 class EntityFormBase(ModelForm):
46 _old_metaclass_new = ModelFormMetaclass.__new__
48 def _new_metaclass_new(cls, name, bases, attrs):
49 new_class = _old_metaclass_new(cls, name, bases, attrs)
50 if issubclass(new_class, EntityFormBase) and new_class._meta.model:
51 new_class.base_fields.update(proxy_fields_for_entity_model(new_class._meta.model, new_class._meta.fields, new_class._meta.exclude, new_class._meta.widgets)) # don't pass in formfield_callback
54 ModelFormMetaclass.__new__ = staticmethod(_new_metaclass_new)
59 class EntityForm(EntityFormBase): # Would inherit from ModelForm directly if it weren't for the above HACK
60 def __init__(self, *args, **kwargs):
61 initial = kwargs.pop('initial', None)
62 instance = kwargs.get('instance', None)
63 if instance is not None:
65 for f in instance._entity_meta.proxy_fields:
66 if self._meta.fields and not f.name in self._meta.fields:
68 if self._meta.exclude and f.name in self._meta.exclude:
70 new_initial[f.name] = f.value_from_object(instance)
73 if initial is not None:
74 new_initial.update(initial)
75 kwargs['initial'] = new_initial
76 super(EntityForm, self).__init__(*args, **kwargs)
78 @fattr(alters_data=True)
79 def save(self, commit=True):
80 cleaned_data = self.cleaned_data
81 instance = super(EntityForm, self).save(commit=False)
83 for f in instance._entity_meta.proxy_fields:
84 if self._meta.fields and f.name not in self._meta.fields:
86 setattr(instance, f.attname, cleaned_data[f.name])
95 def validate_template(template):
97 Makes sure that the template and all included or extended templates are valid.
99 for node in template.nodelist:
101 if isinstance(node, loader_tags.ExtendsNode):
102 extended_template = node.get_parent(Context())
103 validate_template(extended_template)
104 elif isinstance(node, loader_tags.IncludeNode):
105 included_template = loader.get_template(node.template_name.resolve(Context()))
106 validate_template(extended_template)
108 raise ValidationError("Template code invalid. Error was: %s: %s" % (e.__class__.__name__, e))
111 class TemplateForm(ModelForm):
112 def clean_code(self):
113 code = self.cleaned_data['code']
115 t = DjangoTemplate(code)
117 raise ValidationError("Template code invalid. Error was: %s: %s" % (e.__class__.__name__, e))
126 class ContainerForm(ModelForm):
127 def __init__(self, *args, **kwargs):
128 super(ContainerForm, self).__init__(*args, **kwargs)
129 self.verbose_name = self.instance.name.replace('_', ' ')
132 class ContentletForm(ContainerForm):
135 fields = ['name', 'content', 'dynamic']
138 class ContentReferenceForm(ContainerForm):
140 model = ContentReference
141 fields = ['name', 'content_id']
144 class ContainerInlineFormSet(BaseInlineFormSet):
145 def __init__(self, containers, data=None, files=None, instance=None, save_as_new=False, prefix=None, queryset=None):
146 # Unfortunately, I need to add some things to BaseInline between its __init__ and its super call, so
147 # a lot of this is repetition.
149 # Start cribbed from BaseInline
150 from django.db.models.fields.related import RelatedObject
151 self.save_as_new = save_as_new
152 # is there a better way to get the object descriptor?
153 self.rel_name = RelatedObject(self.fk.rel.to, self.model, self.fk).get_accessor_name()
154 if self.fk.rel.field_name == self.fk.rel.to._meta.pk.name:
155 backlink_value = self.instance
157 backlink_value = getattr(self.instance, self.fk.rel.field_name)
159 queryset = self.model._default_manager
160 qs = queryset.filter(**{self.fk.name: backlink_value}).filter(name__in=containers)
161 # End cribbed from BaseInline
163 self.container_instances = []
165 self.container_instances.append(container)
166 containers.remove(container.name)
167 self.extra_containers = containers
168 self.extra = len(self.extra_containers)
169 super(BaseInlineFormSet, self).__init__(data, files, prefix=prefix, queryset=qs)
171 def total_form_count(self):
172 if self.data or self.files:
173 return self.management_form.cleaned_data[TOTAL_FORM_COUNT]
175 return self.initial_form_count() + self.extra
178 class ContentletInlineFormSet(ContainerInlineFormSet):
179 def __init__(self, data=None, files=None, instance=None, save_as_new=False, prefix=None, queryset=None):
181 self.instance = self.fk.rel.to()
184 self.instance = instance
185 containers = list(self.instance.containers[0])
187 super(ContentletInlineFormSet, self).__init__(containers, data, files, instance, save_as_new, prefix, queryset)
189 def _construct_form(self, i, **kwargs):
190 if i >= self.initial_form_count(): # and not kwargs.get('instance'):
191 kwargs['instance'] = self.model(name=self.extra_containers[i - self.initial_form_count() - 1])
193 return super(ContentletInlineFormSet, self)._construct_form(i, **kwargs)
196 class ContentReferenceInlineFormSet(ContainerInlineFormSet):
197 def __init__(self, data=None, files=None, instance=None, save_as_new=False, prefix=None, queryset=None):
199 self.instance = self.fk.rel.to()
202 self.instance = instance
203 containers = list(self.instance.containers[1])
205 super(ContentReferenceInlineFormSet, self).__init__(containers, data, files, instance, save_as_new, prefix, queryset)
207 def _construct_form(self, i, **kwargs):
208 if i >= self.initial_form_count(): # and not kwargs.get('instance'):
209 name, content_type = self.extra_containers[i - self.initial_form_count() - 1]
210 kwargs['instance'] = self.model(name=name, content_type=content_type)
212 return super(ContentReferenceInlineFormSet, self)._construct_form(i, **kwargs)