Compare commits

..

2 Commits

Author SHA1 Message Date
Georg K
f5fec0f374 feat: update code collection check 2022-02-03 20:16:03 +03:00
jar3b
dffcc0992d fix: imports to match v0.13 to python3.10 2021-11-05 18:10:25 +03:00
4 changed files with 39 additions and 60 deletions

View File

@@ -206,10 +206,7 @@ object::
ross.last_name = 'Lawley' ross.last_name = 'Lawley'
ross.save() ross.save()
Assign another user to a variable called ``john``, just like we did above with Now that we've got our user in the database, let's add a couple of posts::
``ross``.
Now that we've got our users in the database, let's add a couple of posts::
post1 = TextPost(title='Fun with MongoEngine', author=john) post1 = TextPost(title='Fun with MongoEngine', author=john)
post1.content = 'Took a look at MongoEngine today, looks pretty cool.' post1.content = 'Took a look at MongoEngine today, looks pretty cool.'

View File

@@ -1,6 +1,6 @@
import copy import copy
import numbers import numbers
from collections import Hashable from collections.abc import Hashable
from functools import partial from functools import partial
from bson import ObjectId, json_util from bson import ObjectId, json_util

View File

@@ -167,63 +167,45 @@ class Document(BaseDocument):
@classmethod @classmethod
def _get_collection(cls): def _get_collection(cls):
"""Return a PyMongo collection for the document.""" """Returns the collection for the document."""
# TODO: use new get_collection() with PyMongo3 ?
if not hasattr(cls, '_collection') or cls._collection is None: if not hasattr(cls, '_collection') or cls._collection is None:
db = cls._get_db()
# Get the collection, either capped or regular. collection_name = cls._get_collection_name()
# Create collection as a capped collection if specified
if cls._meta.get('max_size') or cls._meta.get('max_documents'): if cls._meta.get('max_size') or cls._meta.get('max_documents'):
cls._collection = cls._get_capped_collection() # Get max document limit and max byte size from meta
else: max_size = cls._meta.get('max_size') or 10 * 2 ** 20 # 10MB default
db = cls._get_db() max_documents = cls._meta.get('max_documents')
collection_name = cls._get_collection_name() # Round up to next 256 bytes as MongoDB would do it to avoid exception
cls._collection = db[collection_name] if max_size % 256:
max_size = (max_size // 256 + 1) * 256
# Ensure indexes on the collection unless auto_create_index was if collection_name in db.collection_names():
# set to False. cls._collection = db[collection_name]
# The collection already exists, check if its capped
# options match the specified capped options
options = cls._collection.options()
if options.get('max') != max_documents or \
options.get('size') != max_size:
msg = (('Cannot create collection "%s" as a capped '
'collection as it already exists')
% cls._collection)
raise InvalidCollectionError(msg)
else:
# Create the collection as a capped collection
opts = {'capped': True, 'size': max_size}
if max_documents:
opts['max'] = max_documents
cls._collection = db.create_collection(
collection_name, **opts
)
else:
cls._collection = db[collection_name]
if cls._meta.get('auto_create_index', True): if cls._meta.get('auto_create_index', True):
cls.ensure_indexes() cls.ensure_indexes()
return cls._collection return cls._collection
@classmethod
def _get_capped_collection(cls):
"""Create a new or get an existing capped PyMongo collection."""
db = cls._get_db()
collection_name = cls._get_collection_name()
# Get max document limit and max byte size from meta.
max_size = cls._meta.get('max_size') or 10 * 2 ** 20 # 10MB default
max_documents = cls._meta.get('max_documents')
# MongoDB will automatically raise the size to make it a multiple of
# 256 bytes. We raise it here ourselves to be able to reliably compare
# the options below.
if max_size % 256:
max_size = (max_size // 256 + 1) * 256
# If the collection already exists and has different options
# (i.e. isn't capped or has different max/size), raise an error.
if collection_name in db.collection_names():
collection = db[collection_name]
options = collection.options()
if (
options.get('max') != max_documents or
options.get('size') != max_size
):
raise InvalidCollectionError(
'Cannot create collection "{}" as a capped '
'collection as it already exists'.format(cls._collection)
)
return collection
# Create a new capped collection.
opts = {'capped': True, 'size': max_size}
if max_documents:
opts['max'] = max_documents
return db.create_collection(collection_name, **opts)
def to_mongo(self, *args, **kwargs): def to_mongo(self, *args, **kwargs):
data = super(Document, self).to_mongo(*args, **kwargs) data = super(Document, self).to_mongo(*args, **kwargs)
@@ -826,7 +808,7 @@ class Document(BaseDocument):
collection = cls._get_collection() collection = cls._get_collection()
# 746: when connection is via mongos, the read preference is not necessarily an indication that # 746: when connection is via mongos, the read preference is not necessarily an indication that
# this code runs on a secondary # this code runs on a secondary
if not collection.is_mongos and collection.read_preference > 1: if collection.is_mongos is not None and collection.read_preference.mode > 1:
return return
# determine if an index which we are creating includes # determine if an index which we are creating includes

View File

@@ -6,7 +6,7 @@ import socket
import time import time
import uuid import uuid
import warnings import warnings
from collections import Mapping from collections.abc import Mapping
from operator import itemgetter from operator import itemgetter
from bson import Binary, DBRef, ObjectId, SON from bson import Binary, DBRef, ObjectId, SON
@@ -608,9 +608,9 @@ class EmbeddedDocumentField(BaseField):
""" """
def __init__(self, document_type, **kwargs): def __init__(self, document_type, **kwargs):
if not ( if (
isinstance(document_type, six.string_types) or not isinstance(document_type, six.string_types) and
issubclass(document_type, EmbeddedDocument) not issubclass(document_type, EmbeddedDocument)
): ):
self.error('Invalid embedded document class provided to an ' self.error('Invalid embedded document class provided to an '
'EmbeddedDocumentField') 'EmbeddedDocumentField')