-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.py
More file actions
73 lines (60 loc) · 2.32 KB
/
Copy pathsession.py
File metadata and controls
73 lines (60 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from flask.sessions 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):
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):
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)