From 43bbd79f87d24af69bf8bb380889db6c7173ef32 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sun, 24 Dec 2023 17:58:31 +0100 Subject: [PATCH] fix: add timezone awareness (#3) --- requirements.txt | 1 + src/routes/set_timezone.py | 15 +++++++++++++++ src/templates/index.html | 2 ++ src/templates/login.html | 2 ++ src/templates/read.html | 2 ++ src/templates/tz.html | 6 ++++++ src/utils/config.py | 3 +++ src/utils/misc.py | 16 ++++++++++++++-- 8 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 src/routes/set_timezone.py create mode 100644 src/templates/tz.html diff --git a/requirements.txt b/requirements.txt index 5bd19d3..d2416de 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ flask==3.0.0 +pytz==2023.3.post1 diff --git a/src/routes/set_timezone.py b/src/routes/set_timezone.py new file mode 100644 index 0000000..f073e66 --- /dev/null +++ b/src/routes/set_timezone.py @@ -0,0 +1,15 @@ +from flask import Blueprint, jsonify, request, session +from utils.config import Config +from werkzeug import Response + +name = __name__.split(".")[-1] +router = Blueprint(name, __name__) + + +@router.route(f"/{name}", methods=["POST"]) +def set_timezone() -> Response: + """set timezone""" + timezone = request.data.decode("utf-8") + session[Config._session_timezone] = timezone + + return jsonify(success=True) diff --git a/src/templates/index.html b/src/templates/index.html index 3445eb2..b6c1bd6 100644 --- a/src/templates/index.html +++ b/src/templates/index.html @@ -23,5 +23,7 @@ {% endif %} + + {% include "tz.html" %} diff --git a/src/templates/login.html b/src/templates/login.html index fedb215..b15bb54 100644 --- a/src/templates/login.html +++ b/src/templates/login.html @@ -22,5 +22,7 @@ + + {% include "tz.html" %} diff --git a/src/templates/read.html b/src/templates/read.html index 0451a1d..3f496aa 100644 --- a/src/templates/read.html +++ b/src/templates/read.html @@ -23,5 +23,7 @@ back to index

+ + {% include "tz.html" %} diff --git a/src/templates/tz.html b/src/templates/tz.html new file mode 100644 index 0000000..4e0b184 --- /dev/null +++ b/src/templates/tz.html @@ -0,0 +1,6 @@ +{% if "timezone" not in session %} + +{% endif %} diff --git a/src/utils/config.py b/src/utils/config.py index 733e3c0..abac5f8 100644 --- a/src/utils/config.py +++ b/src/utils/config.py @@ -32,6 +32,9 @@ class Config: # where is stored the password info _session_login = "logged_in" + # where is stored the timezone info + _session_timezone = "timezone" + # data location data_dir = "data" diff --git a/src/utils/misc.py b/src/utils/misc.py index e6af1a9..a38db4c 100644 --- a/src/utils/misc.py +++ b/src/utils/misc.py @@ -1,8 +1,11 @@ +from datetime import datetime from os import listdir, utime from os import path as os_path from os import remove as os_remove from time import ctime +from flask import session +from pytz import timezone from utils.config import Config @@ -80,6 +83,11 @@ def exist_post(file_id: int) -> float | None: return None +def time_now() -> float: + """get current time with timezone awareness""" + return datetime.now(timezone(session[Config._session_timezone])).timestamp() + + def create_post(file_id: int, content: str) -> bool: """create a post""" # check if the file already exists @@ -91,8 +99,12 @@ def create_post(file_id: int, content: str) -> bool: with open(filename, "w", encoding="utf-8") as f: f.write(content) - # keep metadata if update: + # keep metadata utime(filename, (update, update)) return True - return False + else: + # use user's tz + now = time_now() + utime(filename, (now, now)) + return False