Permalink
Browse files

Added session support through postgresql

  • Loading branch information...
1 parent 499b336 commit 13b9c8a549f51e1ffa1adc8b84b401bf7056f588 @steevel steevel committed Jun 11, 2012
Showing with 90 additions and 1 deletion.
  1. +1 −0 db.py
  2. +3 −1 main.py
  3. +8 −0 schema/create.sql
  4. +3 −0 schema/drop.sql
  5. +75 −0 session.py
View
1 db.py
@@ -9,6 +9,7 @@
metadata = MetaData(bind=engine)
users = Table('users', metadata, autoload=True)
+sessions = Table('sessions', metadata, autoload=True)
blacklisted_keys = ( 'password', )
View
@@ -1,8 +1,10 @@
-from flask import Flask, render_template
+from flask import Flask, render_template, session
import api
+from session import PsqlSessionInterface
app = Flask(__name__, static_folder="static")
+app.session_interface = PsqlSessionInterface()
app.config.from_pyfile("config/settings.py")
app.register_blueprint(api.user.page, url_prefix="/api/user")
View
@@ -30,3 +30,11 @@ CREATE TABLE user_project_rel (
modified_at TIMESTAMP,
UNIQUE (user_id, project_id)
);
+
+CREATE TABLE sessions (
+ session_id TEXT NOT NULL PRIMARY KEY,
+ value TEXT NOT NULL,
+ ip TEXT NOT NULL,
+ created_at TIMESTAMP,
+ modified_at TIMESTAMP
+);
View
@@ -1,6 +1,9 @@
DROP TABLE IF EXISTS users;
DROP SEQUENCE IF EXISTS users_id_seq;
+
DROP TABLE IF EXISTS projects;
DROP SEQUENCE IF EXISTS projects_id_seq;
DROP TABLE IF EXISTS user_project_rel;
DROP SEQUENCE IF EXISTS user_project_rel_id_seq;
+
+DROP TABLE IF EXISTS sessions;
View
@@ -0,0 +1,75 @@
+from flask.session import SessionMixin, SessionInterface
+from flask import request
+from datetime import datetime
+import json
+from uuid import uuid4
+from db import sessions
+from werkzeug.datastructures import CallbackDict
+
+class PsqlSession(CallbackDict, SessionMixin):
+ def __init__(self, initial=None, sid=None, new=False):
+ print "psqlsession"
+ def on_update(self):
+ self.modified = True
+
+ CallbackDict.__init__(self, initial, on_update)
+ self.sid = sid
+ self.new = new
+ self.modified = False
+
+class PsqlSessionInterface(SessionInterface):
+ session_class = PsqlSession
+ serializer = json
+
+ def generate_sid(self):
+ print "generate_sid"
+ return str(uuid4())
+
+ def open_session(self, app, request):
+ sid = request.cookies.get(app.session_cookie_name)
+ if not sid:
+ sid = self.generate_sid()
+ return self.session_class(sid=sid, new=True)
+
+ val = sessions.select().where(
+ sessions.c.session_id == sid
+ ).execute().fetchone()
+ if val is not None:
+ # XXX either fetch only value or use a dict.
+ data = self.serializer.loads(val[1])
+ return self.session_class(data, sid=sid)
+
+ return self.session_class(sid=sid, new=True)
+
+ def save_session(self, app, session, response):
+ domain = self.get_cookie_domain(app)
+ if not session:
+ sessions.delete().where(sessions.c.session_id == session.sid)
+
+ if session.modified:
+ response.delete_cookie(app.session_cookie_name, domain=domain)
+ return
+
+ cookie_exp = self.get_expiration_time(app, session)
+ val = self.serializer.dumps(dict(session))
+
+ if session.new:
+ sessions.insert().execute(
+ session_id = session.sid,
+ value = val,
+ created_at = datetime.now(),
+ modified_at = datetime.now(),
+ ip = request.remote_addr
+ )
+ else:
+ sessions.update().where(
+ sessions.c.session_id == session.sid
+ ).values(
+ value = val,
+ modified_at = datetime.now()
+ ).execute()
+
+ response.set_cookie(app.session_cookie_name, session.sid,
+ expires=cookie_exp, httponly=True,
+ domain=domain)
+

0 comments on commit 13b9c8a

Please sign in to comment.