Made _cls etc optional, merged sort to order_by

This commit is contained in:
Harry Marr
2009-12-19 02:33:01 +00:00
parent 551b2755d4
commit 9d12dbad70
4 changed files with 78 additions and 46 deletions

View File

@@ -136,17 +136,32 @@ class TopLevelDocumentMetaclass(DocumentMetaclass):
if attrs.get('__metaclass__') == TopLevelDocumentMetaclass:
return super_new(cls, name, bases, attrs)
collection = attrs.get('__collection__', name.lower())
collection = name.lower()
simple_class = True
# Subclassed documents inherit collection from superclass
for base in bases:
if hasattr(base, '_meta') and 'collection' in base._meta:
# Ensure that the Document class may be subclassed -
# inheritance may be disabled to remove dependency on
# additional fields _cls and _types
if base._meta.get('allow_inheritance', True) == False:
raise ValueError('Document %s may not be subclassed' %
base.__name__)
else:
simple_class = False
collection = base._meta['collection']
meta = {
'collection': collection,
'allow_inheritance': True,
}
meta.update(attrs.get('meta', {}))
# Only simple classes - direct subclasses of Document - may set
# allow_inheritance to False
if not simple_class and not meta['allow_inheritance']:
raise ValueError('Only direct subclasses of Document may set '
'"allow_inheritance" to False')
attrs['_meta'] = meta
attrs['id'] = ObjectIdField(name='_id')
@@ -228,8 +243,11 @@ class BaseDocument(object):
value = getattr(self, field_name, None)
if value is not None:
data[field.name] = field.to_mongo(value)
data['_cls'] = self._class_name
data['_types'] = self._superclasses.keys() + [self._class_name]
# Only add _cls and _types if allow_inheritance is not False
if not (hasattr(self, '_meta') and
self._meta.get('allow_inheritance', True) == False):
data['_cls'] = self._class_name
data['_types'] = self._superclasses.keys() + [self._class_name]
return data
@classmethod
@@ -242,6 +260,9 @@ class BaseDocument(object):
data = dict((str(key), value) for key, value in son.items())
if '_types' in data:
del data['_types']
if '_cls' in data:
del data['_cls']

View File

@@ -12,8 +12,11 @@ class QuerySet(object):
self._document = document
self._collection = collection
self._query = {}
# If inheritance is allowed, only return instances and instances of
# subclasses of the class being used
if document._meta.get('allow_inheritance'):
self._query = {'_types': self._document._class_name}
self._cursor_obj = None
self._ordering = []
def ensure_index(self, key_or_list, direction=None):
"""Ensure that the given indexes are in place.
@@ -105,43 +108,9 @@ class QuerySet(object):
"""
self._cursor.skip(n)
return self
def order_by(self, *params):
"""Apply ordering conditions, Django-style.
e.g., ``Model.objects.().order_by("-published_date", "ordering")``
will order first by ``published_date DESC``, and then ``ordering ASC``.
"""
if not params:
self._ordering = []
for param in params:
if param.startswith("-"):
param = param[1:]
sort_dir = pymongo.DESCENDING
else:
sort_dir = pymongo.ASCENDING
sort_rule = (param, sort_dir)
if not sort_rule in self._ordering:
self._ordering.append(sort_rule)
self._cursor.sort(self._ordering)
return self
def explain(self, format=False):
plan = self._cursor.explain()
if format:
import pprint
plan = pprint.pformat(plan)
return plan
def delete(self):
"""Delete the documents matched by the query.
"""
self._collection.remove(self._query)
def sort(self, *keys):
"""Sort the QuerySet by the keys. The order may be specified by
def order_by(self, *keys):
"""Order the QuerySet by the keys. The order may be specified by
prepending each of the keys by a + or a -. Ascending order is assumed.
"""
key_list = []
@@ -155,6 +124,18 @@ class QuerySet(object):
self._cursor.sort(key_list)
return self
def explain(self, format=False):
plan = self._cursor.explain()
if format:
import pprint
plan = pprint.pformat(plan)
return plan
def delete(self):
"""Delete the documents matched by the query.
"""
self._collection.remove(self._query)
def __iter__(self):
return self