Merge branch 'master' of git://github.com/flosch/mongoengine into v0.4

Conflicts:
	tests/fields.py
This commit is contained in:
Harry Marr 2010-08-30 13:21:10 +01:00
commit 40eb23a97a
6 changed files with 27 additions and 7 deletions

3
.gitignore vendored
View File

@ -4,4 +4,5 @@ docs/.build
docs/_build docs/_build
build/ build/
dist/ dist/
mongoengine.egg-info/ mongoengine.egg-info/
env/

View File

@ -410,8 +410,6 @@ class BaseDocument(object):
value = getattr(self, field_name, None) value = getattr(self, field_name, None)
if value is not None: if value is not None:
data[field.db_field] = field.to_mongo(value) data[field.db_field] = field.to_mongo(value)
else:
data[field.db_field] = None
# Only add _cls and _types if allow_inheritance is not False # Only add _cls and _types if allow_inheritance is not False
if not (hasattr(self, '_meta') and if not (hasattr(self, '_meta') and
self._meta.get('allow_inheritance', True) == False): self._meta.get('allow_inheritance', True) == False):
@ -454,7 +452,8 @@ class BaseDocument(object):
for field_name, field in cls._fields.items(): for field_name, field in cls._fields.items():
if field.db_field in data: if field.db_field in data:
value = data[field.db_field] value = data[field.db_field]
data[field_name] = value if value is None else field.to_python(value) data[field_name] = (value if value is None
else field.to_python(value))
obj = cls(**data) obj = cls(**data)
obj._present_fields = present_fields obj._present_fields = present_fields

View File

@ -66,6 +66,9 @@ class StringField(BaseField):
regex = r'%s$' regex = r'%s$'
elif op == 'exact': elif op == 'exact':
regex = r'^%s$' regex = r'^%s$'
# escape unsafe characters which could lead to a re.error
value = re.escape(value)
value = re.compile(regex % value, flags) value = re.compile(regex % value, flags)
return value return value

View File

@ -135,7 +135,6 @@ class Q(object):
# Handle DBRef # Handle DBRef
if isinstance(value, pymongo.dbref.DBRef): if isinstance(value, pymongo.dbref.DBRef):
# this.created_user.$id == "4c4c56f8cc1831418c000000"
op_js = '(this.%(field)s.$id == "%(id)s" &&'\ op_js = '(this.%(field)s.$id == "%(id)s" &&'\
' this.%(field)s.$ref == "%(ref)s")' % { ' this.%(field)s.$ref == "%(ref)s")' % {
'field': key, 'field': key,
@ -239,6 +238,10 @@ class QuerySet(object):
"""An alias of :meth:`~mongoengine.queryset.QuerySet.__call__` """An alias of :meth:`~mongoengine.queryset.QuerySet.__call__`
""" """
return self.__call__(*q_objs, **query) return self.__call__(*q_objs, **query)
def all(self):
"""Returns all documents."""
return self.__call__()
@property @property
def _collection(self): def _collection(self):
@ -667,11 +670,13 @@ class QuerySet(object):
""" """
key_list = [] key_list = []
for key in keys: for key in keys:
if not key: continue
direction = pymongo.ASCENDING direction = pymongo.ASCENDING
if key[0] == '-': if key[0] == '-':
direction = pymongo.DESCENDING direction = pymongo.DESCENDING
if key[0] in ('-', '+'): if key[0] in ('-', '+'):
key = key[1:] key = key[1:]
key = key.replace('__', '.')
key_list.append((key, direction)) key_list.append((key, direction))
self._ordering = key_list self._ordering = key_list

View File

@ -674,7 +674,12 @@ class FieldTest(unittest.TestCase):
PutFile.drop_collection() PutFile.drop_collection()
StreamFile.drop_collection() StreamFile.drop_collection()
SetFile.drop_collection() SetFile.drop_collection()
# Make sure FileField is optional and not required
class DemoFile(Document):
file = FileField()
d = DemoFile.objects.create()
def test_geo_indexes(self): def test_geo_indexes(self):
"""Ensure that indexes are created automatically for GeoPointFields. """Ensure that indexes are created automatically for GeoPointFields.
""" """
@ -694,7 +699,7 @@ class FieldTest(unittest.TestCase):
Event.drop_collection() Event.drop_collection()
def test_ensure_unique_default_instances(self): def test_ensure_unique_default_instances(self):
"""Ensure that every document has it's own unique default instance.""" """Ensure that every field has it's own unique default instance."""
class D(Document): class D(Document):
data = DictField() data = DictField()
data2 = DictField(default=lambda: {}) data2 = DictField(default=lambda: {})

View File

@ -288,6 +288,13 @@ class QuerySetTest(unittest.TestCase):
self.assertEqual(obj, person) self.assertEqual(obj, person)
obj = self.Person.objects(Q(name__iexact='gUIDO VAN rOSSU')).first() obj = self.Person.objects(Q(name__iexact='gUIDO VAN rOSSU')).first()
self.assertEqual(obj, None) self.assertEqual(obj, None)
# Test unsafe expressions
person = self.Person(name='Guido van Rossum [.\'Geek\']')
person.save()
obj = self.Person.objects(Q(name__icontains='[.\'Geek')).first()
self.assertEqual(obj, person)
def test_filter_chaining(self): def test_filter_chaining(self):
"""Ensure filters can be chained together. """Ensure filters can be chained together.