Browse Source

Changed the implementation of the filesystem_session main module

Roland Haroutiounian 8 years ago
parent
commit
024287982b
1 changed files with 60 additions and 32 deletions
  1. 60
    32
      plugins/filesystem_session/main.py

+ 60
- 32
plugins/filesystem_session/main.py View File

@@ -15,15 +15,46 @@ SESSION_EXPIRATION_LIMIT = Settings.sessions.expiration
15 15
 
16 16
 
17 17
 ## @brief generates a new session id
18
-# @return str
19 18
 def generate_new_sid():
20
-    new_sid = uuid.uuid1()
21
-    return new_sid
19
+    return uuid.uuid1()
20
+
22 21
 
23
-## @brief gets the session file path corresponding to a session id
22
+## @brief gets the session file path, given a session id
23
+# @param sid str : session id
24
+# @return str
24 25
 def get_session_file_path(sid):
25 26
     return os.path.join(SESSION_FILES_BASE_DIR, SESSION_FILES_TEMPLATE) % sid
26 27
 
28
+
29
+## @brief saves the session content to a session file
30
+# @param sid str: session id
31
+# @param session dict : session content to be saved
32
+def save_session(sid, session):
33
+    session_file_path = get_session_file_path(sid)
34
+    pickle.dump(session, open(session_file_path, "wb"))
35
+
36
+
37
+## @brief checks if a session file has expired
38
+# @param sid str : session id
39
+# @return bool
40
+def is_session_expired(sid):
41
+    session_file = get_session_file_path(sid)
42
+    if not os.path.isfile(session_file):
43
+        raise AuthenticationError("No session file found for the sid : %s" % sid)
44
+
45
+    expiration_timestamp = os.stat(session_file).st_mtime + SESSION_EXPIRATION_LIMIT
46
+    now_timestamp = get_utc_timestamp()
47
+    return now_timestamp >= expiration_timestamp
48
+
49
+
50
+## @brief reads a session content
51
+# @param sid str: session id
52
+# @return dict
53
+def get_session_content(sid):
54
+    session_file_path = get_session_file_path(sid)
55
+    return pickle.load(open(session_file_path), 'rb')
56
+
57
+
27 58
 ## @brief starts a new session and returns its sid
28 59
 # @param caller *
29 60
 # @param payload dict
@@ -35,64 +66,61 @@ def start_session(caller, payload):
35 66
     session = dict()
36 67
     for key, value in payload.items():
37 68
         session[key] = value
38
-    pickle.dump(session, open(session_file, "wb"))
69
+    save_session(sid, session)
39 70
     return sid
40 71
 
41
-
42 72
 ## @brief stops a session
43 73
 # @param caller *
44 74
 # @param sid str : session id
45
-@LodelHook('session_stop')
75
+@LodelHook('session_destroy')
46 76
 def stop_session(caller, sid):
47 77
     session_file_path = get_session_file_path(sid)
48 78
     if os.path.isfile(session_file_path):
49 79
         os.unlink(session_file_path)
50 80
     else:
51
-        raise AuthenticationError("No session file found for the sid : %s" % sid)
52
-
53
-
54
-## @brief checks if a session file has expired
55
-# @param sid str : session id
56
-# @return bool
57
-def is_session_file_expired(sid):
58
-    session_file = get_session_file_path(sid)
59
-
60
-    if not os.path.isfile(session_file):
61
-        raise AuthenticationError("No session file found for the sid : %s" % sid)
62
-
63
-    expiration_timestamp = os.stat(session_file).st_mtime + SESSION_EXPIRATION_LIMIT
64
-    timestamp_now = get_utc_timestamp()
65
-    return timestamp_now >= expiration_timestamp
81
+        raise AuthenticationError("No session file found for the sid %s" % sid)
66 82
 
67 83
 
68 84
 ## @brief reads a session content
69 85
 # @param caller *
70
-# @param sid str : session id
86
+# @param sid str: session id
71 87
 @LodelHook('session_load')
72 88
 def read_session(caller, sid):
73 89
     session_file = get_session_file_path(sid)
74 90
     if os.path.isfile(session_file):
75
-        if not is_session_file_expired(sid):
76
-            session = pickle.load(open(session_file, "rb"))
91
+        if not is_session_expired(sid):
92
+            session = get_session_content(sid)
77 93
         else:
78 94
             LodelHook.call_hook('session_stop', __file__, sid)
79 95
             session = {}
80 96
     else:
81
-        raise AuthenticationError("No session file found for the sid : %s" % sid)
82
-
97
+        raise AuthenticationError("No session file found for the sid %s" % sid)
83 98
     return session
84 99
 
85 100
 
86 101
 ## @brief deletes all old session files (expired ones)
87 102
 # @param caller *
88
-@LodelHook('lodel_delete_old_session_files')
89
-def delete_old_session_files(caller):
103
+@LodelHook('session_clean')
104
+def clean_sessions(caller):
90 105
     session_files_path = os.path.abspath(SESSION_FILES_BASE_DIR)
91 106
     session_files = [os.path.join(session_files_path, file_object) for file_object in os.listdir(session_files_path) if os.path.isfile(os.path.join(session_files_path, file_object))]
92
-    timestamp_now = get_utc_timestamp()
93
-
107
+    now_timestamp = get_utc_timestamp()
94 108
     for session_file in session_files:
95 109
         last_modified = os.stat(session_file).st_mtime
96 110
         expiration_timestamp = last_modified + SESSION_EXPIRATION_LIMIT
97
-        if timestamp_now > expiration_timestamp:
111
+        if now_timestamp > expiration_timestamp:
98 112
             os.unlink(session_file)
113
+
114
+## @brief updates the content session
115
+# @param caller *
116
+# @param payload dict: datas to insert/update in the session
117
+@LodelHook('update_session')
118
+def update_session_content(caller, payload):
119
+    if 'sid' in payload:
120
+        sid = payload['sid']
121
+        session = LodelHook.call_hook('session_load', __file__, sid)
122
+        for key, value in payload.items():
123
+            session[key] = value
124
+        save_session(sid, session)
125
+    else:
126
+        raise AuthenticationError("Missing session id in the request")

Loading…
Cancel
Save