fix: update post

This commit is contained in:
Mylloon 2023-12-22 03:47:20 +01:00
parent c8291f415b
commit 87ca9ced16
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 16 additions and 7 deletions

View file

@ -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("/<post_id>", 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
if post_id is None:
filename = fresh_file_id()
else:
filename = post_id
# create/update the post
update = create_post(filename, content)

View file

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

View file

@ -1,5 +1,5 @@
{% if config.is_logged() %}
<form action="/" method="post">
<form action="/{% if name %}{{ post_file_id(name) }}{% endif %}" method="post">
<textarea name="p">{{ file }}</textarea>
<br />
<button type="submit">send</button>

View file

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