add(backend): handle content url

This commit is contained in:
Ada 2023-09-25 16:12:36 +02:00
parent cfd15eafd1
commit 16bbd5ce50
6 changed files with 35 additions and 9 deletions

View file

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

View file

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

View file

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

View file

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

View file

@ -0,0 +1,5 @@
{% with messages = get_flashed_messages() %}
{% if messages %}
<p>{{ messages[0] }}</p>
{% endif %}
{% endwith %}

View file

@ -2,3 +2,8 @@
<input name="content">
<button>Create</button>
</form>
{% with messages = get_flashed_messages() %}
{% if messages %}
<p>{{ messages[0] }}</p>
{% endif %}
{% endwith %}