def on_app_start(self): super(WSApp, self).on_app_start() # Each time you start the app created db-manager for the website. self.app_db_client_manager = ObjectManager(self.app_db_client) # Launch the app's case and the app was not created in a database. with self.app_db_client_manager() as db_client: with self.app_db(db_client) as db: app_settings = db.Settings.pull_one() if app_settings is None: self.on_app_create() db.Settings(db_created=True).save()
class WSApp(App): app_db = WSDatabase app_db_client = MongoClient app_db_client_manager = None app_request_cls = WSRequest lang = LangDict() TIME_ALIVE_FOR_SESSION = 60 * 20 TIME_SEELP_FOR_SESSION_WATCHER = 10 @cached_property def cookie(self): return type(self).__name__ @cached_property def session_cookie(self): return self.cookie+'SID' def __init__(self): super(WSApp, self).__init__() self.request_maker = type(type(self).__name__ + 'Request', (self.app_request_cls,), dict(app=self)) def _create_session(self, db): session = db.Session(token=secret()) self.on_session(session) return session def session(self, req): with self.app_db(req.db_client) as db: token = req.cookies.get(self.session_cookie) if token is None: return self._create_session(db) row = db.Session.doc(token=token) if row is not None: return row req.cookies.pop(self.session_cookie, None) return self._create_session(db) @property def _no_alive_sessions_query(self): return Q.where("this.last_time+%d < %d", self.TIME_ALIVE_FOR_SESSION, Datetime()).S @property def _no_alive_sessions(self): return self.app_db.Session.docs(self._no_alive_sessions_query) def _sessions_watcher(self): while True: with self.app_db_client_manager() as db_client: with self.app_db(db_client): for session in self._no_alive_sessions: self.on_session_end(session) WSResourceManager(session).delete() session.remove() sleep(self.TIME_SEELP_FOR_SESSION_WATCHER) def on_app_watch(self): super(WSApp, self).on_app_watch() start_new_thread(self._sessions_watcher, ()) def on_session(self, session): pass def on_session_end(self, session): pass def on_app_create(self): pass def on_app_start(self): super(WSApp, self).on_app_start() # Each time you start the app created db-manager for the website. self.app_db_client_manager = ObjectManager(self.app_db_client) # Launch the app's case and the app was not created in a database. with self.app_db_client_manager() as db_client: with self.app_db(db_client) as db: app_settings = db.Settings.pull_one() if app_settings is None: self.on_app_create() db.Settings(db_created=True).save() def on_response(self, req, res): super(WSApp, self).on_response(req, res) # Testing if used in the session object of the request. if 'session' in vars(req): with self.app_db(req.db_client): if not req.session.created: # If the session just created it should be kept in the database. req.session.save() else: # Otherwise there is only "update the last access to the session in order that he would timeout. req.session.commit(last_time=Datetime.NOW) # Keeps the token of the session with the client. if self.session_cookie not in req.cookies: res.set_cookie(self.session_cookie, req.session.token) # Checks if the database is used during the processing of the application, if we do then release operation # for the connection. if 'db_client' in vars(req): self.app_db_client_manager.release(req.db_client) def on_request_error(self, req, error): super(WSApp, self).on_request_error(req, error) if 'db_client' in vars(req): self.app_db_client_manager.release(req.db_client)