From 16bbd5ce505b164a7975235d164901e7ce63188a Mon Sep 17 00:00:00 2001 From: Ada Date: Mon, 25 Sep 2023 16:12:36 +0200 Subject: [PATCH] add(backend): handle content url --- paste/app.py | 2 ++ paste/config.py | 2 ++ paste/db.py | 6 ++++-- paste/home.py | 22 ++++++++++++++++------ paste/templates/content.html.j2 | 5 +++++ paste/templates/home.html.j2 | 7 ++++++- 6 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 paste/templates/content.html.j2 diff --git a/paste/app.py b/paste/app.py index f168b73..5c98445 100644 --- a/paste/app.py +++ b/paste/app.py @@ -2,10 +2,12 @@ from flask import Flask +from paste import config from paste.home import home app = Flask(__name__) +app.config.update(TESTING=config.TESTING, SECRET_KEY=config.SECRET_KEY) app.register_blueprint(home) diff --git a/paste/config.py b/paste/config.py index b7280d1..9039716 100644 --- a/paste/config.py +++ b/paste/config.py @@ -6,3 +6,5 @@ REDIS_PORT = getenv("PASTE_REDIS_HOST") or "6379" REDIS_USER = getenv("PASTE_REDIS_USER") or None REDIS_PASSWORD = getenv("PASTE_REDIS_PASSWORD") or None URL_LENGTH = getenv("PASTE_URL_LENGTH") or 4 +TESTING = getenv("PASTE_TESTING") or True +SECRET_KEY = getenv("PASTE_SECRET_KEY") or "verysecure" diff --git a/paste/db.py b/paste/db.py index 24e2dfa..2e6fc78 100644 --- a/paste/db.py +++ b/paste/db.py @@ -11,7 +11,9 @@ def connect_redis() -> Redis: Connect to redis. :return: Redis connection object. """ - return Redis(host=config.REDIS_HOST, port=config.REDIS_PORT) + return Redis( + host=config.REDIS_HOST, port=config.REDIS_PORT, db=0, decode_responses=True + ) def check_content_exist(key: str) -> bool: @@ -32,7 +34,7 @@ def insert_content_db(url_id: str, expiration: int, content: str) -> None: :param content: Paste content :return: None """ - if check_content_exist(url_id): + if not check_content_exist(url_id): db = connect_redis() db.set(url_id, content) db.expire(url_id, expiration) diff --git a/paste/home.py b/paste/home.py index fc4e1a0..574ac3b 100644 --- a/paste/home.py +++ b/paste/home.py @@ -1,5 +1,13 @@ """Manage view and create paste.""" -from flask import Blueprint, redirect, render_template, request, url_for +from flask import ( + Blueprint, + flash, + redirect, + render_template, + request, + url_for, +) +from werkzeug import Response from paste.db import check_content_exist, connect_redis, insert_content_db from paste.utils import generate_id @@ -7,7 +15,7 @@ from paste.utils import generate_id home = Blueprint("home", __name__, url_prefix="/") -def create_paste(content: str) -> None: +def create_paste(content: str) -> str: """ Create paste in DB. :param content: Content to add in redis. @@ -17,6 +25,7 @@ def create_paste(content: str) -> None: while check_content_exist(url_id): url_id = generate_id() insert_content_db(url_id, 3600, content) + return url_id @home.route("/") @@ -29,13 +38,14 @@ def homepage() -> str: @home.route("/create", methods=["POST"]) -def create() -> str: +def create() -> Response: """ Receive POST data for create paste. :return: Redirection to homapage. """ content = request.form.get("content") - create_paste(content) + url_id = create_paste(content) + flash(f"Paste available at {request.host_url}{url_id}") return redirect(url_for("home.homepage")) @@ -47,5 +57,5 @@ def get_content(path: str) -> str: :return: Paste content. """ db = connect_redis() - print(db.get(path)) - return "hello" + flash(db.get(path)) + return render_template("content.html.j2") diff --git a/paste/templates/content.html.j2 b/paste/templates/content.html.j2 new file mode 100644 index 0000000..251fe38 --- /dev/null +++ b/paste/templates/content.html.j2 @@ -0,0 +1,5 @@ +{% with messages = get_flashed_messages() %} + {% if messages %} +

{{ messages[0] }}

+ {% endif %} +{% endwith %} \ No newline at end of file diff --git a/paste/templates/home.html.j2 b/paste/templates/home.html.j2 index af1d88d..1a3e653 100644 --- a/paste/templates/home.html.j2 +++ b/paste/templates/home.html.j2 @@ -1,4 +1,9 @@
-
\ No newline at end of file + +{% with messages = get_flashed_messages() %} + {% if messages %} +

{{ messages[0] }}

+ {% endif %} +{% endwith %}