add: save session initialization time

main
rrr-marble 5 years ago
parent f8ef88d442
commit de0b9cf8d3

@ -1,5 +1,6 @@
from fastapi import FastAPI from fastapi import FastAPI
from datetime import datetime
from uuid import uuid4 from uuid import uuid4
import sqlite3 import sqlite3
@ -18,16 +19,17 @@ cur = con.cursor() # NB! single is enough for now, we might require multiple la
async def new_session(): async def new_session():
"""Start a new session""" """Start a new session"""
# add session to the database # add session to the database
time = datetime.utcnow().replace(microsecond=0).isoformat()
tries = 3 # something is very wrong with our random, if we miss 3 times tries = 3 # something is very wrong with our random, if we miss 3 times
for i in range(tries): for i in range(tries):
try: try:
# generate a cookie # generate a cookie
cookie = uuid4().hex cookie = uuid4().hex
cur.execute( cur.execute(
"""INSERT INTO sessions(cookie) """INSERT INTO sessions(cookie, time)
VALUES(:cookie) VALUES(:cookie, :time)
""", """,
{"cookie": cookie}, {"cookie": cookie, "time": time},
) )
con.commit() con.commit()
except sqlite3.IntegrityError as e: except sqlite3.IntegrityError as e:

@ -138,6 +138,7 @@ def check_database(database_path: str):
( (
sessionid INTEGER PRIMARY KEY, sessionid INTEGER PRIMARY KEY,
cookie TEXT UNIQUE NOT NULL, cookie TEXT UNIQUE NOT NULL,
time TEXT,
description TEXT description TEXT
)""" )"""
) )

Loading…
Cancel
Save