+class PersonField(forms.CharField):
+ def clean(self, value):
+ try:
+ return dcparser.Person.from_text( super(PersonField, self).clean(value) )
+ except ValueError, e:
+ raise django.utils.ValidationError(e.message)
+
+def person_conv(value):
+ if isinstance(value, dcparser.Person):
+ return value
+ elif isinstance(value, basestring):
+ return dcparser.Person.from_text( unicode(value) )
+ else:
+ raise ValueError("Can't convert '%s' to Person object." % value)
+
+class ListField(forms.Field):
+ def __init__(self, *args, **kwargs):
+ self.convert = kwargs.pop('converter', unicode)
+ if not kwargs.has_key('widget'):
+ kwargs['widget'] = forms.Textarea
+ super(ListField, self).__init__(*args, **kwargs)
+
+ def _get_initial(self):
+ return self._initial and (u'\n'.join( ( unicode(person) for person in self._initial)))
+
+ def _set_initial(self, value):
+ if value is None:
+ self._initial = None
+ elif isinstance(value, list):
+ self._initial = [ self.convert(e) for e in value ]
+ elif isinstance(value, basestring):
+ self._initial = [ self.convert(e) for e in value.split('\n') ]
+ else:
+ raise ValueError("Invalid value. Must be a list of dcparser.Person or string")
+
+ initial = property(_get_initial, _set_initial)
+
+ def clean(self, value):
+ super(ListField, self).clean(value)
+ elems = value.split('\n')
+ try:
+ return [self.convert(el) for el in elems]
+ except ValueError, err:
+ raise django.utils.ValidationError(err.message)