From 5de76b2da501baf0867dc5caa6cd8b6621156385 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sun, 24 Dec 2023 18:13:48 +0100 Subject: [PATCH] feat: ability to change the session lifetime, now stay for 7 days (fix #4) --- src/app.py | 10 +++++++++- src/utils/config.py | 6 ++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/app.py b/src/app.py index bac78ba..79115d3 100644 --- a/src/app.py +++ b/src/app.py @@ -1,7 +1,8 @@ +from datetime import timedelta from glob import glob from os import mkdir, urandom -from flask import Flask +from flask import Flask, session from utils.config import Config app = Flask(__name__, static_url_path="/") @@ -14,6 +15,13 @@ for file in glob("*/routes/*.py"): app.secret_key = urandom(12) + +@app.before_request +def make_session_permanent(): + session.permanent = True + app.permanent_session_lifetime = timedelta(days=Config.session_lifetime) + + # create data directory where posts are stored try: mkdir(Config.data_dir) diff --git a/src/utils/config.py b/src/utils/config.py index abac5f8..7c2ca40 100644 --- a/src/utils/config.py +++ b/src/utils/config.py @@ -5,6 +5,7 @@ from flask import session VAR_USERNAME = "TD_USERNAME" VAR_USERPASS = "TD_USERPASS" VAR_PRIVATE = "TD_PRIVATE" +VAR_LOGLIFETIME = "TD_LOGINLIFETIME" class User: @@ -41,6 +42,11 @@ class Config: # turn posts private private = True if VAR_PRIVATE in envar else False + # session duration in days + session_lifetime = ( + float(envar[VAR_LOGLIFETIME]) if VAR_LOGLIFETIME in envar else 7.0 + ) + @staticmethod def is_logged() -> bool: """where the info about connection is stored"""