diff options
author | Christopher Allan Webber <cwebber@dustycloud.org> | 2014-11-30 16:14:05 -0600 |
---|---|---|
committer | Christopher Allan Webber <cwebber@dustycloud.org> | 2014-12-03 15:40:57 -0600 |
commit | a4768df0ca2e956f245e0833331ed8abb203937a (patch) | |
tree | df90466eef7ad4e739d703c1a98b8dd4fdd802a4 /mediagoblin/app.py | |
parent | 9d82dff6fb4738b4efb1a7d3280562578f54ed08 (diff) | |
download | mediagoblin-a4768df0ca2e956f245e0833331ed8abb203937a.tar.lz mediagoblin-a4768df0ca2e956f245e0833331ed8abb203937a.tar.xz mediagoblin-a4768df0ca2e956f245e0833331ed8abb203937a.zip |
Context objects now use a contextmanager (and update gmg shell to use it)
This means that we have a really convenient way to make sure that you
have a context/request that threads its way through the application,
where everything needed gets "shut down" appropriately by the end.
You always get a context object via a context manager! And by the time
you're done with it, things should be cleaned up.
Diffstat (limited to 'mediagoblin/app.py')
-rw-r--r-- | mediagoblin/app.py | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/mediagoblin/app.py b/mediagoblin/app.py index c0458596..3142224d 100644 --- a/mediagoblin/app.py +++ b/mediagoblin/app.py @@ -16,6 +16,7 @@ import os import logging +from contextlib import contextmanager from mediagoblin.routing import get_url_map from mediagoblin.tools.routing import endpoint_to_controller @@ -172,6 +173,7 @@ class MediaGoblinApp(object): self.meddleware = [common.import_component(m)(self) for m in meddleware.ENABLED_MEDDLEWARE] + @contextmanager def gen_context(self, ctx=None): """ Attach contextual information to request, or generate a context object @@ -180,6 +182,13 @@ class MediaGoblinApp(object): information (current translation, etc) are attached to this object. """ + if DISABLE_GLOBALS: + with self.db_manager.session_scope() as db: + yield self._gen_context(db, ctx) + else: + yield self._gen_context(self.db, ctx) + + def _gen_context(self, db, ctx): # Set up context # -------------- @@ -194,8 +203,7 @@ class MediaGoblinApp(object): # Also attach a few utilities from request.app for convenience? ctx.app = self - if not DISABLE_GLOBALS: - ctx.db = self.db + ctx.db = db ctx.staticdirect = self.staticdirector @@ -264,8 +272,10 @@ class MediaGoblinApp(object): environ.pop('HTTPS') ## Attach utilities to the request object - request = self.gen_context(request) + with self.gen_context(request) as request: + return self._finish_call_backend(request, environ, start_response) + def _finish_call_backend(self, request, environ, start_response): # Log user out if authentication_disabled no_auth_logout(request) @@ -305,11 +315,7 @@ class MediaGoblinApp(object): # get the Http response from the controller try: - if DISABLE_GLOBALS: - with self.db_manager.session_scope() as request.db: - response = controller(request) - else: - response = controller(request) + response = controller(request) except HTTPException as e: response = render_http_exception( request, e, e.get_description(environ)) |