From 236fd36219d7b897cd41a21226d89d56e71358dd Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 21 Dec 2023 21:17:56 +0100 Subject: [PATCH] draft: modifications * automatically load all routes * local configuration * rename public to static directory * wip: add basic login page * route based on filename * page name based on route * add login button * add dummy icon --- src/app.py | 16 ++++++++++------ src/config.py | 22 ++++++++++++++++++++++ src/public/css/style.css | 3 --- src/routes/index.py | 8 +++++--- src/routes/login.py | 22 ++++++++++++++++++++++ src/routes/read.py | 8 +++++--- src/static/css/style.css | 15 +++++++++++++++ src/templates/head.html | 12 +++++++++--- src/templates/header.html | 13 ++++++++++++- src/templates/index.html | 1 - src/templates/login.html | 15 +++++++++++++++ src/templates/read.html | 1 - 12 files changed, 115 insertions(+), 21 deletions(-) create mode 100644 src/config.py delete mode 100644 src/public/css/style.css create mode 100644 src/routes/login.py create mode 100644 src/static/css/style.css create mode 100644 src/templates/login.html diff --git a/src/app.py b/src/app.py index d72c7c1..9352b07 100644 --- a/src/app.py +++ b/src/app.py @@ -1,7 +1,11 @@ -from flask import Flask -from routes.index import router as index -from routes.read import router as read +from glob import glob -app = Flask(__name__, static_url_path="/", static_folder="public") -app.register_blueprint(index, url_prefix="/") -app.register_blueprint(read, url_prefix="/read") +from flask import Flask + +app = Flask(__name__, static_url_path="/") + +# Import all routes +for file in glob("src/routes/*.py"): + module = file.replace("/", ".").split(".")[-2] + exec(f"from routes.{module} import router as {module}") + exec(f"app.register_blueprint({module})") diff --git a/src/config.py b/src/config.py new file mode 100644 index 0000000..b882805 --- /dev/null +++ b/src/config.py @@ -0,0 +1,22 @@ +from os import environ as envar + +VAR_USERNAME = "TD_USERNAME" + + +class User: + """user informations""" + + name = envar[VAR_USERNAME] if VAR_USERNAME in envar else None + + +class Config: + """app configuration""" + + # App name + name = "txtdiary" + + # App description + description = "Personal diary page" + + # User + user = User diff --git a/src/public/css/style.css b/src/public/css/style.css deleted file mode 100644 index f0437fa..0000000 --- a/src/public/css/style.css +++ /dev/null @@ -1,3 +0,0 @@ -html { - font-family: "Courier New", Courier, monospace; -} diff --git a/src/routes/index.py b/src/routes/index.py index 694a71f..fcd2106 100644 --- a/src/routes/index.py +++ b/src/routes/index.py @@ -2,12 +2,14 @@ Index page with a list of posts. """ +from config import Config from flask import Blueprint, render_template -router = Blueprint("index", __name__) +name = __name__.split(".")[-1] +router = Blueprint(name, __name__) -@router.route("") +@router.route("/") def index() -> str: """Index page""" - return render_template("index.html") + return render_template("index.html", config=Config, page_name=name) diff --git a/src/routes/login.py b/src/routes/login.py new file mode 100644 index 0000000..419519d --- /dev/null +++ b/src/routes/login.py @@ -0,0 +1,22 @@ +""" +Login page to an account. +""" + +from config import Config +from flask import Blueprint, jsonify, render_template +from werkzeug import Response + +name = __name__.split(".")[-1] +router = Blueprint(name, __name__) + + +@router.route(f"/{name}") +def login() -> str: + """Login page""" + return render_template("login.html", config=Config, page_name=name) + + +@router.route(f"/{name}", methods=["POST"]) +def check() -> Response: + """Login logic""" + return jsonify("{}") diff --git a/src/routes/read.py b/src/routes/read.py index 2303b6a..b8f39b3 100644 --- a/src/routes/read.py +++ b/src/routes/read.py @@ -2,12 +2,14 @@ Read a specific post. """ +from config import Config from flask import Blueprint, render_template -router = Blueprint("read", __name__) +name = __name__.split(".")[-1] +router = Blueprint(name, __name__) -@router.route("") +@router.route(f"/{name}") def read() -> str: """Read page""" - return render_template("read.html") + return render_template("read.html", config=Config, page_name=name) diff --git a/src/static/css/style.css b/src/static/css/style.css new file mode 100644 index 0000000..9f91f91 --- /dev/null +++ b/src/static/css/style.css @@ -0,0 +1,15 @@ +html { + font-family: "Courier New", Courier, monospace; +} + +/* Align title with button */ +header { + display: flex; + align-items: center; +} + +/* SVG centered on the line */ +header svg { + margin-left: 0.5em; + vertical-align: middle; +} diff --git a/src/templates/head.html b/src/templates/head.html index 6cf4630..973973f 100644 --- a/src/templates/head.html +++ b/src/templates/head.html @@ -2,11 +2,17 @@ - + + - + + - + + + + +{{ page_name }} - {{ config.name }} diff --git a/src/templates/header.html b/src/templates/header.html index f3a8152..6a92f85 100644 --- a/src/templates/header.html +++ b/src/templates/header.html @@ -1 +1,12 @@ -
+
+

txtdiary{{ " of " + config.user.name if config.user.name else "" }}

+
+ + + +
+
diff --git a/src/templates/index.html b/src/templates/index.html index 1b35427..51e1122 100644 --- a/src/templates/index.html +++ b/src/templates/index.html @@ -2,7 +2,6 @@ {% include "head.html" %} - Index {% include "header.html" %} diff --git a/src/templates/login.html b/src/templates/login.html new file mode 100644 index 0000000..059d4ed --- /dev/null +++ b/src/templates/login.html @@ -0,0 +1,15 @@ + + + + {% include "head.html" %} + + {% include "header.html" %} + +

Welcome to the login page

+ + +
+ +
+ + diff --git a/src/templates/read.html b/src/templates/read.html index b145366..033747d 100644 --- a/src/templates/read.html +++ b/src/templates/read.html @@ -2,7 +2,6 @@ {% include "head.html" %} - Read {% include "header.html" %}