Restructured files to avoid circular imports

This commit is contained in:
Harry Marr
2009-11-16 13:00:37 +00:00
parent af38a92ec9
commit 768bffd67e
3 changed files with 123 additions and 112 deletions

View File

@@ -1,85 +1,20 @@
from base import BaseField, ValidationError
from document import EmbeddedDocument
import re
__all__ = ['StringField', 'IntField', 'ValidationError']
class ValidationError(Exception):
pass
class Field(object):
"""A base class for fields in a MongoDB document. Instances of this class
may be added to subclasses of `Document` to define a document's schema.
"""
def __init__(self, name=None, default=None):
self.name = name
self.default = default
def __get__(self, instance, owner):
"""Descriptor for retrieving a value from a field in a document. Do
any necessary conversion between Python and MongoDB types.
"""
if instance is None:
# Document class being used rather than a document object
return self
# Get value from document instance if available, if not use default
value = instance._data.get(self.name)
if value is not None:
value = self._to_python(value)
elif self.default is not None:
value = self.default
if callable(value):
value = value()
return value
def __set__(self, instance, value):
"""Descriptor for assigning a value to a field in a document. Do any
necessary conversion between Python and MongoDB types.
"""
if value is not None:
try:
value = self._to_python(value)
self._validate(value)
value = self._to_mongo(value)
except ValueError:
raise ValidationError('Invalid value for field of type "' +
self.__class__.__name__ + '"')
instance._data[self.name] = value
def _to_python(self, value):
"""Convert a MongoDB-compatible type to a Python type.
"""
return unicode(value)
def _to_mongo(self, value):
"""Convert a Python type to a MongoDB-compatible type.
"""
return self._to_python(value)
def _validate(self, value):
"""Perform validation on a value.
"""
return value
class NestedDocumentField(Field):
"""A nested document field. Only valid values are subclasses of
NestedDocument.
"""
pass
class StringField(Field):
class StringField(BaseField):
"""A unicode string field.
"""
def __init__(self, regex=None, max_length=None, **kwargs):
self.regex = re.compile(regex) if regex else None
self.max_length = max_length
Field.__init__(self, **kwargs)
BaseField.__init__(self, **kwargs)
def _validate(self, value):
if self.max_length is not None and len(value) > self.max_length:
@@ -90,13 +25,13 @@ class StringField(Field):
raise ValidationError(message)
class IntField(Field):
class IntField(BaseField):
"""An integer field.
"""
def __init__(self, min_value=None, max_value=None, **kwargs):
self.min_value, self.max_value = min_value, max_value
Field.__init__(self, **kwargs)
BaseField.__init__(self, **kwargs)
def _to_python(self, value):
return int(value)
@@ -107,3 +42,10 @@ class IntField(Field):
if self.max_value is not None and value > self.max_value:
raise ValidationError('Integer value is too large')
class EmbeddedDocumentField(BaseField):
"""An embedded document field. Only valid values are subclasses of
EmbeddedDocument.
"""
pass