Skip to content
Browse files

merged latest changes from upstream

  • Loading branch information...
2 parents 9b489fd + 6f3b9d2 commit d212724f4108d3d72d9b2724b82807d83b155da5 @davidovich davidovich committed Mar 9, 2012
Showing with 37 additions and 2 deletions.
  1. +37 −2 flask_mongokit.py
View
39 flask_mongokit.py
@@ -128,12 +128,40 @@ def register(self, documents):
"""Register one or more :class:`mongokit.Document` instances to the
connection.
+ Can be also used as a decorator on documents:
+
+ .. code-block:: python
+
+ db = MongoKit(app)
+
+ @db.register
+ class Task(Document):
+ structure = {
+ 'title': unicode,
+ 'text': unicode,
+ 'creation': datetime,
+ }
+
:param documents: A :class:`list` of :class:`mongokit.Document`.
"""
+
+ #enable decorator usage as in mongokit.Connection
+ decorator = None
+ if not isinstance(documents, (list, tuple, set, frozenset)):
+ # we assume that the user used this as a decorator
+ # using @register syntax or using db.register(SomeDoc)
+ # we stock the class object in order to return it later
+ decorator = documents
+ documents = [documents]
+
for document in documents:
if document not in self.registered_documents:
self.registered_documents.append(document)
- return self.registered_documents
+
+ if decorator is None:
+ return self.registered_documents
+ else:
+ return decorator
def connect(self):
"""Connect to the MongoDB server and register the documents from
@@ -161,7 +189,14 @@ def connected(self):
"""Connection status to your MongoDB."""
ctx = _request_ctx_stack.top
return hasattr(ctx, 'mongokit_db')
-
+
+ @property
+ def mongokit_db(self):
+ if self.connected:
+ return _request_ctx_stack.top.mongokit_db
+
+ return None
+
def disconnect(self):
"""Close the connection to your MongoDB."""
ctx = _request_ctx_stack.top

0 comments on commit d212724

Please sign in to comment.
Something went wrong with that request. Please try again.