From 1c8c1ba9f399a0e274924ade8e32419839e3465f Mon Sep 17 00:00:00 2001 From: Mylloon Date: Fri, 22 Dec 2023 15:25:47 +0100 Subject: [PATCH] fix: date was updating at each edit --- src/utils/misc.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/utils/misc.py b/src/utils/misc.py index 247ddae..e6af1a9 100644 --- a/src/utils/misc.py +++ b/src/utils/misc.py @@ -1,4 +1,4 @@ -from os import listdir +from os import listdir, utime from os import path as os_path from os import remove as os_remove from time import ctime @@ -45,7 +45,7 @@ def get_post(filename: str) -> File | None: return None else: with open(filename, "r") as reader: - return File(reader.read(), ctime(os_path.getctime(filename))) + return File(reader.read(), ctime(os_path.getmtime(filename))) def delete_post(filename: str) -> bool: @@ -73,12 +73,11 @@ def fresh_file_id() -> int: return filename -def exist_post(file_id: int) -> bool: +def exist_post(file_id: int) -> float | None: try: - open(post_filename(file_id)) - return True + return os_path.getmtime(post_filename(file_id)) except FileNotFoundError: - return False + return None def create_post(file_id: int, content: str) -> bool: @@ -86,8 +85,14 @@ def create_post(file_id: int, content: str) -> bool: # check if the file already exists update = exist_post(file_id) + filename = post_filename(file_id) + # write into the file - with open(post_filename(file_id), "w", encoding="utf-8") as f: + with open(filename, "w", encoding="utf-8") as f: f.write(content) - return update + # keep metadata + if update: + utime(filename, (update, update)) + return True + return False