general pep8 and more clean-up
This commit is contained in:
parent
4c80154437
commit
778c7dc5f2
@ -1,5 +1,6 @@
|
|||||||
import weakref
|
import weakref
|
||||||
import itertools
|
import itertools
|
||||||
|
|
||||||
from mongoengine.common import _import_class
|
from mongoengine.common import _import_class
|
||||||
from mongoengine.errors import DoesNotExist, MultipleObjectsReturned
|
from mongoengine.errors import DoesNotExist, MultipleObjectsReturned
|
||||||
|
|
||||||
@ -20,7 +21,7 @@ class BaseDict(dict):
|
|||||||
if isinstance(instance, (Document, EmbeddedDocument)):
|
if isinstance(instance, (Document, EmbeddedDocument)):
|
||||||
self._instance = weakref.proxy(instance)
|
self._instance = weakref.proxy(instance)
|
||||||
self._name = name
|
self._name = name
|
||||||
return super(BaseDict, self).__init__(dict_items)
|
super(BaseDict, self).__init__(dict_items)
|
||||||
|
|
||||||
def __getitem__(self, key, *args, **kwargs):
|
def __getitem__(self, key, *args, **kwargs):
|
||||||
value = super(BaseDict, self).__getitem__(key)
|
value = super(BaseDict, self).__getitem__(key)
|
||||||
@ -65,7 +66,7 @@ class BaseDict(dict):
|
|||||||
|
|
||||||
def clear(self, *args, **kwargs):
|
def clear(self, *args, **kwargs):
|
||||||
self._mark_as_changed()
|
self._mark_as_changed()
|
||||||
return super(BaseDict, self).clear(*args, **kwargs)
|
return super(BaseDict, self).clear()
|
||||||
|
|
||||||
def pop(self, *args, **kwargs):
|
def pop(self, *args, **kwargs):
|
||||||
self._mark_as_changed()
|
self._mark_as_changed()
|
||||||
@ -73,7 +74,7 @@ class BaseDict(dict):
|
|||||||
|
|
||||||
def popitem(self, *args, **kwargs):
|
def popitem(self, *args, **kwargs):
|
||||||
self._mark_as_changed()
|
self._mark_as_changed()
|
||||||
return super(BaseDict, self).popitem(*args, **kwargs)
|
return super(BaseDict, self).popitem()
|
||||||
|
|
||||||
def setdefault(self, *args, **kwargs):
|
def setdefault(self, *args, **kwargs):
|
||||||
self._mark_as_changed()
|
self._mark_as_changed()
|
||||||
@ -189,7 +190,7 @@ class BaseList(list):
|
|||||||
|
|
||||||
def reverse(self, *args, **kwargs):
|
def reverse(self, *args, **kwargs):
|
||||||
self._mark_as_changed()
|
self._mark_as_changed()
|
||||||
return super(BaseList, self).reverse(*args, **kwargs)
|
return super(BaseList, self).reverse()
|
||||||
|
|
||||||
def sort(self, *args, **kwargs):
|
def sort(self, *args, **kwargs):
|
||||||
self._mark_as_changed()
|
self._mark_as_changed()
|
||||||
@ -368,25 +369,31 @@ class StrictDict(object):
|
|||||||
__slots__ = ()
|
__slots__ = ()
|
||||||
_special_fields = set(['get', 'pop', 'iteritems', 'items', 'keys', 'create'])
|
_special_fields = set(['get', 'pop', 'iteritems', 'items', 'keys', 'create'])
|
||||||
_classes = {}
|
_classes = {}
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
for k,v in kwargs.iteritems():
|
for k, v in kwargs.iteritems():
|
||||||
setattr(self, k, v)
|
setattr(self, k, v)
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
key = '_reserved_' + key if key in self._special_fields else key
|
key = '_reserved_' + key if key in self._special_fields else key
|
||||||
try:
|
try:
|
||||||
return getattr(self, key)
|
return getattr(self, key)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
raise KeyError(key)
|
raise KeyError(key)
|
||||||
|
|
||||||
def __setitem__(self, key, value):
|
def __setitem__(self, key, value):
|
||||||
key = '_reserved_' + key if key in self._special_fields else key
|
key = '_reserved_' + key if key in self._special_fields else key
|
||||||
return setattr(self, key, value)
|
return setattr(self, key, value)
|
||||||
|
|
||||||
def __contains__(self, key):
|
def __contains__(self, key):
|
||||||
return hasattr(self, key)
|
return hasattr(self, key)
|
||||||
|
|
||||||
def get(self, key, default=None):
|
def get(self, key, default=None):
|
||||||
try:
|
try:
|
||||||
return self[key]
|
return self[key]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return default
|
return default
|
||||||
|
|
||||||
def pop(self, key, default=None):
|
def pop(self, key, default=None):
|
||||||
v = self.get(key, default)
|
v = self.get(key, default)
|
||||||
try:
|
try:
|
||||||
@ -394,19 +401,26 @@ class StrictDict(object):
|
|||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
return v
|
return v
|
||||||
|
|
||||||
def iteritems(self):
|
def iteritems(self):
|
||||||
for key in self:
|
for key in self:
|
||||||
yield key, self[key]
|
yield key, self[key]
|
||||||
|
|
||||||
def items(self):
|
def items(self):
|
||||||
return [(k, self[k]) for k in iter(self)]
|
return [(k, self[k]) for k in iter(self)]
|
||||||
|
|
||||||
def keys(self):
|
def keys(self):
|
||||||
return list(iter(self))
|
return list(iter(self))
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return (key for key in self.__slots__ if hasattr(self, key))
|
return (key for key in self.__slots__ if hasattr(self, key))
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return len(list(self.iteritems()))
|
return len(list(self.iteritems()))
|
||||||
|
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
return self.items() == other.items()
|
return self.items() == other.items()
|
||||||
|
|
||||||
def __neq__(self, other):
|
def __neq__(self, other):
|
||||||
return self.items() != other.items()
|
return self.items() != other.items()
|
||||||
|
|
||||||
@ -417,8 +431,10 @@ class StrictDict(object):
|
|||||||
if allowed_keys not in cls._classes:
|
if allowed_keys not in cls._classes:
|
||||||
class SpecificStrictDict(cls):
|
class SpecificStrictDict(cls):
|
||||||
__slots__ = allowed_keys_tuple
|
__slots__ = allowed_keys_tuple
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "{%s}" % ', '.join('"{0!s}": {0!r}'.format(k,v) for (k,v) in self.iteritems())
|
return "{%s}" % ', '.join('"{0!s}": {0!r}'.format(k, v) for (k, v) in self.iteritems())
|
||||||
|
|
||||||
cls._classes[allowed_keys] = SpecificStrictDict
|
cls._classes[allowed_keys] = SpecificStrictDict
|
||||||
return cls._classes[allowed_keys]
|
return cls._classes[allowed_keys]
|
||||||
|
|
||||||
@ -426,6 +442,7 @@ class StrictDict(object):
|
|||||||
class SemiStrictDict(StrictDict):
|
class SemiStrictDict(StrictDict):
|
||||||
__slots__ = ('_extras')
|
__slots__ = ('_extras')
|
||||||
_classes = {}
|
_classes = {}
|
||||||
|
|
||||||
def __getattr__(self, attr):
|
def __getattr__(self, attr):
|
||||||
try:
|
try:
|
||||||
super(SemiStrictDict, self).__getattr__(attr)
|
super(SemiStrictDict, self).__getattr__(attr)
|
||||||
@ -434,6 +451,7 @@ class SemiStrictDict(StrictDict):
|
|||||||
return self.__getattribute__('_extras')[attr]
|
return self.__getattribute__('_extras')[attr]
|
||||||
except KeyError as e:
|
except KeyError as e:
|
||||||
raise AttributeError(e)
|
raise AttributeError(e)
|
||||||
|
|
||||||
def __setattr__(self, attr, value):
|
def __setattr__(self, attr, value):
|
||||||
try:
|
try:
|
||||||
super(SemiStrictDict, self).__setattr__(attr, value)
|
super(SemiStrictDict, self).__setattr__(attr, value)
|
||||||
|
@ -14,7 +14,6 @@ from mongoengine.common import _import_class
|
|||||||
from mongoengine.errors import (ValidationError, InvalidDocumentError,
|
from mongoengine.errors import (ValidationError, InvalidDocumentError,
|
||||||
LookUpError, FieldDoesNotExist)
|
LookUpError, FieldDoesNotExist)
|
||||||
from mongoengine.python_support import PY3, txt_type
|
from mongoengine.python_support import PY3, txt_type
|
||||||
|
|
||||||
from mongoengine.base.common import get_document, ALLOW_INHERITANCE
|
from mongoengine.base.common import get_document, ALLOW_INHERITANCE
|
||||||
from mongoengine.base.datastructures import (
|
from mongoengine.base.datastructures import (
|
||||||
BaseDict,
|
BaseDict,
|
||||||
@ -420,7 +419,7 @@ class BaseDocument(object):
|
|||||||
:param use_db_field: Set to True by default but enables the output of the json structure with the field names and not the mongodb store db_names in case of set to False
|
:param use_db_field: Set to True by default but enables the output of the json structure with the field names and not the mongodb store db_names in case of set to False
|
||||||
"""
|
"""
|
||||||
use_db_field = kwargs.pop('use_db_field', True)
|
use_db_field = kwargs.pop('use_db_field', True)
|
||||||
return json_util.dumps(self.to_mongo(use_db_field), *args, **kwargs)
|
return json_util.dumps(self.to_mongo(use_db_field), *args, **kwargs)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_json(cls, json_data, created=False):
|
def from_json(cls, json_data, created=False):
|
||||||
@ -570,7 +569,7 @@ class BaseDocument(object):
|
|||||||
continue
|
continue
|
||||||
elif (isinstance(data, (EmbeddedDocument, DynamicEmbeddedDocument))
|
elif (isinstance(data, (EmbeddedDocument, DynamicEmbeddedDocument))
|
||||||
and db_field_name not in changed_fields):
|
and db_field_name not in changed_fields):
|
||||||
# Find all embedded fields that have been changed
|
# Find all embedded fields that have been changed
|
||||||
changed = data._get_changed_fields(inspected)
|
changed = data._get_changed_fields(inspected)
|
||||||
changed_fields += ["%s%s" % (key, k) for k in changed if k]
|
changed_fields += ["%s%s" % (key, k) for k in changed if k]
|
||||||
elif (isinstance(data, (list, tuple, dict)) and
|
elif (isinstance(data, (list, tuple, dict)) and
|
||||||
@ -621,7 +620,7 @@ class BaseDocument(object):
|
|||||||
else:
|
else:
|
||||||
set_data = doc
|
set_data = doc
|
||||||
if '_id' in set_data:
|
if '_id' in set_data:
|
||||||
del(set_data['_id'])
|
del set_data['_id']
|
||||||
|
|
||||||
# Determine if any changed items were actually unset.
|
# Determine if any changed items were actually unset.
|
||||||
for path, value in set_data.items():
|
for path, value in set_data.items():
|
||||||
@ -632,7 +631,7 @@ class BaseDocument(object):
|
|||||||
default = None
|
default = None
|
||||||
if (self._dynamic and len(parts) and parts[0] in
|
if (self._dynamic and len(parts) and parts[0] in
|
||||||
self._dynamic_fields):
|
self._dynamic_fields):
|
||||||
del(set_data[path])
|
del set_data[path]
|
||||||
unset_data[path] = 1
|
unset_data[path] = 1
|
||||||
continue
|
continue
|
||||||
elif path in self._fields:
|
elif path in self._fields:
|
||||||
@ -666,7 +665,7 @@ class BaseDocument(object):
|
|||||||
if default != value:
|
if default != value:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
del(set_data[path])
|
del set_data[path]
|
||||||
unset_data[path] = 1
|
unset_data[path] = 1
|
||||||
return set_data, unset_data
|
return set_data, unset_data
|
||||||
|
|
||||||
@ -821,7 +820,6 @@ class BaseDocument(object):
|
|||||||
parts = key.split('.')
|
parts = key.split('.')
|
||||||
if parts in (['pk'], ['id'], ['_id']):
|
if parts in (['pk'], ['id'], ['_id']):
|
||||||
key = '_id'
|
key = '_id'
|
||||||
fields = []
|
|
||||||
else:
|
else:
|
||||||
fields = cls._lookup_field(parts)
|
fields = cls._lookup_field(parts)
|
||||||
parts = []
|
parts = []
|
||||||
|
@ -7,7 +7,6 @@ import pymongo
|
|||||||
|
|
||||||
from mongoengine.common import _import_class
|
from mongoengine.common import _import_class
|
||||||
from mongoengine.errors import ValidationError
|
from mongoengine.errors import ValidationError
|
||||||
|
|
||||||
from mongoengine.base.common import ALLOW_INHERITANCE
|
from mongoengine.base.common import ALLOW_INHERITANCE
|
||||||
from mongoengine.base.datastructures import (
|
from mongoengine.base.datastructures import (
|
||||||
BaseDict, BaseList, EmbeddedDocumentList
|
BaseDict, BaseList, EmbeddedDocumentList
|
||||||
@ -23,7 +22,6 @@ UPDATE_OPERATORS = set(['set', 'unset', 'inc', 'dec', 'pop', 'push',
|
|||||||
|
|
||||||
|
|
||||||
class BaseField(object):
|
class BaseField(object):
|
||||||
|
|
||||||
"""A base class for fields in a MongoDB document. Instances of this class
|
"""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.
|
may be added to subclasses of `Document` to define a document's schema.
|
||||||
|
|
||||||
@ -212,7 +210,6 @@ class BaseField(object):
|
|||||||
|
|
||||||
|
|
||||||
class ComplexBaseField(BaseField):
|
class ComplexBaseField(BaseField):
|
||||||
|
|
||||||
"""Handles complex fields, such as lists / dictionaries.
|
"""Handles complex fields, such as lists / dictionaries.
|
||||||
|
|
||||||
Allows for nesting of embedded documents inside complex types.
|
Allows for nesting of embedded documents inside complex types.
|
||||||
@ -330,8 +327,8 @@ class ComplexBaseField(BaseField):
|
|||||||
return GenericReferenceField().to_mongo(value)
|
return GenericReferenceField().to_mongo(value)
|
||||||
cls = value.__class__
|
cls = value.__class__
|
||||||
val = value.to_mongo()
|
val = value.to_mongo()
|
||||||
# If we its a document thats not inherited add _cls
|
# If it's a document that is not inherited add _cls
|
||||||
if (isinstance(value, EmbeddedDocument)):
|
if isinstance(value, EmbeddedDocument):
|
||||||
val['_cls'] = cls.__name__
|
val['_cls'] = cls.__name__
|
||||||
return val
|
return val
|
||||||
|
|
||||||
@ -370,8 +367,8 @@ class ComplexBaseField(BaseField):
|
|||||||
elif hasattr(v, 'to_mongo'):
|
elif hasattr(v, 'to_mongo'):
|
||||||
cls = v.__class__
|
cls = v.__class__
|
||||||
val = v.to_mongo()
|
val = v.to_mongo()
|
||||||
# If we its a document thats not inherited add _cls
|
# If it's a document that is not inherited add _cls
|
||||||
if (isinstance(v, (Document, EmbeddedDocument))):
|
if isinstance(v, (Document, EmbeddedDocument)):
|
||||||
val['_cls'] = cls.__name__
|
val['_cls'] = cls.__name__
|
||||||
value_dict[k] = val
|
value_dict[k] = val
|
||||||
else:
|
else:
|
||||||
@ -422,7 +419,6 @@ class ComplexBaseField(BaseField):
|
|||||||
|
|
||||||
|
|
||||||
class ObjectIdField(BaseField):
|
class ObjectIdField(BaseField):
|
||||||
|
|
||||||
"""A field wrapper around MongoDB's ObjectIds.
|
"""A field wrapper around MongoDB's ObjectIds.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -454,7 +450,6 @@ class ObjectIdField(BaseField):
|
|||||||
|
|
||||||
|
|
||||||
class GeoJsonBaseField(BaseField):
|
class GeoJsonBaseField(BaseField):
|
||||||
|
|
||||||
"""A geo json field storing a geojson style object.
|
"""A geo json field storing a geojson style object.
|
||||||
|
|
||||||
.. versionadded:: 0.8
|
.. versionadded:: 0.8
|
||||||
|
@ -14,7 +14,6 @@ __all__ = ('DocumentMetaclass', 'TopLevelDocumentMetaclass')
|
|||||||
|
|
||||||
|
|
||||||
class DocumentMetaclass(type):
|
class DocumentMetaclass(type):
|
||||||
|
|
||||||
"""Metaclass for all documents.
|
"""Metaclass for all documents.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -144,7 +143,7 @@ class DocumentMetaclass(type):
|
|||||||
for base in document_bases:
|
for base in document_bases:
|
||||||
if _cls not in base._subclasses:
|
if _cls not in base._subclasses:
|
||||||
base._subclasses += (_cls,)
|
base._subclasses += (_cls,)
|
||||||
base._types = base._subclasses # TODO depreciate _types
|
base._types = base._subclasses # TODO depreciate _types
|
||||||
|
|
||||||
(Document, EmbeddedDocument, DictField,
|
(Document, EmbeddedDocument, DictField,
|
||||||
CachedReferenceField) = cls._import_classes()
|
CachedReferenceField) = cls._import_classes()
|
||||||
@ -250,7 +249,6 @@ class DocumentMetaclass(type):
|
|||||||
|
|
||||||
|
|
||||||
class TopLevelDocumentMetaclass(DocumentMetaclass):
|
class TopLevelDocumentMetaclass(DocumentMetaclass):
|
||||||
|
|
||||||
"""Metaclass for top-level documents (i.e. documents that have their own
|
"""Metaclass for top-level documents (i.e. documents that have their own
|
||||||
collection in the database.
|
collection in the database.
|
||||||
"""
|
"""
|
||||||
@ -260,7 +258,7 @@ class TopLevelDocumentMetaclass(DocumentMetaclass):
|
|||||||
super_new = super(TopLevelDocumentMetaclass, cls).__new__
|
super_new = super(TopLevelDocumentMetaclass, cls).__new__
|
||||||
|
|
||||||
# Set default _meta data if base class, otherwise get user defined meta
|
# Set default _meta data if base class, otherwise get user defined meta
|
||||||
if (attrs.get('my_metaclass') == TopLevelDocumentMetaclass):
|
if attrs.get('my_metaclass') == TopLevelDocumentMetaclass:
|
||||||
# defaults
|
# defaults
|
||||||
attrs['_meta'] = {
|
attrs['_meta'] = {
|
||||||
'abstract': True,
|
'abstract': True,
|
||||||
@ -279,7 +277,7 @@ class TopLevelDocumentMetaclass(DocumentMetaclass):
|
|||||||
attrs['_meta'].update(attrs.get('meta', {}))
|
attrs['_meta'].update(attrs.get('meta', {}))
|
||||||
else:
|
else:
|
||||||
attrs['_meta'] = attrs.get('meta', {})
|
attrs['_meta'] = attrs.get('meta', {})
|
||||||
# Explictly set abstract to false unless set
|
# Explicitly set abstract to false unless set
|
||||||
attrs['_meta']['abstract'] = attrs['_meta'].get('abstract', False)
|
attrs['_meta']['abstract'] = attrs['_meta'].get('abstract', False)
|
||||||
attrs['_is_base_cls'] = False
|
attrs['_is_base_cls'] = False
|
||||||
|
|
||||||
@ -294,7 +292,7 @@ class TopLevelDocumentMetaclass(DocumentMetaclass):
|
|||||||
|
|
||||||
# Clean up top level meta
|
# Clean up top level meta
|
||||||
if 'meta' in attrs:
|
if 'meta' in attrs:
|
||||||
del(attrs['meta'])
|
del attrs['meta']
|
||||||
|
|
||||||
# Find the parent document class
|
# Find the parent document class
|
||||||
parent_doc_cls = [b for b in flattened_bases
|
parent_doc_cls = [b for b in flattened_bases
|
||||||
@ -307,7 +305,7 @@ class TopLevelDocumentMetaclass(DocumentMetaclass):
|
|||||||
and not parent_doc_cls._meta.get('abstract', True)):
|
and not parent_doc_cls._meta.get('abstract', True)):
|
||||||
msg = "Trying to set a collection on a subclass (%s)" % name
|
msg = "Trying to set a collection on a subclass (%s)" % name
|
||||||
warnings.warn(msg, SyntaxWarning)
|
warnings.warn(msg, SyntaxWarning)
|
||||||
del(attrs['_meta']['collection'])
|
del attrs['_meta']['collection']
|
||||||
|
|
||||||
# Ensure abstract documents have abstract bases
|
# Ensure abstract documents have abstract bases
|
||||||
if attrs.get('_is_base_cls') or attrs['_meta'].get('abstract'):
|
if attrs.get('_is_base_cls') or attrs['_meta'].get('abstract'):
|
||||||
@ -425,7 +423,6 @@ class TopLevelDocumentMetaclass(DocumentMetaclass):
|
|||||||
|
|
||||||
|
|
||||||
class MetaDict(dict):
|
class MetaDict(dict):
|
||||||
|
|
||||||
"""Custom dictionary for meta classes.
|
"""Custom dictionary for meta classes.
|
||||||
Handles the merging of set indexes
|
Handles the merging of set indexes
|
||||||
"""
|
"""
|
||||||
@ -440,6 +437,5 @@ class MetaDict(dict):
|
|||||||
|
|
||||||
|
|
||||||
class BasesTuple(tuple):
|
class BasesTuple(tuple):
|
||||||
|
|
||||||
"""Special class to handle introspection of bases tuple in __new__"""
|
"""Special class to handle introspection of bases tuple in __new__"""
|
||||||
pass
|
pass
|
||||||
|
@ -120,7 +120,8 @@ def get_connection(alias=DEFAULT_CONNECTION_NAME, reconnect=False):
|
|||||||
try:
|
try:
|
||||||
connection = None
|
connection = None
|
||||||
# check for shared connections
|
# check for shared connections
|
||||||
connection_settings_iterator = ((db_alias, settings.copy()) for db_alias, settings in _connection_settings.iteritems())
|
connection_settings_iterator = (
|
||||||
|
(db_alias, settings.copy()) for db_alias, settings in _connection_settings.iteritems())
|
||||||
for db_alias, connection_settings in connection_settings_iterator:
|
for db_alias, connection_settings in connection_settings_iterator:
|
||||||
connection_settings.pop('name', None)
|
connection_settings.pop('name', None)
|
||||||
connection_settings.pop('username', None)
|
connection_settings.pop('username', None)
|
||||||
|
@ -11,7 +11,6 @@ from document import Document, EmbeddedDocument
|
|||||||
|
|
||||||
|
|
||||||
class DeReference(object):
|
class DeReference(object):
|
||||||
|
|
||||||
def __call__(self, items, max_depth=1, instance=None, name=None):
|
def __call__(self, items, max_depth=1, instance=None, name=None):
|
||||||
"""
|
"""
|
||||||
Cheaply dereferences the items to a set depth.
|
Cheaply dereferences the items to a set depth.
|
||||||
@ -49,8 +48,8 @@ class DeReference(object):
|
|||||||
|
|
||||||
if is_list and all([i.__class__ == doc_type for i in items]):
|
if is_list and all([i.__class__ == doc_type for i in items]):
|
||||||
return items
|
return items
|
||||||
elif not is_list and all([i.__class__ == doc_type
|
elif not is_list and all(
|
||||||
for i in items.values()]):
|
[i.__class__ == doc_type for i in items.values()]):
|
||||||
return items
|
return items
|
||||||
elif not field.dbref:
|
elif not field.dbref:
|
||||||
if not hasattr(items, 'items'):
|
if not hasattr(items, 'items'):
|
||||||
@ -155,7 +154,7 @@ class DeReference(object):
|
|||||||
elif doc_type is None:
|
elif doc_type is None:
|
||||||
doc = get_document(
|
doc = get_document(
|
||||||
''.join(x.capitalize()
|
''.join(x.capitalize()
|
||||||
for x in collection.split('_')))._from_son(ref)
|
for x in collection.split('_')))._from_son(ref)
|
||||||
else:
|
else:
|
||||||
doc = doc_type._from_son(ref)
|
doc = doc_type._from_son(ref)
|
||||||
object_map[(collection, doc.id)] = doc
|
object_map[(collection, doc.id)] = doc
|
||||||
|
@ -3,6 +3,7 @@ import pymongo
|
|||||||
import re
|
import re
|
||||||
|
|
||||||
from pymongo.read_preferences import ReadPreference
|
from pymongo.read_preferences import ReadPreference
|
||||||
|
from bson import ObjectId
|
||||||
from bson.dbref import DBRef
|
from bson.dbref import DBRef
|
||||||
from mongoengine import signals
|
from mongoengine import signals
|
||||||
from mongoengine.common import _import_class
|
from mongoengine.common import _import_class
|
||||||
@ -46,7 +47,6 @@ class InvalidCollectionError(Exception):
|
|||||||
|
|
||||||
|
|
||||||
class EmbeddedDocument(BaseDocument):
|
class EmbeddedDocument(BaseDocument):
|
||||||
|
|
||||||
"""A :class:`~mongoengine.Document` that isn't stored in its own
|
"""A :class:`~mongoengine.Document` that isn't stored in its own
|
||||||
collection. :class:`~mongoengine.EmbeddedDocument`\ s should be used as
|
collection. :class:`~mongoengine.EmbeddedDocument`\ s should be used as
|
||||||
fields on :class:`~mongoengine.Document`\ s through the
|
fields on :class:`~mongoengine.Document`\ s through the
|
||||||
@ -89,7 +89,6 @@ class EmbeddedDocument(BaseDocument):
|
|||||||
|
|
||||||
|
|
||||||
class Document(BaseDocument):
|
class Document(BaseDocument):
|
||||||
|
|
||||||
"""The base class used for defining the structure and properties of
|
"""The base class used for defining the structure and properties of
|
||||||
collections of documents stored in MongoDB. Inherit from this class, and
|
collections of documents stored in MongoDB. Inherit from this class, and
|
||||||
add fields as class attributes to define a document's structure.
|
add fields as class attributes to define a document's structure.
|
||||||
@ -160,7 +159,9 @@ class Document(BaseDocument):
|
|||||||
|
|
||||||
def fset(self, value):
|
def fset(self, value):
|
||||||
return setattr(self, self._meta['id_field'], value)
|
return setattr(self, self._meta['id_field'], value)
|
||||||
|
|
||||||
return property(fget, fset)
|
return property(fget, fset)
|
||||||
|
|
||||||
pk = pk()
|
pk = pk()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@ -190,7 +191,7 @@ class Document(BaseDocument):
|
|||||||
# options match the specified capped options
|
# options match the specified capped options
|
||||||
options = cls._collection.options()
|
options = cls._collection.options()
|
||||||
if options.get('max') != max_documents or \
|
if options.get('max') != max_documents or \
|
||||||
options.get('size') != max_size:
|
options.get('size') != max_size:
|
||||||
msg = (('Cannot create collection "%s" as a capped '
|
msg = (('Cannot create collection "%s" as a capped '
|
||||||
'collection as it already exists')
|
'collection as it already exists')
|
||||||
% cls._collection)
|
% cls._collection)
|
||||||
@ -248,7 +249,7 @@ class Document(BaseDocument):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
def save(self, force_insert=False, validate=True, clean=True,
|
def save(self, force_insert=False, validate=True, clean=True,
|
||||||
write_concern=None, cascade=None, cascade_kwargs=None,
|
write_concern=None, cascade=None, cascade_kwargs=None,
|
||||||
_refs=None, save_condition=None, **kwargs):
|
_refs=None, save_condition=None, **kwargs):
|
||||||
"""Save the :class:`~mongoengine.Document` to the database. If the
|
"""Save the :class:`~mongoengine.Document` to the database. If the
|
||||||
document already exists, it will be updated, otherwise it will be
|
document already exists, it will be updated, otherwise it will be
|
||||||
@ -455,7 +456,7 @@ class Document(BaseDocument):
|
|||||||
if kwargs.get('upsert', False):
|
if kwargs.get('upsert', False):
|
||||||
query = self.to_mongo()
|
query = self.to_mongo()
|
||||||
if "_cls" in query:
|
if "_cls" in query:
|
||||||
del(query["_cls"])
|
del query["_cls"]
|
||||||
return self._qs.filter(**query).update_one(**kwargs)
|
return self._qs.filter(**query).update_one(**kwargs)
|
||||||
else:
|
else:
|
||||||
raise OperationError(
|
raise OperationError(
|
||||||
@ -580,8 +581,8 @@ class Document(BaseDocument):
|
|||||||
if not self.pk:
|
if not self.pk:
|
||||||
raise self.DoesNotExist("Document does not exist")
|
raise self.DoesNotExist("Document does not exist")
|
||||||
obj = self._qs.read_preference(ReadPreference.PRIMARY).filter(
|
obj = self._qs.read_preference(ReadPreference.PRIMARY).filter(
|
||||||
**self._object_key).only(*fields).limit(1
|
**self._object_key).only(*fields).limit(
|
||||||
).select_related(max_depth=max_depth)
|
1).select_related(max_depth=max_depth)
|
||||||
|
|
||||||
if obj:
|
if obj:
|
||||||
obj = obj[0]
|
obj = obj[0]
|
||||||
@ -640,11 +641,11 @@ class Document(BaseDocument):
|
|||||||
for class_name in document_cls._subclasses
|
for class_name in document_cls._subclasses
|
||||||
if class_name != document_cls.__name__] + [document_cls]
|
if class_name != document_cls.__name__] + [document_cls]
|
||||||
|
|
||||||
for cls in classes:
|
for klass in classes:
|
||||||
for document_cls in documents:
|
for document_cls in documents:
|
||||||
delete_rules = cls._meta.get('delete_rules') or {}
|
delete_rules = klass._meta.get('delete_rules') or {}
|
||||||
delete_rules[(document_cls, field_name)] = rule
|
delete_rules[(document_cls, field_name)] = rule
|
||||||
cls._meta['delete_rules'] = delete_rules
|
klass._meta['delete_rules'] = delete_rules
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def drop_collection(cls):
|
def drop_collection(cls):
|
||||||
@ -769,7 +770,7 @@ class Document(BaseDocument):
|
|||||||
**index_opts)
|
**index_opts)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def list_indexes(cls, go_up=True, go_down=True):
|
def list_indexes(cls):
|
||||||
""" Lists all of the indexes that should be created for given
|
""" Lists all of the indexes that should be created for given
|
||||||
collection. It includes all the indexes from super- and sub-classes.
|
collection. It includes all the indexes from super- and sub-classes.
|
||||||
"""
|
"""
|
||||||
@ -816,8 +817,8 @@ class Document(BaseDocument):
|
|||||||
return indexes
|
return indexes
|
||||||
|
|
||||||
indexes = []
|
indexes = []
|
||||||
for cls in classes:
|
for klass in classes:
|
||||||
for index in get_indexes_spec(cls):
|
for index in get_indexes_spec(klass):
|
||||||
if index not in indexes:
|
if index not in indexes:
|
||||||
indexes.append(index)
|
indexes.append(index)
|
||||||
|
|
||||||
@ -856,7 +857,6 @@ class Document(BaseDocument):
|
|||||||
|
|
||||||
|
|
||||||
class DynamicDocument(Document):
|
class DynamicDocument(Document):
|
||||||
|
|
||||||
"""A Dynamic Document class allowing flexible, expandable and uncontrolled
|
"""A Dynamic Document class allowing flexible, expandable and uncontrolled
|
||||||
schemas. As a :class:`~mongoengine.Document` subclass, acts in the same
|
schemas. As a :class:`~mongoengine.Document` subclass, acts in the same
|
||||||
way as an ordinary document but has expando style properties. Any data
|
way as an ordinary document but has expando style properties. Any data
|
||||||
@ -888,7 +888,6 @@ class DynamicDocument(Document):
|
|||||||
|
|
||||||
|
|
||||||
class DynamicEmbeddedDocument(EmbeddedDocument):
|
class DynamicEmbeddedDocument(EmbeddedDocument):
|
||||||
|
|
||||||
"""A Dynamic Embedded Document class allowing flexible, expandable and
|
"""A Dynamic Embedded Document class allowing flexible, expandable and
|
||||||
uncontrolled schemas. See :class:`~mongoengine.DynamicDocument` for more
|
uncontrolled schemas. See :class:`~mongoengine.DynamicDocument` for more
|
||||||
information about dynamic documents.
|
information about dynamic documents.
|
||||||
@ -915,7 +914,6 @@ class DynamicEmbeddedDocument(EmbeddedDocument):
|
|||||||
|
|
||||||
|
|
||||||
class MapReduceDocument(object):
|
class MapReduceDocument(object):
|
||||||
|
|
||||||
"""A document returned from a map/reduce query.
|
"""A document returned from a map/reduce query.
|
||||||
|
|
||||||
:param collection: An instance of :class:`~pymongo.Collection`
|
:param collection: An instance of :class:`~pymongo.Collection`
|
||||||
|
@ -115,6 +115,7 @@ class ValidationError(AssertionError):
|
|||||||
else:
|
else:
|
||||||
return unicode(source)
|
return unicode(source)
|
||||||
return errors_dict
|
return errors_dict
|
||||||
|
|
||||||
if not self.errors:
|
if not self.errors:
|
||||||
return {}
|
return {}
|
||||||
return build_dict(self.errors)
|
return build_dict(self.errors)
|
||||||
@ -127,7 +128,7 @@ class ValidationError(AssertionError):
|
|||||||
value = ' '.join([generate_key(k) for k in value])
|
value = ' '.join([generate_key(k) for k in value])
|
||||||
if isinstance(value, dict):
|
if isinstance(value, dict):
|
||||||
value = ' '.join(
|
value = ' '.join(
|
||||||
[generate_key(v, k) for k, v in value.iteritems()])
|
[generate_key(v, k) for k, v in value.iteritems()])
|
||||||
|
|
||||||
results = "%s.%s" % (prefix, value) if prefix else value
|
results = "%s.%s" % (prefix, value) if prefix else value
|
||||||
return results
|
return results
|
||||||
|
@ -47,12 +47,10 @@ __all__ = [
|
|||||||
'SequenceField', 'UUIDField', 'MultiPointField', 'MultiLineStringField',
|
'SequenceField', 'UUIDField', 'MultiPointField', 'MultiLineStringField',
|
||||||
'MultiPolygonField', 'GeoJsonBaseField']
|
'MultiPolygonField', 'GeoJsonBaseField']
|
||||||
|
|
||||||
|
|
||||||
RECURSIVE_REFERENCE_CONSTANT = 'self'
|
RECURSIVE_REFERENCE_CONSTANT = 'self'
|
||||||
|
|
||||||
|
|
||||||
class StringField(BaseField):
|
class StringField(BaseField):
|
||||||
|
|
||||||
"""A unicode string field.
|
"""A unicode string field.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -112,7 +110,6 @@ class StringField(BaseField):
|
|||||||
|
|
||||||
|
|
||||||
class URLField(StringField):
|
class URLField(StringField):
|
||||||
|
|
||||||
"""A field that validates input as an URL.
|
"""A field that validates input as an URL.
|
||||||
|
|
||||||
.. versionadded:: 0.3
|
.. versionadded:: 0.3
|
||||||
@ -159,7 +156,6 @@ class URLField(StringField):
|
|||||||
|
|
||||||
|
|
||||||
class EmailField(StringField):
|
class EmailField(StringField):
|
||||||
|
|
||||||
"""A field that validates input as an E-Mail-Address.
|
"""A field that validates input as an E-Mail-Address.
|
||||||
|
|
||||||
.. versionadded:: 0.4
|
.. versionadded:: 0.4
|
||||||
@ -181,7 +177,6 @@ class EmailField(StringField):
|
|||||||
|
|
||||||
|
|
||||||
class IntField(BaseField):
|
class IntField(BaseField):
|
||||||
|
|
||||||
"""An 32-bit integer field.
|
"""An 32-bit integer field.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -216,7 +211,6 @@ class IntField(BaseField):
|
|||||||
|
|
||||||
|
|
||||||
class LongField(BaseField):
|
class LongField(BaseField):
|
||||||
|
|
||||||
"""An 64-bit integer field.
|
"""An 64-bit integer field.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -251,7 +245,6 @@ class LongField(BaseField):
|
|||||||
|
|
||||||
|
|
||||||
class FloatField(BaseField):
|
class FloatField(BaseField):
|
||||||
|
|
||||||
"""An floating point number field.
|
"""An floating point number field.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -286,7 +279,6 @@ class FloatField(BaseField):
|
|||||||
|
|
||||||
|
|
||||||
class DecimalField(BaseField):
|
class DecimalField(BaseField):
|
||||||
|
|
||||||
"""A fixed-point decimal number field.
|
"""A fixed-point decimal number field.
|
||||||
|
|
||||||
.. versionchanged:: 0.8
|
.. versionchanged:: 0.8
|
||||||
@ -360,7 +352,6 @@ class DecimalField(BaseField):
|
|||||||
|
|
||||||
|
|
||||||
class BooleanField(BaseField):
|
class BooleanField(BaseField):
|
||||||
|
|
||||||
"""A boolean field type.
|
"""A boolean field type.
|
||||||
|
|
||||||
.. versionadded:: 0.1.2
|
.. versionadded:: 0.1.2
|
||||||
@ -379,7 +370,6 @@ class BooleanField(BaseField):
|
|||||||
|
|
||||||
|
|
||||||
class DateTimeField(BaseField):
|
class DateTimeField(BaseField):
|
||||||
|
|
||||||
"""A datetime field.
|
"""A datetime field.
|
||||||
|
|
||||||
Uses the python-dateutil library if available alternatively use time.strptime
|
Uses the python-dateutil library if available alternatively use time.strptime
|
||||||
@ -447,7 +437,6 @@ class DateTimeField(BaseField):
|
|||||||
|
|
||||||
|
|
||||||
class ComplexDateTimeField(StringField):
|
class ComplexDateTimeField(StringField):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
ComplexDateTimeField handles microseconds exactly instead of rounding
|
ComplexDateTimeField handles microseconds exactly instead of rounding
|
||||||
like DateTimeField does.
|
like DateTimeField does.
|
||||||
@ -531,7 +520,6 @@ class ComplexDateTimeField(StringField):
|
|||||||
|
|
||||||
|
|
||||||
class EmbeddedDocumentField(BaseField):
|
class EmbeddedDocumentField(BaseField):
|
||||||
|
|
||||||
"""An embedded document field - with a declared document_type.
|
"""An embedded document field - with a declared document_type.
|
||||||
Only valid values are subclasses of :class:`~mongoengine.EmbeddedDocument`.
|
Only valid values are subclasses of :class:`~mongoengine.EmbeddedDocument`.
|
||||||
"""
|
"""
|
||||||
@ -585,7 +573,6 @@ class EmbeddedDocumentField(BaseField):
|
|||||||
|
|
||||||
|
|
||||||
class GenericEmbeddedDocumentField(BaseField):
|
class GenericEmbeddedDocumentField(BaseField):
|
||||||
|
|
||||||
"""A generic embedded document field - allows any
|
"""A generic embedded document field - allows any
|
||||||
:class:`~mongoengine.EmbeddedDocument` to be stored.
|
:class:`~mongoengine.EmbeddedDocument` to be stored.
|
||||||
|
|
||||||
@ -624,7 +611,6 @@ class GenericEmbeddedDocumentField(BaseField):
|
|||||||
|
|
||||||
|
|
||||||
class DynamicField(BaseField):
|
class DynamicField(BaseField):
|
||||||
|
|
||||||
"""A truly dynamic field type capable of handling different and varying
|
"""A truly dynamic field type capable of handling different and varying
|
||||||
types of data.
|
types of data.
|
||||||
|
|
||||||
@ -641,9 +627,9 @@ class DynamicField(BaseField):
|
|||||||
cls = value.__class__
|
cls = value.__class__
|
||||||
val = value.to_mongo()
|
val = value.to_mongo()
|
||||||
# If we its a document thats not inherited add _cls
|
# If we its a document thats not inherited add _cls
|
||||||
if (isinstance(value, Document)):
|
if isinstance(value, Document):
|
||||||
val = {"_ref": value.to_dbref(), "_cls": cls.__name__}
|
val = {"_ref": value.to_dbref(), "_cls": cls.__name__}
|
||||||
if (isinstance(value, EmbeddedDocument)):
|
if isinstance(value, EmbeddedDocument):
|
||||||
val['_cls'] = cls.__name__
|
val['_cls'] = cls.__name__
|
||||||
return val
|
return val
|
||||||
|
|
||||||
@ -678,7 +664,6 @@ class DynamicField(BaseField):
|
|||||||
|
|
||||||
def prepare_query_value(self, op, value):
|
def prepare_query_value(self, op, value):
|
||||||
if isinstance(value, basestring):
|
if isinstance(value, basestring):
|
||||||
from mongoengine.fields import StringField
|
|
||||||
return StringField().prepare_query_value(op, value)
|
return StringField().prepare_query_value(op, value)
|
||||||
return super(DynamicField, self).prepare_query_value(op, self.to_mongo(value))
|
return super(DynamicField, self).prepare_query_value(op, self.to_mongo(value))
|
||||||
|
|
||||||
@ -689,7 +674,6 @@ class DynamicField(BaseField):
|
|||||||
|
|
||||||
|
|
||||||
class ListField(ComplexBaseField):
|
class ListField(ComplexBaseField):
|
||||||
|
|
||||||
"""A list field that wraps a standard field, allowing multiple instances
|
"""A list field that wraps a standard field, allowing multiple instances
|
||||||
of the field to be used as a list in the database.
|
of the field to be used as a list in the database.
|
||||||
|
|
||||||
@ -749,7 +733,6 @@ class EmbeddedDocumentListField(ListField):
|
|||||||
|
|
||||||
|
|
||||||
class SortedListField(ListField):
|
class SortedListField(ListField):
|
||||||
|
|
||||||
"""A ListField that sorts the contents of its list before writing to
|
"""A ListField that sorts the contents of its list before writing to
|
||||||
the database in order to ensure that a sorted list is always
|
the database in order to ensure that a sorted list is always
|
||||||
retrieved.
|
retrieved.
|
||||||
@ -801,7 +784,6 @@ def key_has_dot_or_dollar(d):
|
|||||||
|
|
||||||
|
|
||||||
class DictField(ComplexBaseField):
|
class DictField(ComplexBaseField):
|
||||||
|
|
||||||
"""A dictionary field that wraps a standard Python dictionary. This is
|
"""A dictionary field that wraps a standard Python dictionary. This is
|
||||||
similar to an embedded document, but the structure is not defined.
|
similar to an embedded document, but the structure is not defined.
|
||||||
|
|
||||||
@ -857,7 +839,6 @@ class DictField(ComplexBaseField):
|
|||||||
|
|
||||||
|
|
||||||
class MapField(DictField):
|
class MapField(DictField):
|
||||||
|
|
||||||
"""A field that maps a name to a specified field type. Similar to
|
"""A field that maps a name to a specified field type. Similar to
|
||||||
a DictField, except the 'value' of each item must match the specified
|
a DictField, except the 'value' of each item must match the specified
|
||||||
field type.
|
field type.
|
||||||
@ -873,7 +854,6 @@ class MapField(DictField):
|
|||||||
|
|
||||||
|
|
||||||
class ReferenceField(BaseField):
|
class ReferenceField(BaseField):
|
||||||
|
|
||||||
"""A reference to a document that will be automatically dereferenced on
|
"""A reference to a document that will be automatically dereferenced on
|
||||||
access (lazily).
|
access (lazily).
|
||||||
|
|
||||||
@ -1010,7 +990,6 @@ class ReferenceField(BaseField):
|
|||||||
|
|
||||||
|
|
||||||
class CachedReferenceField(BaseField):
|
class CachedReferenceField(BaseField):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
A referencefield with cache fields to purpose pseudo-joins
|
A referencefield with cache fields to purpose pseudo-joins
|
||||||
|
|
||||||
@ -1025,7 +1004,6 @@ class CachedReferenceField(BaseField):
|
|||||||
"""
|
"""
|
||||||
if not isinstance(document_type, basestring) and \
|
if not isinstance(document_type, basestring) and \
|
||||||
not issubclass(document_type, (Document, basestring)):
|
not issubclass(document_type, (Document, basestring)):
|
||||||
|
|
||||||
self.error('Argument to CachedReferenceField constructor must be a'
|
self.error('Argument to CachedReferenceField constructor must be a'
|
||||||
' document class or a string')
|
' document class or a string')
|
||||||
|
|
||||||
@ -1036,6 +1014,7 @@ class CachedReferenceField(BaseField):
|
|||||||
|
|
||||||
def start_listener(self):
|
def start_listener(self):
|
||||||
from mongoengine import signals
|
from mongoengine import signals
|
||||||
|
|
||||||
signals.post_save.connect(self.on_document_pre_save,
|
signals.post_save.connect(self.on_document_pre_save,
|
||||||
sender=self.document_type)
|
sender=self.document_type)
|
||||||
|
|
||||||
@ -1089,7 +1068,6 @@ class CachedReferenceField(BaseField):
|
|||||||
def to_mongo(self, document):
|
def to_mongo(self, document):
|
||||||
id_field_name = self.document_type._meta['id_field']
|
id_field_name = self.document_type._meta['id_field']
|
||||||
id_field = self.document_type._fields[id_field_name]
|
id_field = self.document_type._fields[id_field_name]
|
||||||
doc_tipe = self.document_type
|
|
||||||
|
|
||||||
if isinstance(document, Document):
|
if isinstance(document, Document):
|
||||||
# We need the id from the saved object to create the DBRef
|
# We need the id from the saved object to create the DBRef
|
||||||
@ -1099,6 +1077,7 @@ class CachedReferenceField(BaseField):
|
|||||||
' been saved to the database')
|
' been saved to the database')
|
||||||
else:
|
else:
|
||||||
self.error('Only accept a document object')
|
self.error('Only accept a document object')
|
||||||
|
# TODO: should raise here or will fail next statement
|
||||||
|
|
||||||
value = SON((
|
value = SON((
|
||||||
("_id", id_field.to_mongo(id_)),
|
("_id", id_field.to_mongo(id_)),
|
||||||
@ -1150,7 +1129,6 @@ class CachedReferenceField(BaseField):
|
|||||||
|
|
||||||
|
|
||||||
class GenericReferenceField(BaseField):
|
class GenericReferenceField(BaseField):
|
||||||
|
|
||||||
"""A reference to *any* :class:`~mongoengine.document.Document` subclass
|
"""A reference to *any* :class:`~mongoengine.document.Document` subclass
|
||||||
that will be automatically dereferenced on access (lazily).
|
that will be automatically dereferenced on access (lazily).
|
||||||
|
|
||||||
@ -1232,7 +1210,6 @@ class GenericReferenceField(BaseField):
|
|||||||
|
|
||||||
|
|
||||||
class BinaryField(BaseField):
|
class BinaryField(BaseField):
|
||||||
|
|
||||||
"""A binary data field.
|
"""A binary data field.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -1264,7 +1241,6 @@ class GridFSError(Exception):
|
|||||||
|
|
||||||
|
|
||||||
class GridFSProxy(object):
|
class GridFSProxy(object):
|
||||||
|
|
||||||
"""Proxy object to handle writing and reading of files to and from GridFS
|
"""Proxy object to handle writing and reading of files to and from GridFS
|
||||||
|
|
||||||
.. versionadded:: 0.4
|
.. versionadded:: 0.4
|
||||||
@ -1278,12 +1254,12 @@ class GridFSProxy(object):
|
|||||||
instance=None,
|
instance=None,
|
||||||
db_alias=DEFAULT_CONNECTION_NAME,
|
db_alias=DEFAULT_CONNECTION_NAME,
|
||||||
collection_name='fs'):
|
collection_name='fs'):
|
||||||
self.grid_id = grid_id # Store GridFS id for file
|
self.grid_id = grid_id # Store GridFS id for file
|
||||||
self.key = key
|
self.key = key
|
||||||
self.instance = instance
|
self.instance = instance
|
||||||
self.db_alias = db_alias
|
self.db_alias = db_alias
|
||||||
self.collection_name = collection_name
|
self.collection_name = collection_name
|
||||||
self.newfile = None # Used for partial writes
|
self.newfile = None # Used for partial writes
|
||||||
self.gridout = None
|
self.gridout = None
|
||||||
|
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
@ -1410,7 +1386,6 @@ class GridFSProxy(object):
|
|||||||
|
|
||||||
|
|
||||||
class FileField(BaseField):
|
class FileField(BaseField):
|
||||||
|
|
||||||
"""A GridFS storage field.
|
"""A GridFS storage field.
|
||||||
|
|
||||||
.. versionadded:: 0.4
|
.. versionadded:: 0.4
|
||||||
@ -1444,7 +1419,7 @@ class FileField(BaseField):
|
|||||||
def __set__(self, instance, value):
|
def __set__(self, instance, value):
|
||||||
key = self.name
|
key = self.name
|
||||||
if ((hasattr(value, 'read') and not
|
if ((hasattr(value, 'read') and not
|
||||||
isinstance(value, GridFSProxy)) or isinstance(value, str_types)):
|
isinstance(value, GridFSProxy)) or isinstance(value, str_types)):
|
||||||
# using "FileField() = file/string" notation
|
# using "FileField() = file/string" notation
|
||||||
grid_file = instance._data.get(self.name)
|
grid_file = instance._data.get(self.name)
|
||||||
# If a file already exists, delete it
|
# If a file already exists, delete it
|
||||||
@ -1494,7 +1469,6 @@ class FileField(BaseField):
|
|||||||
|
|
||||||
|
|
||||||
class ImageGridFsProxy(GridFSProxy):
|
class ImageGridFsProxy(GridFSProxy):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Proxy for ImageField
|
Proxy for ImageField
|
||||||
|
|
||||||
@ -1518,6 +1492,7 @@ class ImageGridFsProxy(GridFSProxy):
|
|||||||
raise ValidationError('Invalid image: %s' % e)
|
raise ValidationError('Invalid image: %s' % e)
|
||||||
|
|
||||||
# Progressive JPEG
|
# Progressive JPEG
|
||||||
|
# TODO: fixme, at least unused, at worst bad implementation
|
||||||
progressive = img.info.get('progressive') or False
|
progressive = img.info.get('progressive') or False
|
||||||
|
|
||||||
if (kwargs.get('progressive') and
|
if (kwargs.get('progressive') and
|
||||||
@ -1633,7 +1608,6 @@ class ImproperlyConfigured(Exception):
|
|||||||
|
|
||||||
|
|
||||||
class ImageField(FileField):
|
class ImageField(FileField):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
A Image File storage field.
|
A Image File storage field.
|
||||||
|
|
||||||
@ -1672,7 +1646,6 @@ class ImageField(FileField):
|
|||||||
|
|
||||||
|
|
||||||
class SequenceField(BaseField):
|
class SequenceField(BaseField):
|
||||||
|
|
||||||
"""Provides a sequential counter see:
|
"""Provides a sequential counter see:
|
||||||
http://www.mongodb.org/display/DOCS/Object+IDs#ObjectIDs-SequenceNumbers
|
http://www.mongodb.org/display/DOCS/Object+IDs#ObjectIDs-SequenceNumbers
|
||||||
|
|
||||||
@ -1796,7 +1769,6 @@ class SequenceField(BaseField):
|
|||||||
|
|
||||||
|
|
||||||
class UUIDField(BaseField):
|
class UUIDField(BaseField):
|
||||||
|
|
||||||
"""A UUID field.
|
"""A UUID field.
|
||||||
|
|
||||||
.. versionadded:: 0.6
|
.. versionadded:: 0.6
|
||||||
@ -1843,13 +1815,12 @@ class UUIDField(BaseField):
|
|||||||
if not isinstance(value, basestring):
|
if not isinstance(value, basestring):
|
||||||
value = str(value)
|
value = str(value)
|
||||||
try:
|
try:
|
||||||
value = uuid.UUID(value)
|
uuid.UUID(value)
|
||||||
except Exception, exc:
|
except Exception, exc:
|
||||||
self.error('Could not convert to UUID: %s' % exc)
|
self.error('Could not convert to UUID: %s' % exc)
|
||||||
|
|
||||||
|
|
||||||
class GeoPointField(BaseField):
|
class GeoPointField(BaseField):
|
||||||
|
|
||||||
"""A list storing a longitude and latitude coordinate.
|
"""A list storing a longitude and latitude coordinate.
|
||||||
|
|
||||||
.. note:: this represents a generic point in a 2D plane and a legacy way of
|
.. note:: this represents a generic point in a 2D plane and a legacy way of
|
||||||
@ -1879,7 +1850,6 @@ class GeoPointField(BaseField):
|
|||||||
|
|
||||||
|
|
||||||
class PointField(GeoJsonBaseField):
|
class PointField(GeoJsonBaseField):
|
||||||
|
|
||||||
"""A GeoJSON field storing a longitude and latitude coordinate.
|
"""A GeoJSON field storing a longitude and latitude coordinate.
|
||||||
|
|
||||||
The data is represented as:
|
The data is represented as:
|
||||||
@ -1900,7 +1870,6 @@ class PointField(GeoJsonBaseField):
|
|||||||
|
|
||||||
|
|
||||||
class LineStringField(GeoJsonBaseField):
|
class LineStringField(GeoJsonBaseField):
|
||||||
|
|
||||||
"""A GeoJSON field storing a line of longitude and latitude coordinates.
|
"""A GeoJSON field storing a line of longitude and latitude coordinates.
|
||||||
|
|
||||||
The data is represented as:
|
The data is represented as:
|
||||||
@ -1920,7 +1889,6 @@ class LineStringField(GeoJsonBaseField):
|
|||||||
|
|
||||||
|
|
||||||
class PolygonField(GeoJsonBaseField):
|
class PolygonField(GeoJsonBaseField):
|
||||||
|
|
||||||
"""A GeoJSON field storing a polygon of longitude and latitude coordinates.
|
"""A GeoJSON field storing a polygon of longitude and latitude coordinates.
|
||||||
|
|
||||||
The data is represented as:
|
The data is represented as:
|
||||||
@ -1943,7 +1911,6 @@ class PolygonField(GeoJsonBaseField):
|
|||||||
|
|
||||||
|
|
||||||
class MultiPointField(GeoJsonBaseField):
|
class MultiPointField(GeoJsonBaseField):
|
||||||
|
|
||||||
"""A GeoJSON field storing a list of Points.
|
"""A GeoJSON field storing a list of Points.
|
||||||
|
|
||||||
The data is represented as:
|
The data is represented as:
|
||||||
@ -1964,7 +1931,6 @@ class MultiPointField(GeoJsonBaseField):
|
|||||||
|
|
||||||
|
|
||||||
class MultiLineStringField(GeoJsonBaseField):
|
class MultiLineStringField(GeoJsonBaseField):
|
||||||
|
|
||||||
"""A GeoJSON field storing a list of LineStrings.
|
"""A GeoJSON field storing a list of LineStrings.
|
||||||
|
|
||||||
The data is represented as:
|
The data is represented as:
|
||||||
@ -1985,7 +1951,6 @@ class MultiLineStringField(GeoJsonBaseField):
|
|||||||
|
|
||||||
|
|
||||||
class MultiPolygonField(GeoJsonBaseField):
|
class MultiPolygonField(GeoJsonBaseField):
|
||||||
|
|
||||||
"""A GeoJSON field storing list of Polygons.
|
"""A GeoJSON field storing list of Polygons.
|
||||||
|
|
||||||
The data is represented as:
|
The data is represented as:
|
||||||
|
@ -14,6 +14,7 @@ PY3 = sys.version_info[0] == 3
|
|||||||
if PY3:
|
if PY3:
|
||||||
import codecs
|
import codecs
|
||||||
from io import BytesIO as StringIO
|
from io import BytesIO as StringIO
|
||||||
|
|
||||||
# return s converted to binary. b('test') should be equivalent to b'test'
|
# return s converted to binary. b('test') should be equivalent to b'test'
|
||||||
def b(s):
|
def b(s):
|
||||||
return codecs.latin_1_encode(s)[0]
|
return codecs.latin_1_encode(s)[0]
|
||||||
|
@ -43,7 +43,6 @@ RE_TYPE = type(re.compile(''))
|
|||||||
|
|
||||||
|
|
||||||
class BaseQuerySet(object):
|
class BaseQuerySet(object):
|
||||||
|
|
||||||
"""A set of results returned from a query. Wraps a MongoDB cursor,
|
"""A set of results returned from a query. Wraps a MongoDB cursor,
|
||||||
providing :class:`~mongoengine.Document` objects as the results.
|
providing :class:`~mongoengine.Document` objects as the results.
|
||||||
"""
|
"""
|
||||||
@ -162,8 +161,8 @@ class BaseQuerySet(object):
|
|||||||
if queryset._as_pymongo:
|
if queryset._as_pymongo:
|
||||||
return queryset._get_as_pymongo(queryset._cursor[key])
|
return queryset._get_as_pymongo(queryset._cursor[key])
|
||||||
return queryset._document._from_son(queryset._cursor[key],
|
return queryset._document._from_son(queryset._cursor[key],
|
||||||
_auto_dereference=self._auto_dereference,
|
_auto_dereference=self._auto_dereference,
|
||||||
only_fields=self.only_fields)
|
only_fields=self.only_fields)
|
||||||
|
|
||||||
raise AttributeError
|
raise AttributeError
|
||||||
|
|
||||||
@ -597,9 +596,10 @@ class BaseQuerySet(object):
|
|||||||
doc_map[doc['_id']] = self._get_as_pymongo(doc)
|
doc_map[doc['_id']] = self._get_as_pymongo(doc)
|
||||||
else:
|
else:
|
||||||
for doc in docs:
|
for doc in docs:
|
||||||
doc_map[doc['_id']] = self._document._from_son(doc,
|
doc_map[doc['_id']] = self._document._from_son(
|
||||||
only_fields=self.only_fields,
|
doc,
|
||||||
_auto_dereference=self._auto_dereference)
|
only_fields=self.only_fields,
|
||||||
|
_auto_dereference=self._auto_dereference)
|
||||||
|
|
||||||
return doc_map
|
return doc_map
|
||||||
|
|
||||||
@ -830,7 +830,6 @@ class BaseQuerySet(object):
|
|||||||
cleaned_fields = []
|
cleaned_fields = []
|
||||||
for key, value in kwargs.items():
|
for key, value in kwargs.items():
|
||||||
parts = key.split('__')
|
parts = key.split('__')
|
||||||
op = None
|
|
||||||
if parts[0] in operators:
|
if parts[0] in operators:
|
||||||
op = parts.pop(0)
|
op = parts.pop(0)
|
||||||
value = {'$' + op: value}
|
value = {'$' + op: value}
|
||||||
@ -1616,7 +1615,7 @@ class BaseQuerySet(object):
|
|||||||
|
|
||||||
return frequencies
|
return frequencies
|
||||||
|
|
||||||
def _fields_to_dbfields(self, fields, subdoc=False):
|
def _fields_to_dbfields(self, fields):
|
||||||
"""Translate fields paths to its db equivalents"""
|
"""Translate fields paths to its db equivalents"""
|
||||||
ret = []
|
ret = []
|
||||||
subclasses = []
|
subclasses = []
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
__all__ = ('QueryFieldList',)
|
__all__ = ('QueryFieldList',)
|
||||||
|
|
||||||
|
|
||||||
|
@ -61,7 +61,6 @@ class QuerySet(BaseQuerySet):
|
|||||||
data[-1] = "...(remaining elements truncated)..."
|
data[-1] = "...(remaining elements truncated)..."
|
||||||
return repr(data)
|
return repr(data)
|
||||||
|
|
||||||
|
|
||||||
def _iter_results(self):
|
def _iter_results(self):
|
||||||
"""A generator for iterating over the result cache.
|
"""A generator for iterating over the result cache.
|
||||||
|
|
||||||
@ -74,7 +73,7 @@ class QuerySet(BaseQuerySet):
|
|||||||
upper = len(self._result_cache)
|
upper = len(self._result_cache)
|
||||||
while pos < upper:
|
while pos < upper:
|
||||||
yield self._result_cache[pos]
|
yield self._result_cache[pos]
|
||||||
pos = pos + 1
|
pos += 1
|
||||||
if not self._has_more:
|
if not self._has_more:
|
||||||
raise StopIteration
|
raise StopIteration
|
||||||
if len(self._result_cache) <= pos:
|
if len(self._result_cache) <= pos:
|
||||||
|
@ -11,7 +11,6 @@ from mongoengine.python_support import IS_PYMONGO_3
|
|||||||
|
|
||||||
__all__ = ('query', 'update')
|
__all__ = ('query', 'update')
|
||||||
|
|
||||||
|
|
||||||
COMPARISON_OPERATORS = ('ne', 'gt', 'gte', 'lt', 'lte', 'in', 'nin', 'mod',
|
COMPARISON_OPERATORS = ('ne', 'gt', 'gte', 'lt', 'lte', 'in', 'nin', 'mod',
|
||||||
'all', 'size', 'exists', 'not', 'elemMatch', 'type')
|
'all', 'size', 'exists', 'not', 'elemMatch', 'type')
|
||||||
GEO_OPERATORS = ('within_distance', 'within_spherical_distance',
|
GEO_OPERATORS = ('within_distance', 'within_spherical_distance',
|
||||||
@ -27,7 +26,7 @@ MATCH_OPERATORS = (COMPARISON_OPERATORS + GEO_OPERATORS +
|
|||||||
STRING_OPERATORS + CUSTOM_OPERATORS)
|
STRING_OPERATORS + CUSTOM_OPERATORS)
|
||||||
|
|
||||||
|
|
||||||
def query(_doc_cls=None, _field_operation=False, **query):
|
def query(_doc_cls=None, **query):
|
||||||
"""Transform a query from Django-style format to Mongo format.
|
"""Transform a query from Django-style format to Mongo format.
|
||||||
"""
|
"""
|
||||||
mongo_query = {}
|
mongo_query = {}
|
||||||
@ -359,6 +358,7 @@ def _infer_geometry(value):
|
|||||||
raise InvalidQueryError("Invalid $geometry dictionary should have "
|
raise InvalidQueryError("Invalid $geometry dictionary should have "
|
||||||
"type and coordinates keys")
|
"type and coordinates keys")
|
||||||
elif isinstance(value, (list, set)):
|
elif isinstance(value, (list, set)):
|
||||||
|
# TODO: shouldn't we test value[0][0][0][0] to see if it is MultiPolygon?
|
||||||
try:
|
try:
|
||||||
value[0][0][0]
|
value[0][0][0]
|
||||||
return {"$geometry": {"type": "Polygon", "coordinates": value}}
|
return {"$geometry": {"type": "Polygon", "coordinates": value}}
|
||||||
|
@ -6,6 +6,7 @@ __all__ = ['pre_init', 'post_init', 'pre_save', 'pre_save_post_validation',
|
|||||||
signals_available = False
|
signals_available = False
|
||||||
try:
|
try:
|
||||||
from blinker import Namespace
|
from blinker import Namespace
|
||||||
|
|
||||||
signals_available = True
|
signals_available = True
|
||||||
except ImportError:
|
except ImportError:
|
||||||
class Namespace(object):
|
class Namespace(object):
|
||||||
@ -27,6 +28,7 @@ except ImportError:
|
|||||||
raise RuntimeError('signalling support is unavailable '
|
raise RuntimeError('signalling support is unavailable '
|
||||||
'because the blinker library is '
|
'because the blinker library is '
|
||||||
'not installed.')
|
'not installed.')
|
||||||
|
|
||||||
send = lambda *a, **kw: None
|
send = lambda *a, **kw: None
|
||||||
connect = disconnect = has_receivers_for = receivers_for = \
|
connect = disconnect = has_receivers_for = receivers_for = \
|
||||||
temporarily_connected_to = _fail
|
temporarily_connected_to = _fail
|
||||||
|
Loading…
x
Reference in New Issue
Block a user