64 lines
1.4 KiB
Python
Executable file
64 lines
1.4 KiB
Python
Executable file
"""Manage view and create paste."""
|
|
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
|
|
|
|
home = Blueprint("home", __name__, url_prefix="/")
|
|
|
|
|
|
def create_paste(content: str, time:int) -> str:
|
|
"""
|
|
Create paste in DB.
|
|
:param content: Content to add in redis.
|
|
:return: None.
|
|
"""
|
|
url_id = generate_id()
|
|
while check_content_exist(url_id):
|
|
url_id = generate_id()
|
|
insert_content_db(url_id, time, content)
|
|
return url_id
|
|
|
|
|
|
@home.route("/")
|
|
def homepage() -> str:
|
|
"""
|
|
Homepage.
|
|
:return: Homepage.
|
|
"""
|
|
return render_template("home.html.j2")
|
|
|
|
|
|
@home.route("/create", methods=["POST"])
|
|
def create() -> Response:
|
|
"""
|
|
Receive POST data for create paste.
|
|
:return: Redirection to homapage.
|
|
"""
|
|
content = request.form.get("content")
|
|
time = request.form.get("time")
|
|
time = 86400 if time == "" else int(time)
|
|
|
|
url_id = create_paste(content, time)
|
|
flash(f"{request.host_url}{url_id}")
|
|
return redirect(url_for("home.homepage"))
|
|
|
|
|
|
@home.route("/<path:path>")
|
|
def get_content(path: str) -> str:
|
|
"""
|
|
Return paste for asked url.
|
|
:param path: Requested path.
|
|
:return: Paste content.
|
|
"""
|
|
db = connect_redis()
|
|
flash(db.get(path))
|
|
return render_template("content.html.j2")
|