From b9b762d793087b863b7079004d11ff4ec13b2906 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Fri, 22 Dec 2023 15:00:51 +0100 Subject: [PATCH] feat: add date of posts (fix #1) --- src/routes/read.py | 7 ++++--- src/templates/read.html | 5 ++++- src/utils/misc.py | 14 ++++++++++++-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/routes/read.py b/src/routes/read.py index 2a7c73d..d56eeb6 100644 --- a/src/routes/read.py +++ b/src/routes/read.py @@ -10,10 +10,10 @@ router = Blueprint(name, __name__) @router.route(f"/{name}/") def read(file: int) -> str: """read page""" - content = None + post = None if not Config.private or (Config.private and Config.is_logged()): filename = post_filename(file) - content = get_post(filename) + post = get_post(filename) return render_template( "read.html", @@ -21,8 +21,9 @@ def read(file: int) -> str: read_page=True, page_name=name, name=f"{file}.txt", - file=content, + file=post.content if post else None, post_file_id=post_file_id, + date=post.creation_date if post else None, ) diff --git a/src/templates/read.html b/src/templates/read.html index d52a232..a20e696 100644 --- a/src/templates/read.html +++ b/src/templates/read.html @@ -17,7 +17,10 @@ diff --git a/src/utils/misc.py b/src/utils/misc.py index eff0779..247ddae 100644 --- a/src/utils/misc.py +++ b/src/utils/misc.py @@ -1,6 +1,7 @@ from os import listdir from os import path as os_path from os import remove as os_remove +from time import ctime from utils.config import Config @@ -27,7 +28,16 @@ def get_posts() -> list[str]: return posts -def get_post(filename: str) -> str | None: +class File: + def __init__(self, content: str, cdate: str): + # content + self.content = content + + # date of creation + self.creation_date = cdate + + +def get_post(filename: str) -> File | None: """get a post""" try: open(filename) @@ -35,7 +45,7 @@ def get_post(filename: str) -> str | None: return None else: with open(filename, "r") as reader: - return reader.read() + return File(reader.read(), ctime(os_path.getctime(filename))) def delete_post(filename: str) -> bool: