Added a cleaner way to get collection names
Also handles dynamic collection naming - refs #180.
This commit is contained in:
@@ -11,7 +11,7 @@ import itertools
|
||||
import operator
|
||||
|
||||
__all__ = ['queryset_manager', 'Q', 'InvalidQueryError',
|
||||
'InvalidCollectionError', 'DO_NOTHING', 'NULLIFY', 'CASCADE', 'DENY']
|
||||
'DO_NOTHING', 'NULLIFY', 'CASCADE', 'DENY']
|
||||
|
||||
|
||||
# The maximum number of items to display in a QuerySet.__repr__
|
||||
@@ -40,10 +40,6 @@ class OperationError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidCollectionError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
RE_TYPE = type(re.compile(''))
|
||||
|
||||
|
||||
@@ -1360,7 +1356,7 @@ class QuerySet(object):
|
||||
|
||||
fields = [QuerySet._translate_field_name(self._document, f)
|
||||
for f in fields]
|
||||
collection = self._document._meta['collection']
|
||||
collection = self._document._get_collection_name()
|
||||
|
||||
scope = {
|
||||
'collection': collection,
|
||||
@@ -1550,39 +1546,9 @@ class QuerySetManager(object):
|
||||
# Document class being used rather than a document object
|
||||
return self
|
||||
|
||||
db = _get_db()
|
||||
collection = owner._meta['collection']
|
||||
if (db, collection) not in self._collections:
|
||||
# Create collection as a capped collection if specified
|
||||
if owner._meta['max_size'] or owner._meta['max_documents']:
|
||||
# Get max document limit and max byte size from meta
|
||||
max_size = owner._meta['max_size'] or 10000000 # 10MB default
|
||||
max_documents = owner._meta['max_documents']
|
||||
|
||||
if collection in db.collection_names():
|
||||
self._collections[(db, collection)] = db[collection]
|
||||
# The collection already exists, check if its capped
|
||||
# options match the specified capped options
|
||||
options = self._collections[(db, 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') % 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._collections[(db, collection)] = db.create_collection(
|
||||
collection, **opts
|
||||
)
|
||||
else:
|
||||
self._collections[(db, collection)] = db[collection]
|
||||
|
||||
# owner is the document that contains the QuerySetManager
|
||||
queryset_class = owner._meta['queryset_class'] or QuerySet
|
||||
queryset = queryset_class(owner, self._collections[(db, collection)])
|
||||
queryset = queryset_class(owner, owner._get_collection())
|
||||
if self.get_queryset:
|
||||
if self.get_queryset.func_code.co_argcount == 1:
|
||||
queryset = self.get_queryset(queryset)
|
||||
|
||||
Reference in New Issue
Block a user