+class ForeignKeyAttribute(AttributeField):
+ descriptor_class = ForeignKeyAttributeDescriptor
+
+ def __init__(self, model, limit_choices_to=None, key=None, **kwargs):
+ super(ForeignKeyAttribute, self).__init__(**kwargs)
+ self.key = key
+ self.model = model
+ if limit_choices_to is None:
+ limit_choices_to = {}
+ self.limit_choices_to = limit_choices_to
+
+ def formfield(self, form_class=forms.ModelChoiceField, **kwargs):
+ defaults = {'required': False, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
+ if self.has_default():
+ defaults['initial'] = self.default
+ defaults.update(kwargs)
+ return form_class(self.model._default_manager.complex_filter(self.limit_choices_to), **defaults)
+
+ def value_from_object(self, obj):
+ try:
+ relobj = super(ForeignKeyAttribute, self).value_from_object(obj)
+ except AttributeError:
+ return None
+ return getattr(relobj, 'pk', None)
+
+ def set_attribute_value(self, attribute, value, value_class=None):
+ if value_class is None:
+ from philo.models.base import ForeignKeyValue
+ value_class = ForeignKeyValue
+ super(ForeignKeyAttribute, self).set_attribute_value(attribute, value, value_class)
+
+
+class ManyToManyAttribute(ForeignKeyAttribute):
+ descriptor_class = ManyToManyAttributeDescriptor
+
+ def formfield(self, form_class=forms.ModelMultipleChoiceField, **kwargs):
+ return super(ManyToManyAttribute, self).formfield(form_class, **kwargs)
+
+ def set_attribute_value(self, attribute, value, value_class=None):
+ if value_class is None:
+ from philo.models.base import ManyToManyValue
+ value_class = ManyToManyValue
+ super(ManyToManyAttribute, self).set_attribute_value(attribute, value, value_class)
+
+
+class TemplateField(models.TextField):
+ def __init__(self, allow=None, disallow=None, secure=True, *args, **kwargs):
+ super(TemplateField, self).__init__(*args, **kwargs)
+ self.validators.append(TemplateValidator(allow, disallow, secure))
+
+
+class JSONFormField(forms.Field):
+ default_validators = [json_validator]
+
+ def clean(self, value):
+ if value == '' and not self.required:
+ return None
+ try:
+ return json.loads(value)
+ except Exception, e:
+ raise ValidationError(u'JSON decode error: %s' % e)
+
+
+class JSONDescriptor(object):