Added ListField type with unit tests
This commit is contained in:
@@ -44,7 +44,7 @@ class BaseField(object):
|
||||
try:
|
||||
value = self._to_python(value)
|
||||
self._validate(value)
|
||||
except (ValueError, AttributeError):
|
||||
except (ValueError, AttributeError, AssertionError):
|
||||
raise ValidationError('Invalid value for field of type "' +
|
||||
self.__class__.__name__ + '"')
|
||||
elif self.required:
|
||||
|
||||
@@ -5,7 +5,7 @@ import re
|
||||
import pymongo
|
||||
|
||||
|
||||
__all__ = ['StringField', 'IntField', 'EmbeddedDocumentField',
|
||||
__all__ = ['StringField', 'IntField', 'EmbeddedDocumentField', 'ListField',
|
||||
'ObjectIdField', 'ValidationError']
|
||||
|
||||
|
||||
@@ -17,8 +17,14 @@ class StringField(BaseField):
|
||||
self.regex = re.compile(regex) if regex else None
|
||||
self.max_length = max_length
|
||||
super(StringField, self).__init__(**kwargs)
|
||||
|
||||
def _to_python(self, value):
|
||||
assert(isinstance(value, (str, unicode)))
|
||||
return unicode(value)
|
||||
|
||||
def _validate(self, value):
|
||||
assert(isinstance(value, (str, unicode)))
|
||||
|
||||
if self.max_length is not None and len(value) > self.max_length:
|
||||
raise ValidationError('String value is too long')
|
||||
|
||||
@@ -36,9 +42,12 @@ class IntField(BaseField):
|
||||
super(IntField, self).__init__(**kwargs)
|
||||
|
||||
def _to_python(self, value):
|
||||
assert(isinstance(value, int))
|
||||
return int(value)
|
||||
|
||||
def _validate(self, value):
|
||||
assert(isinstance(value, int))
|
||||
|
||||
if self.min_value is not None and value < self.min_value:
|
||||
raise ValidationError('Integer value is too small')
|
||||
|
||||
@@ -74,3 +83,41 @@ class EmbeddedDocumentField(BaseField):
|
||||
if not isinstance(value, self.document):
|
||||
raise ValidationError('Invalid embedded document instance '
|
||||
'provided to an EmbeddedDocumentField')
|
||||
|
||||
|
||||
class ListField(BaseField):
|
||||
"""A list field that wraps a standard field, allowing multiple instances
|
||||
of the field to be used as a list in the database.
|
||||
"""
|
||||
|
||||
def __init__(self, field, **kwargs):
|
||||
if not isinstance(field, BaseField):
|
||||
raise ValidationError('Argument to ListField constructor must be '
|
||||
'a valid field')
|
||||
self.field = field
|
||||
super(ListField, self).__init__(**kwargs)
|
||||
|
||||
def _to_python(self, value):
|
||||
assert(isinstance(value, (list, tuple)))
|
||||
return list(value)
|
||||
|
||||
def _to_mongo(self, value):
|
||||
return [self.field._to_mongo(item) for item in value]
|
||||
|
||||
def _validate(self, value):
|
||||
"""Make sure that a list of valid fields is being used.
|
||||
"""
|
||||
# print
|
||||
# print value
|
||||
# print type(value)
|
||||
# print isinstance(value, list)
|
||||
# print
|
||||
if not isinstance(value, (list, tuple)):
|
||||
raise ValidationError('Only lists and tuples may be used in a '
|
||||
'list field')
|
||||
|
||||
try:
|
||||
[self.field._validate(item) for item in value]
|
||||
except:
|
||||
raise ValidationError('All items in a list field must be of the '
|
||||
'specified type')
|
||||
|
||||
Reference in New Issue
Block a user