def start(cli): token = cli.get_secure_cookie('token').decode('UTF-8') if(cli.get_secure_cookie('token') is not None) else None if token: try: Sessions.delete().where(Sessions.last_active <= datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) db_sess = Sessions.get(Sessions.session_key == token) except peewee.DoesNotExist: db_sess = None except Exception as e: print('Get current session from DB Error:', e.__str__()) db_sess = None if db_sess is None: print('crt_sess if no in DB ->>') sess = Session.create_session({}) cli.set_secure_cookie('token', sess.sess_token) else: print('Get sess from DB:', db_sess.session_key) sess = Session.restore_session(db_sess.session_key, Utils.convert_json_to_dict(db_sess.session_data)) else: print('crt_sess ->>') sess = Session.create_session({}) cli.set_secure_cookie('token', sess.sess_token, 1) sess.update_session() print('Token', sess.sess_token) print('Sess Data:', sess.sess_data) return sess
def prepare_request_body(req_body): return Utils.convert_json_to_dict(req_body.decode("UTF-8"))
def init_from_json(json_string, sess): json_obj = Utils.convert_json_to_dict(json_string) if 'event' not in json_obj or 'data' not in json_obj: raise Exception('Incorrect JSON format') return Req(json_obj['event'], json_obj['data'], sess)