fix: add timezone awareness (#3)
All checks were successful
ci/woodpecker/push/publish Pipeline was successful
All checks were successful
ci/woodpecker/push/publish Pipeline was successful
This commit is contained in:
parent
e1277dd7aa
commit
43bbd79f87
8 changed files with 45 additions and 2 deletions
|
@ -1 +1,2 @@
|
||||||
flask==3.0.0
|
flask==3.0.0
|
||||||
|
pytz==2023.3.post1
|
||||||
|
|
15
src/routes/set_timezone.py
Normal file
15
src/routes/set_timezone.py
Normal 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)
|
|
@ -23,5 +23,7 @@
|
||||||
</ul>
|
</ul>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
|
{% include "tz.html" %}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -22,5 +22,7 @@
|
||||||
</form></span
|
</form></span
|
||||||
>
|
>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
|
{% include "tz.html" %}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -23,5 +23,7 @@
|
||||||
<a href="/">back to index</a>
|
<a href="/">back to index</a>
|
||||||
</p>
|
</p>
|
||||||
</footer>
|
</footer>
|
||||||
|
|
||||||
|
{% include "tz.html" %}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
6
src/templates/tz.html
Normal file
6
src/templates/tz.html
Normal 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 %}
|
|
@ -32,6 +32,9 @@ class Config:
|
||||||
# where is stored the password info
|
# where is stored the password info
|
||||||
_session_login = "logged_in"
|
_session_login = "logged_in"
|
||||||
|
|
||||||
|
# where is stored the timezone info
|
||||||
|
_session_timezone = "timezone"
|
||||||
|
|
||||||
# data location
|
# data location
|
||||||
data_dir = "data"
|
data_dir = "data"
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
|
from datetime import datetime
|
||||||
from os import listdir, utime
|
from os import listdir, utime
|
||||||
from os import path as os_path
|
from os import path as os_path
|
||||||
from os import remove as os_remove
|
from os import remove as os_remove
|
||||||
from time import ctime
|
from time import ctime
|
||||||
|
|
||||||
|
from flask import session
|
||||||
|
from pytz import timezone
|
||||||
from utils.config import Config
|
from utils.config import Config
|
||||||
|
|
||||||
|
|
||||||
|
@ -80,6 +83,11 @@ def exist_post(file_id: int) -> float | None:
|
||||||
return 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:
|
def create_post(file_id: int, content: str) -> bool:
|
||||||
"""create a post"""
|
"""create a post"""
|
||||||
# check if the file already exists
|
# 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:
|
with open(filename, "w", encoding="utf-8") as f:
|
||||||
f.write(content)
|
f.write(content)
|
||||||
|
|
||||||
# keep metadata
|
|
||||||
if update:
|
if update:
|
||||||
|
# keep metadata
|
||||||
utime(filename, (update, update))
|
utime(filename, (update, update))
|
||||||
return True
|
return True
|
||||||
return False
|
else:
|
||||||
|
# use user's tz
|
||||||
|
now = time_now()
|
||||||
|
utime(filename, (now, now))
|
||||||
|
return False
|
||||||
|
|
Loading…
Reference in a new issue