+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)
+
+class PersonListField(forms.Field):
+
+ def __init__(self, *args, **kwargs):
+ super(PersonListField, 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 = [ e if isinstance(e, dcparser.Person) \
+ else dcparser.Person.from_text(e) for e in value ]
+ elif isinstance(value, basestring):
+ self._initial = [dcparser.Person.from_text(token) for token 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(PersonListField, self).clean(value)
+ people = value.split('\n')
+ try:
+ return [dcparser.Person.from_text(person) for person in people]
+ except ValueError, e:
+ raise django.utils.ValidationError(e.message)