Merge branch 'dev' of https://github.com/alefnula/mongoengine into alefnula-dev
This commit is contained in:
commit
37818d2d72
5
.gitignore
vendored
5
.gitignore
vendored
@ -6,4 +6,7 @@ docs/_build
|
|||||||
build/
|
build/
|
||||||
dist/
|
dist/
|
||||||
mongoengine.egg-info/
|
mongoengine.egg-info/
|
||||||
env/
|
env/
|
||||||
|
.settings
|
||||||
|
.project
|
||||||
|
.pydevproject
|
@ -25,6 +25,12 @@ class BaseField(object):
|
|||||||
_index_with_types = True
|
_index_with_types = True
|
||||||
_geo_index = False
|
_geo_index = False
|
||||||
|
|
||||||
|
# These track each time a Field instance is created. Used to retain order.
|
||||||
|
# The auto_creation_counter is used for fields that MongoEngine implicitly
|
||||||
|
# creates, creation_counter is used for all user-specified fields.
|
||||||
|
creation_counter = 0
|
||||||
|
auto_creation_counter = -1
|
||||||
|
|
||||||
def __init__(self, db_field=None, name=None, required=False, default=None,
|
def __init__(self, db_field=None, name=None, required=False, default=None,
|
||||||
unique=False, unique_with=None, primary_key=False,
|
unique=False, unique_with=None, primary_key=False,
|
||||||
validation=None, choices=None):
|
validation=None, choices=None):
|
||||||
@ -41,6 +47,13 @@ class BaseField(object):
|
|||||||
self.primary_key = primary_key
|
self.primary_key = primary_key
|
||||||
self.validation = validation
|
self.validation = validation
|
||||||
self.choices = choices
|
self.choices = choices
|
||||||
|
# Adjust the appropriate creation counter, and save our local copy.
|
||||||
|
if self.db_field == '_id':
|
||||||
|
self.creation_counter = BaseField.auto_creation_counter
|
||||||
|
BaseField.auto_creation_counter -= 1
|
||||||
|
else:
|
||||||
|
self.creation_counter = BaseField.creation_counter
|
||||||
|
BaseField.creation_counter += 1
|
||||||
|
|
||||||
def __get__(self, instance, owner):
|
def __get__(self, instance, owner):
|
||||||
"""Descriptor for retrieving a value from a field in a document. Do
|
"""Descriptor for retrieving a value from a field in a document. Do
|
||||||
@ -87,9 +100,9 @@ class BaseField(object):
|
|||||||
def _validate(self, value):
|
def _validate(self, value):
|
||||||
# check choices
|
# check choices
|
||||||
if self.choices is not None:
|
if self.choices is not None:
|
||||||
if value not in self.choices:
|
option_keys = [option_key for option_key, option_value in self.choices]
|
||||||
raise ValidationError("Value must be one of %s."
|
if value not in option_keys:
|
||||||
% unicode(self.choices))
|
raise ValidationError("Value must be one of %s." % unicode(option_keys))
|
||||||
|
|
||||||
# check validation argument
|
# check validation argument
|
||||||
if self.validation is not None:
|
if self.validation is not None:
|
||||||
@ -241,8 +254,8 @@ class TopLevelDocumentMetaclass(DocumentMetaclass):
|
|||||||
|
|
||||||
# Propagate index options.
|
# Propagate index options.
|
||||||
for key in ('index_background', 'index_drop_dups', 'index_opts'):
|
for key in ('index_background', 'index_drop_dups', 'index_opts'):
|
||||||
if key in base._meta:
|
if key in base._meta:
|
||||||
base_meta[key] = base._meta[key]
|
base_meta[key] = base._meta[key]
|
||||||
|
|
||||||
id_field = id_field or base._meta.get('id_field')
|
id_field = id_field or base._meta.get('id_field')
|
||||||
base_indexes += base._meta.get('indexes', [])
|
base_indexes += base._meta.get('indexes', [])
|
||||||
@ -491,6 +504,7 @@ class BaseDocument(object):
|
|||||||
|
|
||||||
if sys.version_info < (2, 5):
|
if sys.version_info < (2, 5):
|
||||||
# Prior to Python 2.5, Exception was an old-style class
|
# Prior to Python 2.5, Exception was an old-style class
|
||||||
|
import types
|
||||||
def subclass_exception(name, parents, unused):
|
def subclass_exception(name, parents, unused):
|
||||||
return types.ClassType(name, parents, {})
|
return types.ClassType(name, parents, {})
|
||||||
else:
|
else:
|
||||||
|
@ -12,7 +12,6 @@ import datetime
|
|||||||
import decimal
|
import decimal
|
||||||
import gridfs
|
import gridfs
|
||||||
import warnings
|
import warnings
|
||||||
import types
|
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['StringField', 'IntField', 'FloatField', 'BooleanField',
|
__all__ = ['StringField', 'IntField', 'FloatField', 'BooleanField',
|
||||||
|
@ -644,7 +644,8 @@ class FieldTest(unittest.TestCase):
|
|||||||
"""Ensure that value is in a container of allowed values.
|
"""Ensure that value is in a container of allowed values.
|
||||||
"""
|
"""
|
||||||
class Shirt(Document):
|
class Shirt(Document):
|
||||||
size = StringField(max_length=3, choices=('S','M','L','XL','XXL'))
|
size = StringField(max_length=3, choices=(('S', 'Small'), ('M', 'Medium'), ('L', 'Large'),
|
||||||
|
('XL', 'Extra Large'), ('XXL', 'Extra Extra Large')))
|
||||||
|
|
||||||
Shirt.drop_collection()
|
Shirt.drop_collection()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user