From 9de9871b40153b83f57f6a812de44b3844e556ab Mon Sep 17 00:00:00 2001 From: Mylloon Date: Fri, 22 Dec 2023 03:07:20 +0100 Subject: [PATCH] feat: post reader --- src/routes/index.py | 4 ++-- src/routes/read.py | 33 +++++++++++++++++++++++++++++---- src/static/css/style.css | 7 ++++++- src/templates/header.html | 24 ++++++++++++++++++++++++ src/templates/index.html | 4 +++- src/templates/post.html | 12 +++++------- src/templates/read.html | 14 ++++++++++---- src/utils/misc.py | 31 ++++++++++++++++++++++++++++++- 8 files changed, 109 insertions(+), 20 deletions(-) diff --git a/src/routes/index.py b/src/routes/index.py index 43f01e2..6e0add2 100644 --- a/src/routes/index.py +++ b/src/routes/index.py @@ -2,7 +2,7 @@ from os import path as os_path from config import Config 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 name = __name__.split(".")[-1] @@ -36,7 +36,7 @@ def new_post() -> Response: # creating the post with open( - f"{Config.data_dir}/{filename}.txt", + post_filename(filename), "w", encoding="utf-8", ) as f: diff --git a/src/routes/read.py b/src/routes/read.py index 8a43487..33afafc 100644 --- a/src/routes/read.py +++ b/src/routes/read.py @@ -1,11 +1,36 @@ 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] router = Blueprint(name, __name__) -@router.route(f"/{name}") -def read() -> str: +@router.route(f"/{name}/") +def read(file: int) -> str: """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}/", 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("/") diff --git a/src/static/css/style.css b/src/static/css/style.css index 3419b86..020cef0 100644 --- a/src/static/css/style.css +++ b/src/static/css/style.css @@ -25,7 +25,7 @@ span#login-form { } /* button stylised as text */ -span#login-form button { +span button { margin-left: 0.4em; background: none; border: none; @@ -81,3 +81,8 @@ ul { li { padding-bottom: 1em; } + +/* Edit button */ +summary { + display: inline; +} diff --git a/src/templates/header.html b/src/templates/header.html index f263c4f..107cf8f 100644 --- a/src/templates/header.html +++ b/src/templates/header.html @@ -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" /> + {% endif %} {% if read_page and config.is_logged() and file %} +
+ + + + + + {% include "post.html" %} +
+ +
+
{% endif %} diff --git a/src/templates/index.html b/src/templates/index.html index 84eebb7..eff77f9 100644 --- a/src/templates/index.html +++ b/src/templates/index.html @@ -6,7 +6,9 @@ {% include "header.html" %} - {% include "post.html" %} + {% if config.is_logged() %} + + {% endif %}

{{ "".join(get_flashed_messages()) | safe }}

diff --git a/src/templates/post.html b/src/templates/post.html index 22cc68b..bdc9ddf 100644 --- a/src/templates/post.html +++ b/src/templates/post.html @@ -1,9 +1,7 @@ {% if config.is_logged() %} - +
+ +
+ +
{% endif %} diff --git a/src/templates/read.html b/src/templates/read.html index ae130a8..813b64d 100644 --- a/src/templates/read.html +++ b/src/templates/read.html @@ -6,11 +6,17 @@ {% include "header.html" %}
- + {% if file %} +

{{ name }}

- - - +

{{ file }}

+ {% else %} +

{{ name }} doesn't exists

+ {% endif %}
+ + diff --git a/src/utils/misc.py b/src/utils/misc.py index 39b1d78..ca6ad52 100644 --- a/src/utils/misc.py +++ b/src/utils/misc.py @@ -1,10 +1,39 @@ from os import listdir from os import path as os_path +from os import remove as os_remove 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 [ 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