2016-03-28 20:31:08 +00:00
|
|
|
''' Exports DbSession. '''
|
|
|
|
|
|
|
|
class DbSession(object):
|
|
|
|
''' Attaches database session to the context of every request. '''
|
|
|
|
|
|
|
|
def __init__(self, session_factory):
|
|
|
|
self._session_factory = session_factory
|
|
|
|
|
|
|
|
def process_request(self, request, response):
|
|
|
|
''' Executed before passing the request to the API. '''
|
2016-03-28 20:53:56 +00:00
|
|
|
request.context.session = self._session_factory()
|
2016-03-28 20:31:08 +00:00
|
|
|
|
|
|
|
def process_response(self, request, response, resource):
|
2016-03-28 20:53:56 +00:00
|
|
|
'''
|
|
|
|
Executed before passing the response to falcon.
|
|
|
|
Any commits to database need to happen explicitly in the API layer.
|
|
|
|
'''
|
|
|
|
request.context.session.close()
|