|
@@ -1,66 +0,0 @@
|
1
|
|
-# -*- coding: utf-8 -*-
|
2
|
|
-import os
|
3
|
|
-import datetime
|
4
|
|
-from werkzeug.contrib.sessions import FilesystemSessionStore
|
5
|
|
-
|
6
|
|
-from lodel.interface.web.router import get_controller
|
7
|
|
-from lodel.interface.web.lodelrequest import LodelRequest
|
8
|
|
-
|
9
|
|
-# TODO Déplacer ces trois paramètres dans les settings
|
10
|
|
-SESSION_FILES_TEMPLATE = 'lodel_%s.sess'
|
11
|
|
-SESSION_FILES_BASE_DIR = 'tmp/sessions'
|
12
|
|
-SESSION_EXPIRATION_LIMIT = 900 # 15 min
|
13
|
|
-
|
14
|
|
-session_store = FilesystemSessionStore(path=SESSION_FILES_BASE_DIR, filename_template=SESSION_FILES_TEMPLATE)
|
15
|
|
-
|
16
|
|
-# TODO Déplacer cette méthode dans un module Lodel/utils/datetime.py
|
17
|
|
-def get_utc_timestamp():
|
18
|
|
- d = datetime.datetime.utcnow()
|
19
|
|
- epoch = datetime.datetime(1970, 1, 1)
|
20
|
|
- t = (d - epoch).total_seconds()
|
21
|
|
- return t
|
22
|
|
-
|
23
|
|
-# TODO déplacer dans un module "sessions.py"
|
24
|
|
-def delete_old_session_files(timestamp_now):
|
25
|
|
- session_files_path = os.path.abspath(session_store.path)
|
26
|
|
- session_files = [file_object for file_object in os.listdir(session_files_path)
|
27
|
|
- if os.path.isfile(os.path.join(session_files_path, file_object))]
|
28
|
|
- for session_file in session_files:
|
29
|
|
- expiration_timestamp = os.statos.path.join(session_files_path, session_file)).st_mtime + \
|
30
|
|
- SESSION_EXPIRATION_LIMIT
|
31
|
|
- if timestamp_now > expiration_timestamp:
|
32
|
|
- os.unlink(os.path.join(session_files_path, session_file))
|
33
|
|
-
|
34
|
|
-
|
35
|
|
-# TODO Déplacer dans une module "sessions.py"
|
36
|
|
-def is_session_file_expired(timestamp_now, sid):
|
37
|
|
- session_file = session_store.get_session_filename(sid)
|
38
|
|
- expiration_timestamp = os.stat(session_file).st_mtime + SESSION_EXPIRATION_LIMIT
|
39
|
|
- if timestamp_now < expiration_timestamp:
|
40
|
|
- return False
|
41
|
|
- return True
|
42
|
|
-
|
43
|
|
-# WSGI Application
|
44
|
|
-def application(env, start_response):
|
45
|
|
- current_timestamp = get_utc_timestamp()
|
46
|
|
- delete_old_session_files(current_timestamp)
|
47
|
|
- request = LodelRequest(env)
|
48
|
|
- sid = request.cookies.get('sid')
|
49
|
|
- if sid is None or sid not in session_store.list():
|
50
|
|
- request.session = session_store.new()
|
51
|
|
- request.session['last_accessed'] = current_timestamp
|
52
|
|
- else:
|
53
|
|
- request.session = session_store.get(sid)
|
54
|
|
- if is_session_file_expired(current_timestamp, sid):
|
55
|
|
- session_store.delete(request.session)
|
56
|
|
- request.session = session_store.new()
|
57
|
|
- request.session['user_context'] = None
|
58
|
|
- request.session['last_accessed'] = current_timestamp
|
59
|
|
-
|
60
|
|
- controller = get_controller(request)
|
61
|
|
- response = controller(request)
|
62
|
|
- if request.session.should_save:
|
63
|
|
- session_store.save(request.session)
|
64
|
|
- response.set_cookie('sid', request.session.sid)
|
65
|
|
-
|
66
|
|
- return response(env, start_response)
|