summaryrefslogtreecommitdiff
path: root/src/jwebmail
diff options
context:
space:
mode:
authorJannis M. Hoffmann <jannis@fehcom.de>2024-11-04 12:47:26 +0100
committerJannis M. Hoffmann <jannis@fehcom.de>2024-11-04 12:47:26 +0100
commita2cca19e4f39620b6674fb6cbbe3287a49eec5c5 (patch)
treee291d9dbe768dc30194f2e5028b76a4a1f38d985 /src/jwebmail
parentd18f9d3b0e4f16c3ec6e2f41ff567e0e2891bbf0 (diff)
use context manager in MysqlTimeoutSession
add documentation for database creation
Diffstat (limited to 'src/jwebmail')
-rw-r--r--src/jwebmail/__init__.py2
-rw-r--r--src/jwebmail/read_mails.py45
2 files changed, 24 insertions, 23 deletions
diff --git a/src/jwebmail/__init__.py b/src/jwebmail/__init__.py
index 631215b..875bcb5 100644
--- a/src/jwebmail/__init__.py
+++ b/src/jwebmail/__init__.py
@@ -36,7 +36,7 @@ else:
toml_read_file = dict(load=toml_load, text=True)
-__version__ = "2.3.0.dev3"
+__version__ = "2.3.0.dev4"
csrf = CSRFProtect()
diff --git a/src/jwebmail/read_mails.py b/src/jwebmail/read_mails.py
index 44eba8d..404a242 100644
--- a/src/jwebmail/read_mails.py
+++ b/src/jwebmail/read_mails.py
@@ -1,3 +1,4 @@
+from contextlib import closing
from datetime import datetime, timedelta
from flask import current_app, g
@@ -36,7 +37,7 @@ class RedisTimeoutSession:
class MysqlTimeoutSession:
- def __init__(self, username, passwd, timeout, port=3306):
+ def __init__(self, username, passwd, timeout, database="jwebmaildb1", port=3306):
import mysql.connector
self.timeout = timeout
@@ -45,35 +46,35 @@ class MysqlTimeoutSession:
port=port,
username=username,
password=passwd,
- database="jwebmaildb1",
+ database=database,
)
def set(self, key, value):
timeout = datetime.now() + timedelta(seconds=self.timeout)
- cur = self.conn.cursor()
- cur.execute("DELETE FROM session WHERE user = %s", [key])
- cur.execute("INSERT INTO session VALUES (%s, %s, %s)", [key, value, timeout])
- self.conn.commit()
- cur.close()
- def get(self, key):
- cur = self.conn.cursor()
- cur.execute("DELETE FROM session WHERE timeout < NOW()")
- cur.execute("SELECT password FROM session WHERE user = %s", [key])
- row = cur.fetchone()
-
- if row is None:
- self.conn.commit()
- cur.close()
- return None
- else:
- timeout = datetime.now() + timedelta(seconds=self.timeout)
+ with closing(self.conn.cursor()) as cur:
+ cur.execute("DELETE FROM session WHERE user = %s", [key])
cur.execute(
- "UPDATE session SET timeout = %s WHERE user = %s", [timeout, key]
+ "INSERT INTO session VALUES (%s, %s, %s)", [key, value, timeout]
)
self.conn.commit()
- cur.close()
- return row[0]
+
+ def get(self, key):
+ with closing(self.conn.cursor()) as cur:
+ cur.execute("DELETE FROM session WHERE timeout < NOW()")
+ cur.execute("SELECT password FROM session WHERE user = %s", [key])
+ row = cur.fetchone()
+
+ if row is None:
+ self.conn.commit()
+ return None
+ else:
+ timeout = datetime.now() + timedelta(seconds=self.timeout)
+ cur.execute(
+ "UPDATE session SET timeout = %s WHERE user = %s", [timeout, key]
+ )
+ self.conn.commit()
+ return row[0]
def select_timeout_session():