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
This commit is contained in:
parent
fdc385ea33
commit
beacfae400
1
AUTHORS
1
AUTHORS
@ -79,3 +79,4 @@ that much better:
|
|||||||
* Alice Zoë Bevan-McGregor
|
* Alice Zoë Bevan-McGregor
|
||||||
* Stephen Young
|
* Stephen Young
|
||||||
* tkloc
|
* tkloc
|
||||||
|
* aid
|
||||||
|
@ -5,6 +5,8 @@ Changelog
|
|||||||
Changes in dev
|
Changes in dev
|
||||||
==============
|
==============
|
||||||
|
|
||||||
|
- Removed Document._get_subclasses() - no longer required
|
||||||
|
- Fixed bug requiring subclasses when not actually needed
|
||||||
- Fixed deletion of dynamic data
|
- Fixed deletion of dynamic data
|
||||||
- Added support for the $elementMatch operator
|
- Added support for the $elementMatch operator
|
||||||
- Added reverse option to SortedListFields
|
- Added reverse option to SortedListFields
|
||||||
|
@ -8,6 +8,8 @@ Upgrading
|
|||||||
Embedded Documents - if you had a `pk` field you will have to rename it from `_id`
|
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.
|
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
|
0.4 to 0.5
|
||||||
===========
|
===========
|
||||||
|
|
||||||
|
@ -845,21 +845,6 @@ class BaseDocument(object):
|
|||||||
"""
|
"""
|
||||||
return cls._meta.get('collection', None)
|
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
|
@classmethod
|
||||||
def _from_son(cls, son):
|
def _from_son(cls, son):
|
||||||
"""Create an instance of a Document (subclass) from a PyMongo 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
|
# Return correct subclass for document type
|
||||||
if class_name != cls._class_name:
|
if class_name != cls._class_name:
|
||||||
subclasses = cls._get_subclasses()
|
cls = get_document(class_name)
|
||||||
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]
|
|
||||||
|
|
||||||
changed_fields = []
|
changed_fields = []
|
||||||
for field_name, field in cls._fields.items():
|
for field_name, field in cls._fields.items():
|
||||||
|
@ -162,11 +162,10 @@ class Document(BaseDocument):
|
|||||||
|
|
||||||
doc = self.to_mongo()
|
doc = self.to_mongo()
|
||||||
|
|
||||||
created = '_id' in doc
|
created = force_insert or '_id' not in doc
|
||||||
creation_mode = force_insert or not created
|
|
||||||
try:
|
try:
|
||||||
collection = self.__class__.objects._collection
|
collection = self.__class__.objects._collection
|
||||||
if creation_mode:
|
if created:
|
||||||
if force_insert:
|
if force_insert:
|
||||||
object_id = collection.insert(doc, safe=safe, **write_options)
|
object_id = collection.insert(doc, safe=safe, **write_options)
|
||||||
else:
|
else:
|
||||||
@ -194,7 +193,7 @@ class Document(BaseDocument):
|
|||||||
self[id_field] = self._fields[id_field].to_python(object_id)
|
self[id_field] = self._fields[id_field].to_python(object_id)
|
||||||
|
|
||||||
self._changed_fields = []
|
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):
|
def cascade_save(self, *args, **kwargs):
|
||||||
"""Recursively saves any references / generic references on an object"""
|
"""Recursively saves any references / generic references on an object"""
|
||||||
|
@ -158,29 +158,6 @@ class DocumentTest(unittest.TestCase):
|
|||||||
}
|
}
|
||||||
self.assertEqual(Dog._superclasses, dog_superclasses)
|
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):
|
def test_external_super_and_sub_classes(self):
|
||||||
"""Ensure that the correct list of sub and super classes is assembled.
|
"""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)
|
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()
|
Base.drop_collection()
|
||||||
|
|
||||||
h = Human()
|
h = Human()
|
||||||
@ -1014,10 +977,8 @@ class DocumentTest(unittest.TestCase):
|
|||||||
|
|
||||||
# Mimic Place and NicePlace definitions being in a different file
|
# Mimic Place and NicePlace definitions being in a different file
|
||||||
# and the NicePlace model not being imported in at query time.
|
# and the NicePlace model not being imported in at query time.
|
||||||
@classmethod
|
from mongoengine.base import _document_registry
|
||||||
def _get_subclasses(cls):
|
del(_document_registry['Place.NicePlace'])
|
||||||
return {}
|
|
||||||
Place._get_subclasses = _get_subclasses
|
|
||||||
|
|
||||||
def query_without_importing_nice_place():
|
def query_without_importing_nice_place():
|
||||||
print Place.objects.all()
|
print Place.objects.all()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user