Merge branch 'master' of git://github.com/hmarr/mongoengine
This commit is contained in:
		| @@ -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 | :func:`~mongoengine.django.auth.get_user` helper function, that takes a user's | ||||||
| :attr:`id` and returns a :class:`~mongoengine.django.auth.User` object. | :attr:`id` and returns a :class:`~mongoengine.django.auth.User` object. | ||||||
|  |  | ||||||
|  | .. versionadded:: 0.1.3 | ||||||
|  |  | ||||||
| Sessions | Sessions | ||||||
| ======== | ======== | ||||||
| Django allows the use of different backend stores for its sessions. MongoEngine | 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:: | into you settings module:: | ||||||
|  |  | ||||||
|     SESSION_ENGINE = 'mongoengine.django.sessions' |     SESSION_ENGINE = 'mongoengine.django.sessions' | ||||||
|  |  | ||||||
|  | .. versionadded:: 0.2.1 | ||||||
|   | |||||||
| @@ -247,6 +247,25 @@ class BaseDocument(object): | |||||||
|                 value = getattr(self, attr_name, None) |                 value = getattr(self, attr_name, None) | ||||||
|                 setattr(self, attr_name, value) |                 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 |     @classmethod | ||||||
|     def _get_subclasses(cls): |     def _get_subclasses(cls): | ||||||
|         """Return a dictionary of all subclasses (found recursively). |         """Return a dictionary of all subclasses (found recursively). | ||||||
|   | |||||||
| @@ -107,25 +107,6 @@ class Document(BaseDocument): | |||||||
|         for field in self._fields: |         for field in self._fields: | ||||||
|             setattr(self, field, obj[field]) |             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 |     @classmethod | ||||||
|     def drop_collection(cls): |     def drop_collection(cls): | ||||||
|         """Drops the entire collection associated with this |         """Drops the entire collection associated with this | ||||||
|   | |||||||
| @@ -388,6 +388,25 @@ class DocumentTest(unittest.TestCase): | |||||||
|         self.assertFalse('id' in Comment._fields) |         self.assertFalse('id' in Comment._fields) | ||||||
|         self.assertFalse(hasattr(Comment, '_meta')) |         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): |     def test_save(self): | ||||||
|         """Ensure that a document may be saved in the database. |         """Ensure that a document may be saved in the database. | ||||||
|         """ |         """ | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user