From beacfae400296ba364c3b6f0839005a6e601650b Mon Sep 17 00:00:00 2001 From: Ross Lawley Date: Wed, 30 Nov 2011 07:55:33 -0800 Subject: [PATCH] Removed use of _get_subclasses favouring get_document _get_subclasses not actually required and causes issues where Base Classes aren't imported but dont actually need to be. Fixes #271 --- AUTHORS | 1 + docs/changelog.rst | 2 ++ docs/upgrade.rst | 2 ++ mongoengine/base.py | 26 +------------------------ mongoengine/document.py | 7 +++---- tests/document.py | 43 ++--------------------------------------- 6 files changed, 11 insertions(+), 70 deletions(-) diff --git a/AUTHORS b/AUTHORS index 7e7c3321..28b57bb4 100644 --- a/AUTHORS +++ b/AUTHORS @@ -79,3 +79,4 @@ that much better: * Alice Zoƫ Bevan-McGregor * Stephen Young * tkloc + * aid diff --git a/docs/changelog.rst b/docs/changelog.rst index 055cf92c..d510531f 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,6 +5,8 @@ Changelog Changes in dev ============== +- Removed Document._get_subclasses() - no longer required +- Fixed bug requiring subclasses when not actually needed - Fixed deletion of dynamic data - Added support for the $elementMatch operator - Added reverse option to SortedListFields diff --git a/docs/upgrade.rst b/docs/upgrade.rst index 9151cf3c..b44ecad5 100644 --- a/docs/upgrade.rst +++ b/docs/upgrade.rst @@ -8,6 +8,8 @@ Upgrading Embedded Documents - if you had a `pk` field you will have to rename it from `_id` to `pk` as pk is no longer a property of Embedded Documents. +Document._get_subclasses - Is no longer used and the class method has been removed. + 0.4 to 0.5 =========== diff --git a/mongoengine/base.py b/mongoengine/base.py index 58fcbd6e..402f191e 100644 --- a/mongoengine/base.py +++ b/mongoengine/base.py @@ -845,21 +845,6 @@ class BaseDocument(object): """ return cls._meta.get('collection', None) - @classmethod - def _get_subclasses(cls): - """Return a dictionary of all subclasses (found recursively). - """ - try: - subclasses = cls.__subclasses__() - except: - subclasses = cls.__subclasses__(cls) - - all_subclasses = {} - for subclass in subclasses: - all_subclasses[subclass._class_name] = subclass - all_subclasses.update(subclass._get_subclasses()) - return all_subclasses - @classmethod def _from_son(cls, son): """Create an instance of a Document (subclass) from a PyMongo SON. @@ -877,16 +862,7 @@ class BaseDocument(object): # Return correct subclass for document type if class_name != cls._class_name: - subclasses = cls._get_subclasses() - if class_name not in subclasses: - # Type of document is probably more generic than the class - # that has been queried to return this SON - raise NotRegistered(""" - `%s` has not been registered in the document registry. - Importing the document class automatically registers it, - has it been imported? - """.strip() % class_name) - cls = subclasses[class_name] + cls = get_document(class_name) changed_fields = [] for field_name, field in cls._fields.items(): diff --git a/mongoengine/document.py b/mongoengine/document.py index e22e36ff..0eee4af6 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -162,11 +162,10 @@ class Document(BaseDocument): doc = self.to_mongo() - created = '_id' in doc - creation_mode = force_insert or not created + created = force_insert or '_id' not in doc try: collection = self.__class__.objects._collection - if creation_mode: + if created: if force_insert: object_id = collection.insert(doc, safe=safe, **write_options) else: @@ -194,7 +193,7 @@ class Document(BaseDocument): self[id_field] = self._fields[id_field].to_python(object_id) self._changed_fields = [] - signals.post_save.send(self.__class__, document=self, created=creation_mode) + signals.post_save.send(self.__class__, document=self, created=created) def cascade_save(self, *args, **kwargs): """Recursively saves any references / generic references on an object""" diff --git a/tests/document.py b/tests/document.py index a4dffe70..ca42c091 100644 --- a/tests/document.py +++ b/tests/document.py @@ -158,29 +158,6 @@ class DocumentTest(unittest.TestCase): } self.assertEqual(Dog._superclasses, dog_superclasses) - def test_get_subclasses(self): - """Ensure that the correct list of subclasses is retrieved by the - _get_subclasses method. - """ - class Animal(Document): pass - class Fish(Animal): pass - class Mammal(Animal): pass - class Human(Mammal): pass - class Dog(Mammal): pass - - mammal_subclasses = { - 'Animal.Mammal.Dog': Dog, - 'Animal.Mammal.Human': Human - } - self.assertEqual(Mammal._get_subclasses(), mammal_subclasses) - - animal_subclasses = { - 'Animal.Fish': Fish, - 'Animal.Mammal': Mammal, - 'Animal.Mammal.Dog': Dog, - 'Animal.Mammal.Human': Human - } - self.assertEqual(Animal._get_subclasses(), animal_subclasses) def test_external_super_and_sub_classes(self): """Ensure that the correct list of sub and super classes is assembled. @@ -202,20 +179,6 @@ class DocumentTest(unittest.TestCase): } self.assertEqual(Dog._superclasses, dog_superclasses) - animal_subclasses = { - 'Base.Animal.Fish': Fish, - 'Base.Animal.Mammal': Mammal, - 'Base.Animal.Mammal.Dog': Dog, - 'Base.Animal.Mammal.Human': Human - } - self.assertEqual(Animal._get_subclasses(), animal_subclasses) - - mammal_subclasses = { - 'Base.Animal.Mammal.Dog': Dog, - 'Base.Animal.Mammal.Human': Human - } - self.assertEqual(Mammal._get_subclasses(), mammal_subclasses) - Base.drop_collection() h = Human() @@ -1014,10 +977,8 @@ class DocumentTest(unittest.TestCase): # Mimic Place and NicePlace definitions being in a different file # and the NicePlace model not being imported in at query time. - @classmethod - def _get_subclasses(cls): - return {} - Place._get_subclasses = _get_subclasses + from mongoengine.base import _document_registry + del(_document_registry['Place.NicePlace']) def query_without_importing_nice_place(): print Place.objects.all()