Skip to content

Commit

Permalink
We don't need no recursion for #60
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Schrewe committed Nov 9, 2013
1 parent d8c16b5 commit efa1bb1
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
67 changes: 62 additions & 5 deletions mongodbforms/documentoptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +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, empty

from mongoengine.fields import ReferenceField, ListField

Expand Down Expand Up @@ -57,6 +58,56 @@ def __setattr__(self, attr, value):
super(PkWrapper, self).__setattr__(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",]:
# Assign to __dict__ to avoid infinite __setattr__ loops.
object.__setattr__(self, name, value)
else:
super(LazyDocumentMetaWrapper, self).__setattr__(name, value)

def __dir__(self):
if self._wrapped is empty:
self._setup()
return self._wrapped.__dir__()

def __getitem__(self, key):
if self._wrapped is empty:
self._setup()
return self._wrapped.__getitem__(key)

def __setitem__(self, key, value):
if self._wrapped is empty:
self._setup()
return self._wrapped.__getitem__(key, value)

def __delitem__(self, key):
if self._wrapped is empty:
self._setup()
return self._wrapped.__delitem__(key)

def __len__(self):
if self._wrapped is empty:
self._setup()
return self._wrapped.__len__()

def __contains__(self, key):
if self._wrapped is empty:
self._setup()
return self._wrapped.__contains__(key)


class DocumentMetaWrapper(MutableMapping):
"""
Used to store mongoengine's _meta dict to make the document admin
Expand All @@ -81,14 +132,19 @@ class DocumentMetaWrapper(MutableMapping):
concrete_model = None
concrete_managers = []

def __init__(self, document):
def __init__(self, document, meta=None):
super(DocumentMetaWrapper, self).__init__()

print "__init__'s document is:"
print type(document)
self.document = document
# used by Django to distinguish between abstract and concrete models
# here for now always the document
self.concrete_model = document
self._meta = getattr(document, '_meta', {})
if meta is None:
self._meta = getattr(document, '_meta', {})
else:
self._meta = meta

try:
self.object_name = self.document.__name__
Expand Down Expand Up @@ -131,9 +187,10 @@ def _setup_document_fields(self):
flat.append((choice, value))
f.flatchoices = flat
if isinstance(f, ReferenceField) and not \
isinstance(f.document_type._meta, DocumentMetaWrapper) \
and self.document != f.document_type:
f.document_type._meta = DocumentMetaWrapper(f.document_type)
isinstance(f.document_type._meta, DocumentMetaWrapper) and not \
isinstance(f.document_type._meta, LazyDocumentMetaWrapper) and \
self.document != f.document_type:
f.document_type._meta = LazyDocumentMetaWrapper(f.document_type)

def _init_pk(self):
"""
Expand Down
6 changes: 4 additions & 2 deletions mongodbforms/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.conf import settings

from .documentoptions import DocumentMetaWrapper
from mongodbforms.documentoptions import DocumentMetaWrapper, LazyDocumentMetaWrapper
from .fieldgenerator import MongoDefaultFormFieldGenerator

try:
Expand Down Expand Up @@ -50,7 +50,9 @@ def load_field_generator():


def init_document_options(document):
if not isinstance(document._meta, DocumentMetaWrapper):
if not (isinstance(document._meta, DocumentMetaWrapper) or
isinstance(document._meta, LazyDocumentMetaWrapper)):
print "setting up wrapper for meta of type %s" % type(document._meta)
document._meta = DocumentMetaWrapper(document)
return document

Expand Down

0 comments on commit efa1bb1

Please sign in to comment.