Syntax cleaning

This commit is contained in:
Ross Lawley 2011-12-07 01:34:36 -08:00
parent 216f15602b
commit 112e921ce2
2 changed files with 43 additions and 40 deletions

View File

@ -87,43 +87,43 @@ class Document(BaseDocument):
return property(fget, fset) return property(fget, fset)
@classmethod @classmethod
def _get_db(self): def _get_db(cls):
"""Some Model using other db_alias""" """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 @classmethod
def _get_collection(self): def _get_collection(cls):
"""Returns the collection for the document.""" """Returns the collection for the document."""
if not hasattr(self, '_collection') or self._collection is None: if not hasattr(cls, '_collection') or cls._collection is None:
db = self._get_db() db = cls._get_db()
collection_name = self._get_collection_name() collection_name = cls._get_collection_name()
# Create collection as a capped collection if specified # 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 # Get max document limit and max byte size from meta
max_size = self._meta['max_size'] or 10000000 # 10MB default max_size = cls._meta['max_size'] or 10000000 # 10MB default
max_documents = self._meta['max_documents'] max_documents = cls._meta['max_documents']
if collection_name in db.collection_names(): 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 # The collection already exists, check if its capped
# options match the specified capped options # options match the specified capped options
options = self._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') % self._collection 'collection as it already exists') % cls._collection
raise InvalidCollectionError(msg) raise InvalidCollectionError(msg)
else: else:
# Create the collection as a capped collection # Create the collection as a capped collection
opts = {'capped': True, 'size': max_size} opts = {'capped': True, 'size': max_size}
if max_documents: if max_documents:
opts['max'] = max_documents opts['max'] = max_documents
self._collection = db.create_collection( cls._collection = db.create_collection(
collection_name, **opts collection_name, **opts
) )
else: else:
self._collection = db[collection_name] cls._collection = db[collection_name]
return self._collection return cls._collection
def save(self, safe=True, force_insert=False, validate=True, write_options=None, def save(self, safe=True, force_insert=False, validate=True, write_options=None,
cascade=None, cascade_kwargs=None, _refs=None): cascade=None, cascade_kwargs=None, _refs=None):

View File

@ -2586,6 +2586,7 @@ class DocumentTest(unittest.TestCase):
def test_db_ref_usage(self): def test_db_ref_usage(self):
""" DB Ref usage in __raw__ queries """ """ DB Ref usage in __raw__ queries """
class User(Document): class User(Document):
name = StringField() name = StringField()
@ -2608,50 +2609,52 @@ class DocumentTest(unittest.TestCase):
Book.drop_collection() Book.drop_collection()
# Authors # Authors
bob = User.objects.create(name = "Bob") bob = User.objects.create(name="Bob")
jon = User.objects.create(name = "Jon") jon = User.objects.create(name="Jon")
# Redactors # Redactors
karl = User.objects.create(name = "Karl") karl = User.objects.create(name="Karl")
susan = User.objects.create(name = "Susan") susan = User.objects.create(name="Susan")
peter = User.objects.create(name = "Peter") peter = User.objects.create(name="Peter")
# Bob # Bob
Book.objects.create(name = "1", author = bob, extra = {"a": bob.to_dbref(), "b" : [karl.to_dbref(), susan.to_dbref()]} ) 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="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="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="4", author=bob)
# Jon # Jon
Book.objects.create(name = "5", author = jon) Book.objects.create(name="5", author=jon)
Book.objects.create(name = "6", author = peter) Book.objects.create(name="6", author=peter)
Book.objects.create(name = "7", author = jon) Book.objects.create(name="7", author=jon)
Book.objects.create(name = "8", 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="9", author=jon, extra={"a": peter.to_dbref()})
# Checks # Checks
self.assertEqual(u",".join([str(b) for b in Book.objects.all()] ) , "1,2,3,4,5,6,7,8,9" ) self.assertEqual(u",".join([str(b) for b in Book.objects.all()] ) , "1,2,3,4,5,6,7,8,9" )
# bob related books # bob related books
self.assertEqual(u",".join([str(b) for b in Book.objects.filter( self.assertEqual(u",".join([str(b) for b in Book.objects.filter(
Q(extra__a = bob ) | Q(extra__a=bob ) |
Q(author = bob) | Q(author=bob) |
Q(extra__b = bob ) )] ) , Q(extra__b=bob))]) ,
"1,2,3,4" ) "1,2,3,4")
# Susan & Karl related books # Susan & Karl related books
self.assertEqual(u",".join([str(b) for b in Book.objects.filter( self.assertEqual(u",".join([str(b) for b in Book.objects.filter(
Q(extra__a__all = [karl, susan] ) | Q(extra__a__all=[karl, susan] ) |
Q(author__all = [karl, susan ] ) | Q(author__all=[karl, susan ] ) |
Q(extra__b__all = [karl.to_dbref(), susan.to_dbref()] ) Q(extra__b__all=[karl.to_dbref(), susan.to_dbref()] )
) ] ) , "1" ) ) ] ) , "1" )
# $Where # $Where
self.assertEqual(u",".join([str(b) for b in Book.objects.filter( self.assertEqual(u",".join([str(b) for b in Book.objects.filter(
__raw__ = { __raw__={
"$where" : """function(){ return this.name == '1' || this.name == '2'; } """ "$where": """
function(){
return this.name == '1' ||
this.name == '2';}"""
} }
) ] ) , "1,2" ) ) ]), "1,2")
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()