diff --git a/docs/django.rst b/docs/django.rst index d2c468a2..92a8a52b 100644 --- a/docs/django.rst +++ b/docs/django.rst @@ -28,6 +28,8 @@ The :mod:`~mongoengine.django.auth` module also contains a :func:`~mongoengine.django.auth.get_user` helper function, that takes a user's :attr:`id` and returns a :class:`~mongoengine.django.auth.User` object. +.. versionadded:: 0.1.3 + Sessions ======== Django allows the use of different backend stores for its sessions. MongoEngine @@ -40,3 +42,5 @@ session backend, ensure that your settings module has into you settings module:: SESSION_ENGINE = 'mongoengine.django.sessions' + +.. versionadded:: 0.2.1 diff --git a/mongoengine/base.py b/mongoengine/base.py index 9ac35607..cf97f875 100644 --- a/mongoengine/base.py +++ b/mongoengine/base.py @@ -246,6 +246,25 @@ class BaseDocument(object): value = getattr(self, attr_name, None) setattr(self, attr_name, value) + def validate(self): + """Ensure that all fields' values are valid and that required fields + are present. + """ + # Get a list of tuples of field names and their current values + fields = [(field, getattr(self, name)) + for name, field in self._fields.items()] + + # Ensure that each field is matched to a valid value + for field, value in fields: + if value is not None: + try: + field.validate(value) + except (ValueError, AttributeError, AssertionError), e: + raise ValidationError('Invalid value for field of type "' + + field.__class__.__name__ + '"') + elif field.required: + raise ValidationError('Field "%s" is required' % field.name) + @classmethod def _get_subclasses(cls): """Return a dictionary of all subclasses (found recursively). diff --git a/mongoengine/document.py b/mongoengine/document.py index 3492fd76..62f9ecce 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -107,25 +107,6 @@ class Document(BaseDocument): for field in self._fields: setattr(self, field, obj[field]) - def validate(self): - """Ensure that all fields' values are valid and that required fields - are present. - """ - # Get a list of tuples of field names and their current values - fields = [(field, getattr(self, name)) - for name, field in self._fields.items()] - - # Ensure that each field is matched to a valid value - for field, value in fields: - if value is not None: - try: - field.validate(value) - except (ValueError, AttributeError, AssertionError), e: - raise ValidationError('Invalid value for field of type "' + - field.__class__.__name__ + '"') - elif field.required: - raise ValidationError('Field "%s" is required' % field.name) - @classmethod def drop_collection(cls): """Drops the entire collection associated with this diff --git a/tests/document.py b/tests/document.py index 91b09e75..2aa98811 100644 --- a/tests/document.py +++ b/tests/document.py @@ -387,6 +387,25 @@ class DocumentTest(unittest.TestCase): self.assertTrue('content' in Comment._fields) self.assertFalse('id' in Comment._fields) self.assertFalse(hasattr(Comment, '_meta')) + + def test_embedded_document_validation(self): + """Ensure that embedded documents may be validated. + """ + class Comment(EmbeddedDocument): + date = DateTimeField() + content = StringField(required=True) + + comment = Comment() + self.assertRaises(ValidationError, comment.validate) + + comment.content = 'test' + comment.validate() + + comment.date = 4 + self.assertRaises(ValidationError, comment.validate) + + comment.date = datetime.datetime.now() + comment.validate() def test_save(self): """Ensure that a document may be saved in the database.