1. Simone Marzola
  2. gartichoke

Commits

Simone Marzola  committed 430501d

Datastore transactions middleware

  • Participants
  • Parent commits 8cf5425
  • Branches default

Comments (0)

Files changed (6)

File .hgignore

View file
 venv
 dist
 .DS_Store
+env

File controllers/__init__.py

View file
+__author__ = 'simock85'

File controllers/root.py

View file
+import artichoke, os
+from artichoke import expose
+from google.appengine.api.app_identity import get_application_id
+
+class SubController(artichoke.Controller):
+    @expose()
+    def about(self, args, params):
+        return 'HELLO %s' % str(args)
+
+    @expose()
+    def index(self, args, params):
+        return 'INDEX'
+
+    @expose()
+    def not_found(self, args, params):
+        return 'Sub Not Found'
+
+class RootController(artichoke.Controller):
+    def __init__(self, application, templates_path, helpers):
+        super(RootController, self).__init__(application, templates_path, helpers)
+        self.sub = SubController(application, os.path.join(templates_path, 'sub'), helpers)
+
+    @expose('index')
+    def index(self, args, params):
+        return dict(appname=get_application_id())

File main.py

View file
-import artichoke, os, mimetypes, base64
-from artichoke import request, response, expose, url, redirect, flash
-from artichoke import FormBuilder
-from google.appengine.api.app_identity import get_application_id
+import artichoke
+from controllers.root import RootController
+from middlewares import TransactionsMiddleware
 
-class SubController(artichoke.Controller):
-    @expose()
-    def about(self, args, params):
-        return 'HELLO %s' % str(args)
-
-    @expose()
-    def index(self, args, params):
-        return 'INDEX'
-
-    @expose()
-    def not_found(self, args, params):
-        return 'Sub Not Found'
-
-class RootController(artichoke.Controller):
-    def __init__(self, application, templates_path, helpers):
-        super(RootController, self).__init__(application, templates_path, helpers)
-        self.sub = SubController(application, os.path.join(templates_path, 'sub'), helpers)
-
-    @expose('index')
-    def index(self, args, params):
-        return dict(appname=get_application_id())
-
-app = artichoke.Application(root=RootController, templates_path='views')
+app = artichoke.Application(root=RootController, templates_path='views',
+    config={'middlewares': [TransactionsMiddleware, ]})

File middlewares/__init__.py

View file
+from transactions import TransactionsMiddleware

File middlewares/transactions.py

View file
+from google.appengine.api.datastore import _GetConnection
+
+class TransactionsMiddleware(object):
+    """Provides transaction consistency inside a request
+
+    If the request raise an exception, the transaction will be rolled back.
+
+    This middleware does not provide ACID datastore transactions, you must use
+    @db.transactional decorator to ensure the datastore's consistency before
+    and after the transaction.
+    """
+
+    def __init__(self, app, core, config):
+        self.app = app
+
+    def __call__(self, environ, start_response):
+        connection = _GetConnection()
+        try:
+            ans = self.app(environ, start_response)
+        except Exception:
+            connection.rollback()
+            raise
+        return ans