Skip to content

Commit

Permalink
Use Beaker for Flask session interface
Browse files Browse the repository at this point in the history
  • Loading branch information
brew committed May 26, 2016
1 parent f7f2765 commit d4367d4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
25 changes: 24 additions & 1 deletion ckan/common.py
Expand Up @@ -12,7 +12,7 @@
from flask.ext.babel import gettext as flask_gettext
from pylons.i18n import _ as pylons_gettext, ungettext

from pylons import g, session, response
from pylons import g, response
import simplejson as json

try:
Expand Down Expand Up @@ -87,3 +87,26 @@ def __delattr__(self, name):


c = PylonsStyleContext()


class Session():

def __getattr__(self, name):
if is_flask():
return getattr(flask.session, name, None)
else:
return getattr(pylons.session, name, None)

def __setattr__(self, name, value):
if is_flask():
return setattr(flask.session, name, value)
else:
return setattr(pylons.session, name, value)

def __delattr__(self, name):
if is_flask():
return delattr(flask.session, name, None)
else:
return delattr(pylons.session, name, None)

session = Session()
13 changes: 13 additions & 0 deletions ckan/config/middleware.py
Expand Up @@ -28,6 +28,7 @@
from flask import request as flask_request
from flask import _request_ctx_stack
from flask.ctx import _AppCtxGlobals
from flask.sessions import SessionInterface
from werkzeug.exceptions import HTTPException
from werkzeug.test import create_environ, run_wsgi_app
from flask.ext.babel import Babel
Expand Down Expand Up @@ -260,6 +261,18 @@ def make_flask_stack(conf, **app_conf):

# Do all the Flask-specific stuff before adding other middlewares

# Use Beaker as the Flask session interface
class BeakerSessionInterface(SessionInterface):
def open_session(self, app, request):
session = request.environ['beaker.session']
return session

def save_session(self, app, session, response):
session.save()

app.wsgi_app = SessionMiddleware(app.wsgi_app)
app.session_interface = BeakerSessionInterface()

# secret key needed for flask-debug-toolbar
app.config['SECRET_KEY'] = '<replace with a secret key>'
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
Expand Down

0 comments on commit d4367d4

Please sign in to comment.