diff --git a/src/routes/index.py b/src/routes/index.py index 84439b9..d925e8f 100644 --- a/src/routes/index.py +++ b/src/routes/index.py @@ -18,14 +18,17 @@ def index() -> str: ) -@router.route("/", methods=["POST"]) -def new_post() -> Response: +@router.route("/", methods=["POST"], defaults={"post_id": None}) +@router.route("/", methods=["POST"]) +def new_post(post_id: int | None) -> Response: """create a new post""" if Config.is_logged(): content = request.form.get("p") if content: - # finding an appropriate filename - filename = fresh_file_id() + if post_id is None: + filename = fresh_file_id() + else: + filename = post_id # create/update the post update = create_post(filename, content) diff --git a/src/routes/read.py b/src/routes/read.py index 33afafc..05bf247 100644 --- a/src/routes/read.py +++ b/src/routes/read.py @@ -1,6 +1,6 @@ from config import Config from flask import Blueprint, flash, redirect, render_template -from utils.misc import delete_post, get_post, post_filename +from utils.misc import delete_post, get_post, post_filename, post_file_id from werkzeug import Response name = __name__.split(".")[-1] @@ -20,6 +20,7 @@ def read(file: int) -> str: page_name=name, name=f"{file}.txt", file=content, + post_file_id=post_file_id, ) diff --git a/src/templates/post.html b/src/templates/post.html index bdc9ddf..29d69c4 100644 --- a/src/templates/post.html +++ b/src/templates/post.html @@ -1,5 +1,5 @@ {% if config.is_logged() %} -
+
diff --git a/src/utils/misc.py b/src/utils/misc.py index af8c54a..d0c596e 100644 --- a/src/utils/misc.py +++ b/src/utils/misc.py @@ -10,6 +10,11 @@ def post_filename(number: int) -> str: return f"{Config.data_dir}/{number}.txt" +def post_file_id(filename: str) -> int: + """extract the id from the filename""" + return int(filename.split("/")[-1][:-4]) + + def get_posts() -> list[str]: """get posts list""" return [ @@ -49,7 +54,7 @@ def fresh_file_id() -> int: chosen = max(iter(paths), key=os_path.getmtime) # add 1 to the chosen id - filename = int(chosen.split("/")[-1][:-4]) + 1 + filename = post_file_id(chosen) + 1 return filename