1 from django.forms.models import ModelFormMetaclass, ModelForm, ModelFormOptions
2 from django.utils.datastructures import SortedDict
3 from philo.utils import fattr
6 __all__ = ('EntityForm',)
9 def proxy_fields_for_entity_model(entity_model, fields=None, exclude=None, widgets=None, formfield_callback=None):
12 opts = entity_model._entity_meta
13 for f in opts.proxy_fields:
16 if fields and not f.name in fields:
18 if exclude and f.name in exclude:
20 if widgets and f.name in widgets:
21 kwargs = {'widget': widgets[f.name]}
25 if formfield_callback is None:
26 formfield = f.formfield(**kwargs)
27 elif not callable(formfield_callback):
28 raise TypeError('formfield_callback must be a function or callable')
30 formfield = formfield_callback(f, **kwargs)
33 field_list.append((f.name, formfield))
35 ignored.append(f.name)
36 field_dict = SortedDict(field_list)
38 field_dict = SortedDict(
39 [(f, field_dict.get(f)) for f in fields
40 if ((not exclude) or (exclude and f not in exclude)) and (f not in ignored) and (f in field_dict)]
45 # HACK until http://code.djangoproject.com/ticket/14082 is resolved.
46 _old = ModelFormMetaclass.__new__
47 def _new(cls, name, bases, attrs):
48 if cls == ModelFormMetaclass:
49 m = attrs.get('__metaclass__', None)
51 parents = [b for b in bases if issubclass(b, ModelForm)]
53 if c.__metaclass__ != ModelFormMetaclass:
58 return m(name, bases, attrs)
60 return _old(cls, name, bases, attrs)
61 ModelFormMetaclass.__new__ = staticmethod(_new)
65 class EntityFormMetaclass(ModelFormMetaclass):
66 def __new__(cls, name, bases, attrs):
68 parents = [b for b in bases if issubclass(b, EntityForm)]
70 # We are defining EntityForm itself
72 sup = super(EntityFormMetaclass, cls)
75 # Then there's no business trying to use proxy fields.
76 return sup.__new__(cls, name, bases, attrs)
78 # Fake a declaration of all proxy fields so they'll be handled correctly.
79 opts = ModelFormOptions(attrs.get('Meta', None))
82 formfield_callback = attrs.get('formfield_callback', None)
83 proxy_fields = proxy_fields_for_entity_model(opts.model, opts.fields, opts.exclude, opts.widgets, formfield_callback)
87 new_attrs = proxy_fields.copy()
88 new_attrs.update(attrs)
90 new_class = sup.__new__(cls, name, bases, new_attrs)
91 new_class.proxy_fields = proxy_fields
95 class EntityForm(ModelForm):
96 __metaclass__ = EntityFormMetaclass
98 def __init__(self, *args, **kwargs):
99 initial = kwargs.pop('initial', None)
100 instance = kwargs.get('instance', None)
101 if instance is not None:
103 for f in instance._entity_meta.proxy_fields:
104 if self._meta.fields and not f.name in self._meta.fields:
106 if self._meta.exclude and f.name in self._meta.exclude:
108 new_initial[f.name] = f.value_from_object(instance)
111 if initial is not None:
112 new_initial.update(initial)
113 kwargs['initial'] = new_initial
114 super(EntityForm, self).__init__(*args, **kwargs)
116 @fattr(alters_data=True)
117 def save(self, commit=True):
118 cleaned_data = self.cleaned_data
119 instance = super(EntityForm, self).save(commit=False)
121 for f in instance._entity_meta.proxy_fields:
122 if not f.editable or not f.name in cleaned_data:
124 if self._meta.fields and f.name not in self._meta.fields:
126 if self._meta.exclude and f.name in self._meta.exclude:
128 setattr(instance, f.attname, f.get_storage_value(cleaned_data[f.name]))