feat: post reader

This commit is contained in:
Mylloon 2023-12-22 03:07:20 +01:00
parent 4a216d7a81
commit 9de9871b40
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
8 changed files with 109 additions and 20 deletions

View file

@ -2,7 +2,7 @@ from os import path as os_path
from config import Config from config import Config
from flask import Blueprint, flash, redirect, render_template, request from flask import Blueprint, flash, redirect, render_template, request
from utils.misc import get_posts from utils.misc import get_posts, post_filename
from werkzeug import Response from werkzeug import Response
name = __name__.split(".")[-1] name = __name__.split(".")[-1]
@ -36,7 +36,7 @@ def new_post() -> Response:
# creating the post # creating the post
with open( with open(
f"{Config.data_dir}/{filename}.txt", post_filename(filename),
"w", "w",
encoding="utf-8", encoding="utf-8",
) as f: ) as f:

View file

@ -1,11 +1,36 @@
from config import Config from config import Config
from flask import Blueprint, render_template from flask import Blueprint, flash, redirect, render_template
from utils.misc import delete_post, get_post, post_filename
from werkzeug import Response
name = __name__.split(".")[-1] name = __name__.split(".")[-1]
router = Blueprint(name, __name__) router = Blueprint(name, __name__)
@router.route(f"/{name}") @router.route(f"/{name}/<int:file>")
def read() -> str: def read(file: int) -> str:
"""read page""" """read page"""
return render_template("read.html", config=Config, page_name=name) filename = post_filename(file)
content = get_post(filename)
return render_template(
"read.html",
config=Config,
read_page=True,
page_name=name,
name=f"{file}.txt",
file=content,
)
@router.route(f"/{name}/<int:file>", methods=["POST"])
def remove_post(file: int) -> Response:
"""remove a post"""
if Config.is_logged():
filename = post_filename(file)
if delete_post(filename):
flash(f"{filename} deleted.")
else:
flash(f"{filename} doesn't exists")
return redirect("/")

View file

@ -25,7 +25,7 @@ span#login-form {
} }
/* button stylised as text */ /* button stylised as text */
span#login-form button { span button {
margin-left: 0.4em; margin-left: 0.4em;
background: none; background: none;
border: none; border: none;
@ -81,3 +81,8 @@ ul {
li { li {
padding-bottom: 1em; padding-bottom: 1em;
} }
/* Edit button */
summary {
display: inline;
}

View file

@ -9,5 +9,29 @@
d="M5 21a7 7 0 1 1 14 0M16 7a4 4 0 1 1-8 0 4 4 0 0 1 8 0Z" d="M5 21a7 7 0 1 1 14 0M16 7a4 4 0 1 1-8 0 4 4 0 0 1 8 0Z"
/></svg /></svg
></a> ></a>
{% endif %} {% if read_page and config.is_logged() and file %}
<details>
<summary>
<svg width="30px" fill="none" viewBox="0 0 24 24">
<path
stroke="#000"
d="M3.8 12.963 2 18l4.8-.63L18.11 6.58a2.612 2.612 0 0 0-3.601-3.785L3.8 12.963z"
/>
</svg>
</summary>
{% include "post.html" %}
</details>
<span
><form method="post">
<button type="submit">
<svg width="30px" fill="none" viewBox="0 0 24 24">
<path
stroke="#000"
d="m18 6-.8 12.013c-.071 1.052-.106 1.578-.333 1.977a2 2 0 0 1-.866.81c-.413.2-.94.2-1.995.2H9.994c-1.055 0-1.582 0-1.995-.2a2 2 0 0 1-.866-.81c-.227-.399-.262-.925-.332-1.977L6 6M4 6h16m-4 0-.27-.812c-.263-.787-.394-1.18-.637-1.471a2 2 0 0 0-.803-.578C13.938 3 13.524 3 12.694 3h-1.388c-.829 0-1.244 0-1.596.139a2 2 0 0 0-.803.578c-.243.29-.374.684-.636 1.471L8 6m6 4v7m-4-7v7"
/>
</svg>
</button></form
></span>
{% endif %} {% endif %}
</header> </header>

View file

@ -6,7 +6,9 @@
{% include "header.html" %} {% include "header.html" %}
<body> <body>
{% include "post.html" %} {% if config.is_logged() %}
<aside>{% include "post.html" %}</aside>
{% endif %}
<main> <main>
<p>{{ "".join(get_flashed_messages()) | safe }}</p> <p>{{ "".join(get_flashed_messages()) | safe }}</p>

View file

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

View file

@ -6,11 +6,17 @@
{% include "header.html" %} {% include "header.html" %}
<body> <body>
<main> <main>
<!-- TODO: show a specific post --> {% if file %}
<h3>{{ name }}</h3>
<!-- TODO: button to edit the post if logged --> <p>{{ file }}</p>
{% else %}
<!-- TODO: button to delete the post if logged --> <p>{{ name }} doesn't exists</p>
{% endif %}
</main> </main>
<footer>
<a href="/">back to index</a>
</footer>
</body> </body>
</html> </html>

View file

@ -1,10 +1,39 @@
from os import listdir from os import listdir
from os import path as os_path from os import path as os_path
from os import remove as os_remove
from config import Config from config import Config
def get_posts(): def post_filename(number: int):
"""get filename of post"""
return f"{Config.data_dir}/{number}.txt"
def get_posts() -> list[str]:
"""get posts list"""
return [ return [
os_path.join(Config.data_dir, basename) for basename in listdir(Config.data_dir) os_path.join(Config.data_dir, basename) for basename in listdir(Config.data_dir)
] ]
def get_post(filename: str) -> str | None:
"""get a post"""
try:
open(filename)
except FileNotFoundError:
return None
else:
with open(filename, "r") as reader:
return reader.read()
def delete_post(filename: str) -> bool:
"""delete a post"""
try:
open(filename)
except FileNotFoundError:
return False
else:
os_remove(filename)
return True