made compatable with python 2.5

This commit is contained in:
Laine
2012-08-02 15:30:21 -07:00
parent 91aa90ad4a
commit ed74477150
8 changed files with 94 additions and 40 deletions

View File

@@ -877,6 +877,10 @@ class BaseDocument(object):
if not is_list and '_cls' in value:
cls = get_document(value['_cls'])
# Make keys str instead of unicode
for key,val in value.items():
del value[key]
value[str(key)] = val
value = cls(**value)
value._dynamic = True
value._changed_fields = []
@@ -998,6 +1002,10 @@ class BaseDocument(object):
raise InvalidDocumentError("""
Invalid data to create a `%s` instance.\n%s""".strip() % (cls._class_name, errors))
# Make all keys str instead of unicode
for key,val in data.items():
del data[key]
data[str(key)] = val
obj = cls(**data)
obj._changed_fields = changed_fields

View File

@@ -1267,8 +1267,8 @@ class SequenceField(IntField):
"""
Generate and Increment the counter
"""
sequence_id = "{0}.{1}".format(self.owner_document._get_collection_name(),
self.name)
sequence_id = "%s.%s"%(self.owner_document._get_collection_name(),
self.name)
collection = get_db(alias = self.db_alias )[self.collection_name]
counter = collection.find_and_modify(query={"_id": sequence_id},
update={"$inc": {"next": 1}},

View File

@@ -4,6 +4,7 @@ import copy
import itertools
import operator
import functools
import sys
from functools import partial
import pymongo
@@ -14,6 +15,18 @@ from mongoengine import signals
__all__ = ['queryset_manager', 'Q', 'InvalidQueryError',
'DO_NOTHING', 'NULLIFY', 'CASCADE', 'DENY', 'PULL']
if sys.version_info < (2,6,0):
def product(*args, **kwds):
pools = map(tuple, args) * kwds.get('repeat', 1)
result = [[]]
for pool in pools:
result = [x+[y] for x in result for y in pool]
for prod in result:
yield tuple(prod)
else:
from itertools import product
from functools import reduce
# The maximum number of items to display in a QuerySet.__repr__
REPR_OUTPUT_SIZE = 20
@@ -120,13 +133,13 @@ class QueryTreeTransformerVisitor(QNodeVisitor):
# the necessary parts. Then for each $or part, create a new query
# that ANDs the necessary part with the $or part.
clauses = []
for or_group in itertools.product(*or_groups):
q_object = functools.reduce(lambda a, b: a & b, and_parts, Q())
q_object = functools.reduce(lambda a, b: a & b, or_group, q_object)
for or_group in product(*or_groups):
q_object = reduce(lambda a, b: a & b, and_parts, Q())
q_object = reduce(lambda a, b: a & b, or_group, q_object)
clauses.append(q_object)
# Finally, $or the generated clauses in to one query. Each of the
# clauses is sufficient for the query to succeed.
return functools.reduce(lambda a, b: a | b, clauses, Q())
return reduce(lambda a, b: a | b, clauses, Q())
if combination.operation == combination.OR:
children = []