Updated docs and added a NotRegistered exception
For handling GenericReferences that reference documents that haven't been imported. Closes #170
This commit is contained in:
		| @@ -7,16 +7,26 @@ import pymongo | |||||||
| import pymongo.objectid | import pymongo.objectid | ||||||
|  |  | ||||||
|  |  | ||||||
| _document_registry = {} | class NotRegistered(Exception): | ||||||
|  |     pass | ||||||
| def get_document(name): |  | ||||||
|     return _document_registry[name] |  | ||||||
|  |  | ||||||
|  |  | ||||||
| class ValidationError(Exception): | class ValidationError(Exception): | ||||||
|     pass |     pass | ||||||
|  |  | ||||||
|  |  | ||||||
|  | _document_registry = {} | ||||||
|  |  | ||||||
|  | def get_document(name): | ||||||
|  |     if name not in _document_registry: | ||||||
|  |         raise NotRegistered(""" | ||||||
|  |             `%s` has not been registered in the document registry. | ||||||
|  |             Importing the document class automatically registers it, has it | ||||||
|  |             been imported? | ||||||
|  |         """.strip() % name) | ||||||
|  |     return _document_registry[name] | ||||||
|  |  | ||||||
|  |  | ||||||
| class BaseField(object): | class BaseField(object): | ||||||
|     """A base class for fields in a MongoDB document. Instances of this class |     """A base class for fields in a MongoDB document. Instances of this class | ||||||
|     may be added to subclasses of `Document` to define a document's schema. |     may be added to subclasses of `Document` to define a document's schema. | ||||||
|   | |||||||
| @@ -522,6 +522,9 @@ class GenericReferenceField(BaseField): | |||||||
|     """A reference to *any* :class:`~mongoengine.document.Document` subclass |     """A reference to *any* :class:`~mongoengine.document.Document` subclass | ||||||
|     that will be automatically dereferenced on access (lazily). |     that will be automatically dereferenced on access (lazily). | ||||||
|  |  | ||||||
|  |     note:  Any documents used as a generic reference must be registered in the | ||||||
|  |     document registry.  Importing the model will automatically register it. | ||||||
|  |  | ||||||
|     .. versionadded:: 0.3 |     .. versionadded:: 0.3 | ||||||
|     """ |     """ | ||||||
|  |  | ||||||
|   | |||||||
| @@ -7,6 +7,7 @@ import gridfs | |||||||
|  |  | ||||||
| from mongoengine import * | from mongoengine import * | ||||||
| from mongoengine.connection import _get_db | from mongoengine.connection import _get_db | ||||||
|  | from mongoengine.base import _document_registry, NotRegistered | ||||||
|  |  | ||||||
|  |  | ||||||
| class FieldTest(unittest.TestCase): | class FieldTest(unittest.TestCase): | ||||||
| @@ -584,6 +585,39 @@ class FieldTest(unittest.TestCase): | |||||||
|         Post.drop_collection() |         Post.drop_collection() | ||||||
|         User.drop_collection() |         User.drop_collection() | ||||||
|  |  | ||||||
|  |     def test_generic_reference_document_not_registered(self): | ||||||
|  |         """Ensure dereferencing out of the document registry throws a | ||||||
|  |         `NotRegistered` error. | ||||||
|  |         """ | ||||||
|  |         class Link(Document): | ||||||
|  |             title = StringField() | ||||||
|  |  | ||||||
|  |         class User(Document): | ||||||
|  |             bookmarks = ListField(GenericReferenceField()) | ||||||
|  |  | ||||||
|  |         Link.drop_collection() | ||||||
|  |         User.drop_collection() | ||||||
|  |  | ||||||
|  |         link_1 = Link(title="Pitchfork") | ||||||
|  |         link_1.save() | ||||||
|  |  | ||||||
|  |         user = User(bookmarks=[link_1]) | ||||||
|  |         user.save() | ||||||
|  |  | ||||||
|  |         # Mimic User and Link definitions being in a different file | ||||||
|  |         # and the Link model not being imported in the User file. | ||||||
|  |         del(_document_registry["Link"]) | ||||||
|  |  | ||||||
|  |         user = User.objects.first() | ||||||
|  |         try: | ||||||
|  |             user.bookmarks | ||||||
|  |             raise AssertionError, "Link was removed from the registry" | ||||||
|  |         except NotRegistered: | ||||||
|  |             pass | ||||||
|  |  | ||||||
|  |         Link.drop_collection() | ||||||
|  |         User.drop_collection() | ||||||
|  |  | ||||||
|     def test_binary_fields(self): |     def test_binary_fields(self): | ||||||
|         """Ensure that binary fields can be stored and retrieved. |         """Ensure that binary fields can be stored and retrieved. | ||||||
|         """ |         """ | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user