Browse Source

Changed method types in session module

Roland Haroutiounian 8 years ago
parent
commit
2cf23a50c8
2 changed files with 9 additions and 118 deletions
  1. 7
    116
      lodel/auth/session.py
  2. 2
    2
      plugins/filesystem_session/filesystem_session_store.py

+ 7
- 116
lodel/auth/session.py View File

126
         delattr(p_object=self, key)
126
         delattr(p_object=self, key)
127
         self.save()
127
         self.save()
128
 
128
 
129
-
130
-
131
-
132
-
133
-
134
-
135
-'''
136
-class SessionStore(ABC):
137
-
138
-    expiration_limit = 900
139
-
140
-    _instance = None
141
-
142
-    def __init__(self, session_class=Session):
143
-        self.session_class = session_class
144
-        if self.__class__._instance is None:
145
-            self.__class__._instance = self.__class__()
146
-
147
-    def __getattr__(self, item):
148
-        return getattr(self.__class__._instance, item)
149
-
150
-    ## @brief Generates a session unique ID
151
-    # @param cls
152
-    # @param salt str
153
-    # @return str
154
-    @classmethod
155
-    def generate_new_sid(cls, salt=None):
156
-        return generate_key(salt)
157
-
158
-    ## @brief Creates a new session
159
-    # @param content dict : session content (default: {})
160
-    # @return str
161
-    def create_new_session(self, content={}):
162
-        sid = self.__class__.generate_new_sid()
163
-        self.save_session(sid, content)
164
-        return sid
165
-
166
-    ## @brief Destroys a session
167
-    # @param sid str : session id
168
-    @abstractmethod
169
-    def delete_session(self, sid):
170
-        pass
171
-
172
-    ## @brief Reads a session content
173
-    # @param sid str : session id
174
-    # @return dict
175
-    @abstractmethod
176
-    def read_session(self, sid):
177
-        return {}
178
-
179
-    ## @brief saves a session to a file
180
-    # @param sid str : session id
181
-    # @param session dict : content to be saved
182
-    @abstractmethod
183
-    def save_session(self, sid, session):
184
-        pass
185
-
186
-    ## @brief lists all the sessions ids
129
+    ## @brief lists all the sessions in the session store
187
     # @return list
130
     # @return list
131
+    @classmethod
188
     @abstractmethod
132
     @abstractmethod
189
-    def list_all_sessions(self):
133
+    def list_all_sessions(cls):
190
         return []
134
         return []
191
 
135
 
192
-    ## @brief cleans the session store's by destroying the expired sessions
193
-    def clean(self):
194
-        sessions_list = self.list_all_sessions()
195
-        for sid in sessions_list:
196
-            if self.has_session_expired(sid):
197
-                self.delete_session(sid)
198
-
199
-    ## @brief checks if a session exists
200
-    # @param sid str : session id
136
+    ## @brief checks if a given session id corresponds to an existing session
137
+    # @param sid str: session id
201
     # @return bool
138
     # @return bool
139
+    @classmethod
202
     @abstractmethod
140
     @abstractmethod
203
-    def is_session_existing(self, sid):
141
+    def exists(cls, sid):
204
         return True
142
         return True
205
-
206
-    ## @brief gets a session's last modified timestamp
207
-    # @param sid str: session id
208
-    # @return float
209
-    @abstractmethod
210
-    def get_session_last_modified(self, sid):
211
-        pass
212
-
213
-    ## @brief checks if a session has expired
214
-    # @param sid str: session id
215
-    # @return bool
216
-    def has_session_expired(self, sid):
217
-        session_last_modified = self.get_session_last_modified(sid)
218
-        expiration_timestamp = session_last_modified + self.expiration_limit
219
-        now_timestamp = get_utc_timestamp()
220
-        return now_timestamp > expiration_timestamp
221
-
222
-    ## @brief updates a session's content
223
-    # @param sid str : session's id
224
-    # @param content dict : items to update with their new value
225
-    def update_session(self, sid, content):
226
-        session = self.get_session(sid)
227
-        if session is not None:
228
-            for key, value in content.items():
229
-                if key != 'sid':
230
-                    session[key] = value
231
-            self.save_session(sid, session)
232
-            return session
233
-        else:
234
-            session = self.create_new_session(content)
235
-            return session
236
-
237
-    ## @brief gets a session's content
238
-    # @param sid str : id of the session to read
239
-    # @return dict | None if no valid session if found
240
-    def get_session(self, sid):
241
-        if self.is_session_existing(sid):
242
-            if not self.has_session_expired(sid):
243
-                session = self.read_session(sid)
244
-            else:
245
-                self.delete_session(sid)
246
-                session = None
247
-        else:
248
-            session = None
249
-
250
-        return session
251
-'''

+ 2
- 2
plugins/filesystem_session/filesystem_session_store.py View File

52
     @classmethod
52
     @classmethod
53
     def clean(cls):
53
     def clean(cls):
54
         # unregistered files in the session directory (if any)
54
         # unregistered files in the session directory (if any)
55
-        session_dir_files = cls.list_all_session_files()
55
+        session_dir_files = cls.list_all_sessions()
56
         for session_dir_file in session_dir_files:
56
         for session_dir_file in session_dir_files:
57
             sid = cls.filename_to_sid(session_dir_file)
57
             sid = cls.filename_to_sid(session_dir_file)
58
             if sid is None or sid not in cls.__sessions.keys():
58
             if sid is None or sid not in cls.__sessions.keys():
71
     ## @brief lists all the files contained in the session directory
71
     ## @brief lists all the files contained in the session directory
72
     # @return list
72
     # @return list
73
     @classmethod
73
     @classmethod
74
-    def list_all_session_files(cls):
74
+    def list_all_sessions(cls):
75
         session_files_directory = os.abspath(cls.BASE_DIRECTORY)
75
         session_files_directory = os.abspath(cls.BASE_DIRECTORY)
76
         files_list = [file_path for file_path in os.listdir(session_files_directory) if os.path.isfile(os.path.join(session_files_directory, file_path))]
76
         files_list = [file_path for file_path in os.listdir(session_files_directory) if os.path.isfile(os.path.join(session_files_directory, file_path))]
77
         return files_list
77
         return files_list

Loading…
Cancel
Save