Compare commits
5 Commits
unicode-em
...
get-collec
Author | SHA1 | Date | |
---|---|---|---|
|
3c8e1e5336 | ||
|
bd4a603e16 | ||
|
358b80d782 | ||
|
824ec42005 | ||
|
466935e9a3 |
@@ -6,6 +6,11 @@ Development
|
|||||||
===========
|
===========
|
||||||
- (Fill this out as you fix issues and develop your features).
|
- (Fill this out as you fix issues and develop your features).
|
||||||
|
|
||||||
|
Changes in 0.13.0
|
||||||
|
=================
|
||||||
|
- POTENTIAL BREAKING CHANGE: Added Unicode support to the `EmailField`, see
|
||||||
|
docs/upgrade.rst for details.
|
||||||
|
|
||||||
Changes in 0.12.0
|
Changes in 0.12.0
|
||||||
=================
|
=================
|
||||||
- POTENTIAL BREAKING CHANGE: Fixed limit/skip/hint/batch_size chaining #1476
|
- POTENTIAL BREAKING CHANGE: Fixed limit/skip/hint/batch_size chaining #1476
|
||||||
|
@@ -206,7 +206,10 @@ object::
|
|||||||
ross.last_name = 'Lawley'
|
ross.last_name = 'Lawley'
|
||||||
ross.save()
|
ross.save()
|
||||||
|
|
||||||
Now that we've got our user in the database, let's add a couple of posts::
|
Assign another user to a variable called ``john``, just like we did above with
|
||||||
|
``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.'
|
||||||
|
@@ -6,6 +6,17 @@ Development
|
|||||||
***********
|
***********
|
||||||
(Fill this out whenever you introduce breaking changes to MongoEngine)
|
(Fill this out whenever you introduce breaking changes to MongoEngine)
|
||||||
|
|
||||||
|
0.13.0
|
||||||
|
******
|
||||||
|
This release adds Unicode support to the `EmailField` and changes its
|
||||||
|
structure significantly. Previously, email addresses containing Unicode
|
||||||
|
characters didn't work at all. Starting with v0.13.0, domains with Unicode
|
||||||
|
characters are supported out of the box, meaning some emails that previously
|
||||||
|
didn't pass validation now do. Make sure the rest of your application can
|
||||||
|
accept such email addresses. Additionally, if you subclassed the `EmailField`
|
||||||
|
in your application and overrode `EmailField.EMAIL_REGEX`, you will have to
|
||||||
|
adjust your code to override `EmailField.USER_REGEX`, `EmailField.DOMAIN_REGEX`,
|
||||||
|
and potentially `EmailField.UTF8_USER_REGEX`.
|
||||||
|
|
||||||
0.12.0
|
0.12.0
|
||||||
******
|
******
|
||||||
|
@@ -23,7 +23,7 @@ __all__ = (list(document.__all__) + list(fields.__all__) +
|
|||||||
list(signals.__all__) + list(errors.__all__))
|
list(signals.__all__) + list(errors.__all__))
|
||||||
|
|
||||||
|
|
||||||
VERSION = (0, 12, 0)
|
VERSION = (0, 13, 0)
|
||||||
|
|
||||||
|
|
||||||
def get_version():
|
def get_version():
|
||||||
|
@@ -167,45 +167,63 @@ class Document(BaseDocument):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _get_collection(cls):
|
def _get_collection(cls):
|
||||||
"""Returns the collection for the document."""
|
"""Return a PyMongo 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()
|
|
||||||
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'):
|
|
||||||
# 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')
|
|
||||||
# Round up to next 256 bytes as MongoDB would do it to avoid exception
|
|
||||||
if max_size % 256:
|
|
||||||
max_size = (max_size // 256 + 1) * 256
|
|
||||||
|
|
||||||
if collection_name in db.collection_names():
|
# Get the collection, either capped or regular.
|
||||||
cls._collection = db[collection_name]
|
if cls._meta.get('max_size') or cls._meta.get('max_documents'):
|
||||||
# The collection already exists, check if its capped
|
cls._collection = cls._get_capped_collection()
|
||||||
# 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:
|
else:
|
||||||
|
db = cls._get_db()
|
||||||
|
collection_name = cls._get_collection_name()
|
||||||
cls._collection = db[collection_name]
|
cls._collection = db[collection_name]
|
||||||
|
|
||||||
|
# Ensure indexes on the collection unless auto_create_index was
|
||||||
|
# set to False.
|
||||||
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)
|
||||||
|
|
||||||
|
@@ -608,9 +608,9 @@ class EmbeddedDocumentField(BaseField):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, document_type, **kwargs):
|
def __init__(self, document_type, **kwargs):
|
||||||
if (
|
if not (
|
||||||
not isinstance(document_type, six.string_types) and
|
isinstance(document_type, six.string_types) or
|
||||||
not issubclass(document_type, EmbeddedDocument)
|
issubclass(document_type, EmbeddedDocument)
|
||||||
):
|
):
|
||||||
self.error('Invalid embedded document class provided to an '
|
self.error('Invalid embedded document class provided to an '
|
||||||
'EmbeddedDocumentField')
|
'EmbeddedDocumentField')
|
||||||
|
Reference in New Issue
Block a user