From 3070e0bf5d6eef8548a980b7223f9d6153ed07d0 Mon Sep 17 00:00:00 2001 From: Ross Lawley Date: Fri, 20 Jul 2012 10:34:08 +0100 Subject: [PATCH] Fix for inheritance bug and db_alias --- docs/changelog.rst | 7 ++++++- mongoengine/__init__.py | 2 +- mongoengine/base.py | 15 ++++++++------- python-mongoengine.spec | 2 +- tests/test_document.py | 14 +++++++++++++- 5 files changed, 29 insertions(+), 11 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index b4d747d07..19d21b879 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -2,9 +2,14 @@ Changelog ========= + +Changes in 0.6.16 +================= +- Fixed issue where db_alias wasn't inherited + Changes in 0.6.15 ================= -- Updated validation error message +- Updated validation error messages - Added support for null / zero / false values in item_frequencies - Fixed cascade save edge case - Fixed geo index creation through reference fields diff --git a/mongoengine/__init__.py b/mongoengine/__init__.py index c26caf452..ad12479d4 100644 --- a/mongoengine/__init__.py +++ b/mongoengine/__init__.py @@ -12,7 +12,7 @@ __all__ = (document.__all__ + fields.__all__ + connection.__all__ + queryset.__all__ + signals.__all__) -VERSION = (0, 6, 15) +VERSION = (0, 6, 16) def get_version(): diff --git a/mongoengine/base.py b/mongoengine/base.py index 3ab6850f5..09768c4a2 100644 --- a/mongoengine/base.py +++ b/mongoengine/base.py @@ -649,8 +649,13 @@ def __new__(cls, name, bases, attrs): del(attrs['meta']['collection']) if base._get_collection_name(): collection = base._get_collection_name() - # Propagate index options. - for key in ('index_background', 'index_drop_dups', 'index_opts'): + + # Propagate inherited values + keys_to_propogate = ( + 'index_background', 'index_drop_dups', 'index_opts', + 'allow_inheritance', 'queryset_class', 'db_alias', + ) + for key in keys_to_propogate: if key in base._meta: base_meta[key] = base._meta[key] @@ -659,11 +664,6 @@ def __new__(cls, name, bases, attrs): abstract_base_indexes += base._meta.get('indexes', []) else: base_indexes += base._meta.get('indexes', []) - # Propagate 'allow_inheritance' - if 'allow_inheritance' in base._meta: - base_meta['allow_inheritance'] = base._meta['allow_inheritance'] - if 'queryset_class' in base._meta: - base_meta['queryset_class'] = base._meta['queryset_class'] try: base_meta['objects'] = base.__getattribute__(base, 'objects') except TypeError: @@ -671,6 +671,7 @@ def __new__(cls, name, bases, attrs): except AttributeError: pass + # defaults meta = { 'abstract': False, 'collection': collection, diff --git a/python-mongoengine.spec b/python-mongoengine.spec index fd7b197bc..03093b366 100644 --- a/python-mongoengine.spec +++ b/python-mongoengine.spec @@ -5,7 +5,7 @@ %define srcname mongoengine Name: python-%{srcname} -Version: 0.6.15 +Version: 0.6.16 Release: 1%{?dist} Summary: A Python Document-Object Mapper for working with MongoDB diff --git a/tests/test_document.py b/tests/test_document.py index 825086182..6915caff3 100644 --- a/tests/test_document.py +++ b/tests/test_document.py @@ -2999,7 +2999,7 @@ class Book(Document): self.assertEqual(User.objects.first(), bob) self.assertEqual(Book.objects.first(), hp) - # DeRefecence + # DeReference class AuthorBooks(Document): author = ReferenceField(User) book = ReferenceField(Book) @@ -3027,6 +3027,18 @@ class AuthorBooks(Document): self.assertEqual(Book._get_collection(), get_db("testdb-2")[Book._get_collection_name()]) self.assertEqual(AuthorBooks._get_collection(), get_db("testdb-3")[AuthorBooks._get_collection_name()]) + def test_db_alias_propagates(self): + """db_alias propagates? + """ + class A(Document): + name = StringField() + meta = {"db_alias": "testdb-1", "allow_inheritance": True} + + class B(A): + pass + + self.assertEquals('testdb-1', B._meta.get('db_alias')) + def test_db_ref_usage(self): """ DB Ref usage in __raw__ queries """