fix: add timezone awareness (#3)
All checks were successful
ci/woodpecker/push/publish Pipeline was successful

This commit is contained in:
Mylloon 2023-12-24 17:58:31 +01:00
parent e1277dd7aa
commit 43bbd79f87
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
8 changed files with 45 additions and 2 deletions

View file

@ -1 +1,2 @@
flask==3.0.0
pytz==2023.3.post1

View file

@ -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)

View file

@ -23,5 +23,7 @@
</ul>
{% endif %}
</main>
{% include "tz.html" %}
</body>
</html>

View file

@ -22,5 +22,7 @@
</form></span
>
</main>
{% include "tz.html" %}
</body>
</html>

View file

@ -23,5 +23,7 @@
<a href="/">back to index</a>
</p>
</footer>
{% include "tz.html" %}
</body>
</html>

6
src/templates/tz.html Normal file
View file

@ -0,0 +1,6 @@
{% if "timezone" not in session %}
<script type="text/javascript">
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
fetch("/set_timezone", { method: "POST", body: timezone });
</script>
{% endif %}

View file

@ -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"

View file

@ -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
else:
# use user's tz
now = time_now()
utime(filename, (now, now))
return False