From d99c7c20cc049f230fa619e42119b7eada5ba4fd Mon Sep 17 00:00:00 2001 From: Pau Aliagas Date: Mon, 3 Oct 2011 16:42:10 +0200 Subject: [PATCH 1/8] Don't allow empty lists when they are required When using ListField, an empty list is added as the default value. But when you mark this field as required, you expect it not to be empty, so this patch makes sure that this is duly checked. --- mongoengine/fields.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mongoengine/fields.py b/mongoengine/fields.py index 705bf3ae..933ab4d3 100644 --- a/mongoengine/fields.py +++ b/mongoengine/fields.py @@ -476,6 +476,10 @@ class ListField(ComplexBaseField): isinstance(value, basestring)): raise ValidationError('Only lists and tuples may be used in a ' 'list field') + # don't allow empty lists when they are required + if self.required and not value: + raise ValidationError('Field "%s" is required and cannot be empty' % + self.name) super(ListField, self).validate(value) def prepare_query_value(self, op, value): From 09f9c59b3d1a036eda0bf704de20c460c56b9ad9 Mon Sep 17 00:00:00 2001 From: Pau Aliagas Date: Tue, 4 Oct 2011 10:24:44 +0200 Subject: [PATCH 2/8] Add some more files to ignore in .gitignore --- .gitignore | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 315674fe..0300bc77 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,8 @@ .* !.gitignore -*.pyc -.*.swp +*~ +*.py[co] +.*.sw[po] *.egg docs/.build docs/_build From 2f26f7a827ad2be25e872a46cba8110607cbfefb Mon Sep 17 00:00:00 2001 From: Pau Aliagas Date: Tue, 4 Oct 2011 10:33:26 +0200 Subject: [PATCH 3/8] Add dependencies to spec file Add spec file for rpm-based systems --- python-mongoengine.spec | 53 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 python-mongoengine.spec diff --git a/python-mongoengine.spec b/python-mongoengine.spec new file mode 100644 index 00000000..054993d4 --- /dev/null +++ b/python-mongoengine.spec @@ -0,0 +1,53 @@ +# sitelib for noarch packages, sitearch for others (remove the unneeded one) +%{!?python_sitelib: %global python_sitelib %(%{__python} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")} +%{!?python_sitearch: %global python_sitearch %(%{__python} -c "from distutils.sysconfig import get_python_lib; print(get_python_lib(1))")} + +%define srcname mongoengine + +Name: python-%{srcname} +Version: 0.5.0 +Release: 2%{?dist} +Summary: A Python Document-Object Mapper for working with MongoDB + +Group: Development/Libraries +License: MIT +URL: https://github.com/namlook/mongoengine +Source0: %{srcname}-%{version}.tar.bz2 + +BuildRequires: python-devel +BuildRequires: python-setuptools + +Requires: mongodb +Requires: pymongo +Requires: python-blinker + +%description +MongoEngine is an ORM-like layer on top of PyMongo. + +%prep +%setup -q -n %{srcname}-%{version} + + +%build +# Remove CFLAGS=... for noarch packages (unneeded) +CFLAGS="$RPM_OPT_FLAGS" %{__python} setup.py build + + +%install +rm -rf $RPM_BUILD_ROOT +%{__python} setup.py install -O1 --skip-build --root $RPM_BUILD_ROOT + +%clean +rm -rf $RPM_BUILD_ROOT + +%files +%defattr(-,root,root,-) +%doc docs AUTHORS LICENSE README.rst +# For noarch packages: sitelib + %{python_sitelib}/* +# For arch-specific packages: sitearch +# %{python_sitearch}/* + +%changelog +* Fri Sep 23 2011 Pau Aliagas 0.5.0-1 +- Initial version From 17728d4e7423780f02a959716fe3d50db9cb6b75 Mon Sep 17 00:00:00 2001 From: Ross Lawley Date: Tue, 4 Oct 2011 02:57:50 -0700 Subject: [PATCH 4/8] Added tests for empty lists --- tests/fields.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/fields.py b/tests/fields.py index fd993167..802b02d0 100644 --- a/tests/fields.py +++ b/tests/fields.py @@ -485,7 +485,6 @@ class FieldTest(unittest.TestCase): post.info = [{'test': 3}] post.save() - self.assertEquals(BlogPost.objects.count(), 3) self.assertEquals(BlogPost.objects.filter(info__exact='test').count(), 1) self.assertEquals(BlogPost.objects.filter(info__0__test='test').count(), 1) @@ -515,7 +514,6 @@ class FieldTest(unittest.TestCase): Simple.drop_collection() - def test_list_field_rejects_strings(self): """Strings aren't valid list field data types""" @@ -528,6 +526,17 @@ class FieldTest(unittest.TestCase): self.assertRaises(ValidationError, e.save) + def test_list_field_required(self): + """Ensure required cant be None / Empty""" + + class Simple(Document): + mapping = ListField(required=True) + + Simple.drop_collection() + e = Simple() + e.mapping = [] + + self.assertRaises(ValidationError, e.save) def test_list_field_complex(self): """Ensure that the list fields can handle the complex types.""" From 94ae1388b15d2b3d253b804b30e6057145062c79 Mon Sep 17 00:00:00 2001 From: Ross Lawley Date: Tue, 4 Oct 2011 02:59:00 -0700 Subject: [PATCH 5/8] Updated .gitignore --- .gitignore | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 315674fe..0300bc77 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,8 @@ .* !.gitignore -*.pyc -.*.swp +*~ +*.py[co] +.*.sw[po] *.egg docs/.build docs/_build From 6d70ef1a08755e576cf605df6600071d1bff9c68 Mon Sep 17 00:00:00 2001 From: Ross Lawley Date: Tue, 4 Oct 2011 03:18:39 -0700 Subject: [PATCH 6/8] Updated changelog [#304] --- docs/changelog.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index b2ef802e..830c0192 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,6 +5,7 @@ Changelog Changes in dev ============== +- Added spec file for rpm-based distributions - Fixed ListField so it doesnt accept strings - Added DynamicDocument and EmbeddedDynamicDocument classes for expando schemas From 6961a9494f156769c8e3a5d20977eff98f15b0c2 Mon Sep 17 00:00:00 2001 From: Ross Lawley Date: Tue, 4 Oct 2011 04:26:56 -0700 Subject: [PATCH 7/8] Updates to ComplexFields Required now means they cannot be empty [#302] --- docs/changelog.rst | 1 + mongoengine/base.py | 5 +++++ mongoengine/fields.py | 10 ++++++---- tests/fields.py | 11 ++++++++++- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index b2ef802e..e76b4416 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,6 +5,7 @@ Changelog Changes in dev ============== +- Updated ComplexFields so if required they won't accept empty lists / dicts - Fixed ListField so it doesnt accept strings - Added DynamicDocument and EmbeddedDynamicDocument classes for expando schemas diff --git a/mongoengine/base.py b/mongoengine/base.py index 40953f17..adf5eee9 100644 --- a/mongoengine/base.py +++ b/mongoengine/base.py @@ -282,6 +282,11 @@ class ComplexBaseField(BaseField): raise ValidationError('Invalid %s item (%s)' % ( self.field.__class__.__name__, str(v))) + # Don't allow empty values if required + if self.required and not value: + raise ValidationError('Field "%s" is required and cannot be empty' % + self.name) + def prepare_query_value(self, op, value): return self.to_mongo(value) diff --git a/mongoengine/fields.py b/mongoengine/fields.py index 933ab4d3..e75890f7 100644 --- a/mongoengine/fields.py +++ b/mongoengine/fields.py @@ -459,6 +459,9 @@ class GenericEmbeddedDocumentField(BaseField): class ListField(ComplexBaseField): """A list field that wraps a standard field, allowing multiple instances of the field to be used as a list in the database. + + .. note:: + Required means it cannot be empty - as the default for ListFields is [] """ # ListFields cannot be indexed with _types - MongoDB doesn't support this @@ -476,10 +479,6 @@ class ListField(ComplexBaseField): isinstance(value, basestring)): raise ValidationError('Only lists and tuples may be used in a ' 'list field') - # don't allow empty lists when they are required - if self.required and not value: - raise ValidationError('Field "%s" is required and cannot be empty' % - self.name) super(ListField, self).validate(value) def prepare_query_value(self, op, value): @@ -517,6 +516,9 @@ class DictField(ComplexBaseField): """A dictionary field that wraps a standard Python dictionary. This is similar to an embedded document, but the structure is not defined. + .. note:: + Required means it cannot be empty - as the default for ListFields is [] + .. versionadded:: 0.3 .. versionchanged:: 0.5 - Can now handle complex / varying types of data """ diff --git a/tests/fields.py b/tests/fields.py index 802b02d0..b1825026 100644 --- a/tests/fields.py +++ b/tests/fields.py @@ -526,7 +526,7 @@ class FieldTest(unittest.TestCase): self.assertRaises(ValidationError, e.save) - def test_list_field_required(self): + def test_complex_field_required(self): """Ensure required cant be None / Empty""" class Simple(Document): @@ -538,6 +538,15 @@ class FieldTest(unittest.TestCase): self.assertRaises(ValidationError, e.save) + class Simple(Document): + mapping = DictField(required=True) + + Simple.drop_collection() + e = Simple() + e.mapping = {} + + self.assertRaises(ValidationError, e.save) + def test_list_field_complex(self): """Ensure that the list fields can handle the complex types.""" From dca135190a7a04b7d1254b66b779c7d45a1911cf Mon Sep 17 00:00:00 2001 From: Ross Lawley Date: Tue, 4 Oct 2011 04:28:30 -0700 Subject: [PATCH 8/8] Fixed changelog --- docs/changelog.rst | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index a4a7fa9e..71f8e514 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,11 +5,8 @@ Changelog Changes in dev ============== -<<<<<<< HEAD -- Added spec file for rpm-based distributions -======= - Updated ComplexFields so if required they won't accept empty lists / dicts ->>>>>>> listfield +- Added spec file for rpm-based distributions - Fixed ListField so it doesnt accept strings - Added DynamicDocument and EmbeddedDynamicDocument classes for expando schemas