From 7115aa0f6c9b2a54b28bf8d8fa7b86ea0bbc74f4 Mon Sep 17 00:00:00 2001 From: Oleg Churkin Date: Tue, 23 Dec 2014 14:55:42 +0300 Subject: [PATCH 1/2] LazyDocumentMetaWrapper getitem method fixed --- mongodbforms/documentoptions.py | 32 +++++++-------------- mongodbforms/tests.py | 50 +++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 22 deletions(-) create mode 100644 mongodbforms/tests.py diff --git a/mongodbforms/documentoptions.py b/mongodbforms/documentoptions.py index 082e3440..4b515fae 100644 --- a/mongodbforms/documentoptions.py +++ b/mongodbforms/documentoptions.py @@ -5,7 +5,7 @@ from django.db.models.fields import FieldDoesNotExist from django.utils.text import capfirst from django.db.models.options import get_verbose_name -from django.utils.functional import LazyObject +from django.utils.functional import LazyObject, new_method_proxy from django.conf import settings from mongoengine.fields import ReferenceField, ListField @@ -65,40 +65,28 @@ def __setattr__(self, attr, value): class LazyDocumentMetaWrapper(LazyObject): _document = None _meta = None - + def __init__(self, document): self._document = document self._meta = document._meta super(LazyDocumentMetaWrapper, self).__init__() - + def _setup(self): self._wrapped = DocumentMetaWrapper(self._document, self._meta) - + def __setattr__(self, name, value): if name in ["_document", "_meta",]: object.__setattr__(self, name, value) else: super(LazyDocumentMetaWrapper, self).__setattr__(name, value) - - def __dir__(self): - return self._wrapped.__dir__() - - def __getitem__(self, key): - return self._wrapped.__getitem__(key) - - def __setitem__(self, key, value): - return self._wrapped.__getitem__(key, value) - - def __delitem__(self, key): - return self._wrapped.__delitem__(key) - - def __len__(self): - return self._wrapped.__len__() - + + __len__ = new_method_proxy(len) + + @new_method_proxy def __contains__(self, key): - return self._wrapped.__contains__(key) - + return key in self + class DocumentMetaWrapper(MutableMapping): """ Used to store mongoengine's _meta dict to make the document admin diff --git a/mongodbforms/tests.py b/mongodbforms/tests.py new file mode 100644 index 00000000..a6c26258 --- /dev/null +++ b/mongodbforms/tests.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +from django.conf import settings + +settings.configure( + DEBUG=True, + DATABASES={ + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + } + }, + ROOT_URLCONF='', + INSTALLED_APPS=( + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.admin', + 'mongodbforms', + ) +) + + +import mongoengine +from django.test import SimpleTestCase +from mongodbforms.documentoptions import LazyDocumentMetaWrapper + + +class TestDocument(mongoengine.Document): + meta = {'abstract': True} + + name = mongoengine.StringField() + + +class LazyWrapperTest(SimpleTestCase): + + def test_lazy_getitem(self): + meta = LazyDocumentMetaWrapper(TestDocument) + self.assertTrue(meta['abstract']) + + meta = LazyDocumentMetaWrapper(TestDocument) + self.assertTrue(meta.get('abstract')) + + meta = LazyDocumentMetaWrapper(TestDocument) + self.assertTrue('abstract' in meta) + + meta = LazyDocumentMetaWrapper(TestDocument) + self.assertEqual(len(meta), 1) + + meta = LazyDocumentMetaWrapper(TestDocument) + meta.custom = 'yes' + self.assertEqual(meta.custom, 'yes') From cef1d4918a99d589e3ab61b426de98a5db02a2ef Mon Sep 17 00:00:00 2001 From: Oleg Churkin Date: Tue, 23 Dec 2014 15:03:28 +0300 Subject: [PATCH 2/2] Version bump to 0.3.1 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 60d87bd8..91eb0b14 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ def convert_readme(): return open('README.txt').read() setup(name='mongodbforms', - version='0.3', + version='0.3.1', description="An implementation of django forms using mongoengine.", author='Jan Schrewe', author_email='jan@schafproductions.com',