Django

Code

Ticket #5620: newforms_xmlfield.diff3

File newforms_xmlfield.diff3, 2.0 kB (added by teepark, 4 months ago)
Line 
1 Index: django/db/models/fields/__init__.py
2 ===================================================================
3 --- django/db/models/fields/__init__.py (revision 7538)
4 +++ django/db/models/fields/__init__.py (working copy)
5 @@ -1157,6 +1157,11 @@
6          self.schema_path = schema_path
7          Field.__init__(self, verbose_name, name, **kwargs)
8  
9 +    def formfield(self, **kwargs):
10 +        defaults = {'form_class': curry(forms.XMLField, schema_path=self.schema_path)}
11 +        defaults.update(kwargs)
12 +        return super(XMLField, self).formfield(**defaults)
13 +
14      def get_manipulator_field_objs(self):
15          return [curry(oldforms.XMLLargeTextField, schema_path=self.schema_path)]
16  
17 Index: django/newforms/fields.py
18 ===================================================================
19 --- django/newforms/fields.py   (revision 7538)
20 +++ django/newforms/fields.py   (working copy)
21 @@ -32,7 +32,7 @@
22      'RegexField', 'EmailField', 'FileField', 'ImageField', 'URLField',
23      'BooleanField', 'NullBooleanField', 'ChoiceField', 'MultipleChoiceField',
24      'ComboField', 'MultiValueField', 'FloatField', 'DecimalField',
25 -    'SplitDateTimeField', 'IPAddressField', 'FilePathField',
26 +    'SplitDateTimeField', 'IPAddressField', 'FilePathField', 'XMLField'
27  )
28  
29  # These values, if given to to_python(), will trigger the self.required check.
30 @@ -782,3 +782,19 @@
31  
32      def __init__(self, *args, **kwargs):
33          super(IPAddressField, self).__init__(ipv4_re, *args, **kwargs)
34 +
35 +class XMLField(CharField):
36 +    """
37 +    A field that validates XML content against a RelaxNG schema
38 +    """
39 +    def __init__(self, schema_path=None, *args, **kwargs):
40 +        super(XMLField, self).__init__(*args, **kwargs)
41 +        self.schema_path = schema_path
42 +
43 +    def clean(self, value):
44 +        from django.core.validators import RelaxNGCompact, ValidationError as OldValidationError
45 +        if self.schema_path:
46 +            try:
47 +                RelaxNGCompact(self.schema_path)(value, "")
48 +            except OldValidationError, e:
49 +                raise ValidationError(e.messages)