Syntax cleaning
This commit is contained in:
parent
216f15602b
commit
112e921ce2
@ -87,43 +87,43 @@ class Document(BaseDocument):
|
||||
return property(fget, fset)
|
||||
|
||||
@classmethod
|
||||
def _get_db(self):
|
||||
def _get_db(cls):
|
||||
"""Some Model using other db_alias"""
|
||||
return get_db(self._meta.get("db_alias", DEFAULT_CONNECTION_NAME ))
|
||||
return get_db(cls._meta.get("db_alias", DEFAULT_CONNECTION_NAME ))
|
||||
|
||||
@classmethod
|
||||
def _get_collection(self):
|
||||
def _get_collection(cls):
|
||||
"""Returns the collection for the document."""
|
||||
if not hasattr(self, '_collection') or self._collection is None:
|
||||
db = self._get_db()
|
||||
collection_name = self._get_collection_name()
|
||||
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 self._meta['max_size'] or self._meta['max_documents']:
|
||||
if cls._meta['max_size'] or cls._meta['max_documents']:
|
||||
# Get max document limit and max byte size from meta
|
||||
max_size = self._meta['max_size'] or 10000000 # 10MB default
|
||||
max_documents = self._meta['max_documents']
|
||||
max_size = cls._meta['max_size'] or 10000000 # 10MB default
|
||||
max_documents = cls._meta['max_documents']
|
||||
|
||||
if collection_name in db.collection_names():
|
||||
self._collection = db[collection_name]
|
||||
cls._collection = db[collection_name]
|
||||
# The collection already exists, check if its capped
|
||||
# options match the specified capped options
|
||||
options = self._collection.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') % self._collection
|
||||
'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
|
||||
self._collection = db.create_collection(
|
||||
cls._collection = db.create_collection(
|
||||
collection_name, **opts
|
||||
)
|
||||
else:
|
||||
self._collection = db[collection_name]
|
||||
return self._collection
|
||||
cls._collection = db[collection_name]
|
||||
return cls._collection
|
||||
|
||||
def save(self, safe=True, force_insert=False, validate=True, write_options=None,
|
||||
cascade=None, cascade_kwargs=None, _refs=None):
|
||||
|
@ -2586,6 +2586,7 @@ class DocumentTest(unittest.TestCase):
|
||||
|
||||
def test_db_ref_usage(self):
|
||||
""" DB Ref usage in __raw__ queries """
|
||||
|
||||
class User(Document):
|
||||
name = StringField()
|
||||
|
||||
@ -2608,50 +2609,52 @@ class DocumentTest(unittest.TestCase):
|
||||
Book.drop_collection()
|
||||
|
||||
# Authors
|
||||
bob = User.objects.create(name = "Bob")
|
||||
jon = User.objects.create(name = "Jon")
|
||||
bob = User.objects.create(name="Bob")
|
||||
jon = User.objects.create(name="Jon")
|
||||
|
||||
# Redactors
|
||||
karl = User.objects.create(name = "Karl")
|
||||
susan = User.objects.create(name = "Susan")
|
||||
peter = User.objects.create(name = "Peter")
|
||||
karl = User.objects.create(name="Karl")
|
||||
susan = User.objects.create(name="Susan")
|
||||
peter = User.objects.create(name="Peter")
|
||||
|
||||
# Bob
|
||||
Book.objects.create(name = "1", author = bob, extra = {"a": bob.to_dbref(), "b" : [karl.to_dbref(), susan.to_dbref()]} )
|
||||
Book.objects.create(name = "2", author = bob, extra = {"a": bob.to_dbref(), "b" : karl.to_dbref()} )
|
||||
Book.objects.create(name = "3", author = bob, extra = {"a": bob.to_dbref(), "c" : [jon.to_dbref(), peter.to_dbref()] })
|
||||
Book.objects.create(name = "4", author = bob)
|
||||
Book.objects.create(name="1", author=bob, extra={"a": bob.to_dbref(), "b": [karl.to_dbref(), susan.to_dbref()]})
|
||||
Book.objects.create(name="2", author=bob, extra={"a": bob.to_dbref(), "b": karl.to_dbref()} )
|
||||
Book.objects.create(name="3", author=bob, extra={"a": bob.to_dbref(), "c": [jon.to_dbref(), peter.to_dbref()]})
|
||||
Book.objects.create(name="4", author=bob)
|
||||
|
||||
# Jon
|
||||
Book.objects.create(name = "5", author = jon)
|
||||
Book.objects.create(name = "6", author = peter)
|
||||
Book.objects.create(name = "7", author = jon)
|
||||
Book.objects.create(name = "8", author = jon)
|
||||
Book.objects.create(name = "9", author = jon, extra = {"a": peter.to_dbref()})
|
||||
Book.objects.create(name="5", author=jon)
|
||||
Book.objects.create(name="6", author=peter)
|
||||
Book.objects.create(name="7", author=jon)
|
||||
Book.objects.create(name="8", author=jon)
|
||||
Book.objects.create(name="9", author=jon, extra={"a": peter.to_dbref()})
|
||||
|
||||
# Checks
|
||||
self.assertEqual(u",".join([str(b) for b in Book.objects.all()] ) , "1,2,3,4,5,6,7,8,9" )
|
||||
# bob related books
|
||||
self.assertEqual(u",".join([str(b) for b in Book.objects.filter(
|
||||
Q(extra__a = bob ) |
|
||||
Q(author = bob) |
|
||||
Q(extra__b = bob ) )] ) ,
|
||||
"1,2,3,4" )
|
||||
Q(extra__a=bob ) |
|
||||
Q(author=bob) |
|
||||
Q(extra__b=bob))]) ,
|
||||
"1,2,3,4")
|
||||
|
||||
# Susan & Karl related books
|
||||
self.assertEqual(u",".join([str(b) for b in Book.objects.filter(
|
||||
Q(extra__a__all = [karl, susan] ) |
|
||||
Q(author__all = [karl, susan ] ) |
|
||||
Q(extra__b__all = [karl.to_dbref(), susan.to_dbref()] )
|
||||
Q(extra__a__all=[karl, susan] ) |
|
||||
Q(author__all=[karl, susan ] ) |
|
||||
Q(extra__b__all=[karl.to_dbref(), susan.to_dbref()] )
|
||||
) ] ) , "1" )
|
||||
|
||||
# $Where
|
||||
self.assertEqual(u",".join([str(b) for b in Book.objects.filter(
|
||||
__raw__ = {
|
||||
"$where" : """function(){ return this.name == '1' || this.name == '2'; } """
|
||||
__raw__={
|
||||
"$where": """
|
||||
function(){
|
||||
return this.name == '1' ||
|
||||
this.name == '2';}"""
|
||||
}
|
||||
) ] ) , "1,2" )
|
||||
|
||||
) ]), "1,2")
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
Loading…
x
Reference in New Issue
Block a user