Created
November 18, 2014 18:49
-
-
Save kgriffs/b4eb14c91212f2fa3a0a to your computer and use it in GitHub Desktop.
SQLAlchemySessionManager (B)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ========================================================================= | |
# middleware.py | |
# ========================================================================= | |
import sqlalchemy.orm.scoping as scoping | |
class SQLAlchemySessionManager(object): | |
def __init__(self, session_factory, auto_commit=False): | |
self._session_factory = session_factory | |
self._scoped = isinstance(session_factory, scoping.ScopedSession) | |
self._auto_commit = auto_commit | |
def process_request(self, req, resp, params): | |
req.context['session'] = self._session_factory() | |
def process_response(self, req, resp): | |
session = req.context['session'] | |
if self._auto_commit: | |
session.commit() | |
if self._scoped: | |
session.remove() | |
else: | |
session.close() | |
# ========================================================================= | |
# db.py | |
# ========================================================================= | |
import sqlalchemy.orm as orm | |
Session = orm.scoped_session(orm.sessionmaker()) | |
# ========================================================================= | |
# app.py | |
# ========================================================================= | |
import falcon | |
import sqlalchemy as sa | |
import db | |
import middleware | |
# TODO: Load engine options from a config file | |
engine = sa.create_engine('postgresql://scott:tiger@localhost/') | |
db.Session.configure(bind=engine) | |
session_manager = middleware.SQLAlchemySessionManager(db.Session) | |
api = falcon.API(middleware=[session_manager]) | |
# Alternatively, you could use the global hooks mechanism, but that is | |
# deprecated so will probably go away eventually (we need to sort out how | |
# to keep talons compatible with the latest falcon when that happens) | |
api_alt = falcon.API(before=[session_manager.process_request], | |
after=[session_manager.process_response]) | |
# ========================================================================= | |
# somewhere_else.py | |
# ========================================================================= | |
import db | |
class Foo(object): | |
def do_something(): | |
# Get the scoped session object (from thread-local storage) | |
session = db.Session() | |
# <use the session> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi!
The example doesnt work properly, I get a failure containing the following traceback:
The type of req.context['session'] is 'sqlalchemy.orm.session.Session'.
The line 14 should be:
?