Compare commits
No commits in common. "fc8527d69f204ae74a3afea7778ad716fb93b243" and "4b19713c0695d03cba72bdf28e89d7968f75ed06" have entirely different histories.
fc8527d69f
...
4b19713c06
4 changed files with 15 additions and 62 deletions
|
@ -26,10 +26,9 @@ def check_content_exist(key: str) -> bool:
|
|||
return db.exists(key)
|
||||
|
||||
|
||||
def insert_content_db(url_id: str, expiration: int, content: str, secret: str) -> None:
|
||||
def insert_content_db(url_id: str, expiration: int, content: str) -> None:
|
||||
"""
|
||||
|
||||
:param secret: Secret key for delete url
|
||||
:param url_id: key for access to content.
|
||||
:param expiration: Content expiration in second.
|
||||
:param content: Paste content
|
||||
|
@ -37,9 +36,7 @@ def insert_content_db(url_id: str, expiration: int, content: str, secret: str) -
|
|||
"""
|
||||
if not check_content_exist(url_id):
|
||||
db = connect_redis()
|
||||
data = {"content": content, "secret": secret}
|
||||
for key, key_content in data.items():
|
||||
db.hset(url_id, key, key_content)
|
||||
db.set(url_id, content)
|
||||
db.expire(url_id, expiration)
|
||||
else:
|
||||
logging.error(f"Key : {url_id} already exist") # noqa: G004
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
"""Manage view and create paste."""
|
||||
|
||||
from flask import (
|
||||
Blueprint,
|
||||
abort,
|
||||
flash,
|
||||
redirect,
|
||||
render_template,
|
||||
|
@ -12,24 +10,22 @@ from flask import (
|
|||
from werkzeug import Response
|
||||
|
||||
from paste.db import check_content_exist, connect_redis, insert_content_db
|
||||
from paste.utils import generate_id, generate_secret
|
||||
from paste.utils import generate_id
|
||||
|
||||
home = Blueprint("home", __name__, url_prefix="/")
|
||||
|
||||
|
||||
def create_paste(content: str, time: int) -> dict[str, str]:
|
||||
def create_paste(content: str, time:int) -> str:
|
||||
"""
|
||||
Create paste in DB.
|
||||
:param time: Expiration time in second
|
||||
:param content: Content to add in redis.
|
||||
:return: A dict with url_id and delete secret.
|
||||
:return: None.
|
||||
"""
|
||||
secret = generate_secret()
|
||||
url_id = generate_id()
|
||||
while check_content_exist(url_id):
|
||||
url_id = generate_id()
|
||||
insert_content_db(url_id, time, content, secret)
|
||||
return {"url_id": url_id, "secret": secret}
|
||||
insert_content_db(url_id, time, content)
|
||||
return url_id
|
||||
|
||||
|
||||
@home.route("/")
|
||||
|
@ -51,28 +47,11 @@ def create() -> Response:
|
|||
time = request.form.get("time")
|
||||
time = 86400 if time == "" else int(time)
|
||||
|
||||
flash_data = create_paste(content, time)
|
||||
flash(flash_data["url_id"], category="create")
|
||||
flash(flash_data["secret"], category="create")
|
||||
url_id = create_paste(content, time)
|
||||
flash(f"{request.host_url}{url_id}")
|
||||
return redirect(url_for("home.homepage"))
|
||||
|
||||
|
||||
@home.route("/delete/<path:path>")
|
||||
def delete_paste(path: str) -> Response:
|
||||
"""
|
||||
Delete a paste
|
||||
:param path: path fetched from url
|
||||
:return: forbidden (403) or homepage.
|
||||
"""
|
||||
db = connect_redis()
|
||||
secret = request.args.get("secret")
|
||||
if db.hget(path, "secret") == secret:
|
||||
db.delete(path)
|
||||
flash(path, category="delete")
|
||||
return redirect(url_for("home.homepage"))
|
||||
return abort(403)
|
||||
|
||||
|
||||
@home.route("/<path:path>")
|
||||
def get_content(path: str) -> str:
|
||||
"""
|
||||
|
@ -81,9 +60,5 @@ def get_content(path: str) -> str:
|
|||
:return: Paste content.
|
||||
"""
|
||||
db = connect_redis()
|
||||
data = db.hgetall(path)
|
||||
if check_content_exist(path):
|
||||
flash(data["content"])
|
||||
return render_template("content.html.j2")
|
||||
|
||||
return abort(404)
|
||||
flash(db.get(path))
|
||||
return render_template("content.html.j2")
|
||||
|
|
|
@ -1,21 +1,11 @@
|
|||
<form id="paste-content" method="POST" accept-charset="UTF-8" action="{{ url_for('home.create') }}">
|
||||
<textarea name="content" form="paste-content">Enter text here...</textarea>
|
||||
<textarea name="content" form="paste-content">Enter text here...</textarea>
|
||||
<label for="time">Time in second</label>
|
||||
<input id="time" name="time">
|
||||
<input type="submit">
|
||||
</form>
|
||||
{% with messages = get_flashed_messages(category_filter=["create"]) %}
|
||||
{% if messages[0] %}
|
||||
<p>Paste available at : <a href="{{ request.base_url }}{{ messages[0] }}">{{ messages[0] }}</a></p>
|
||||
{% endif %}
|
||||
{% if messages[1] %}
|
||||
<p>You can delete paste with : <a href="{{ request.base_url }}delete/{{ messages[0] }}?secret={{ messages[1] }}">delete</a></p>
|
||||
{% with messages = get_flashed_messages() %}
|
||||
{% if messages %}
|
||||
<p>Paste available at : <a href="{{ messages[0] }}">{{ messages[0] }}</a></p>
|
||||
{% endif %}
|
||||
{% endwith %}
|
||||
|
||||
{% with messages = get_flashed_messages(category_filter=["delete"]) %}
|
||||
{% if messages[0] %}
|
||||
<p>Paste {{ messages[0] }} deleted </p>
|
||||
{% endif %}
|
||||
{% endwith %}
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
# ruff: noqa: S311
|
||||
"""Shared utils function."""
|
||||
import random
|
||||
import secrets
|
||||
import string
|
||||
|
||||
from paste import config
|
||||
|
@ -16,11 +15,3 @@ def generate_id() -> str:
|
|||
random.choices(string.ascii_letters + string.digits,
|
||||
k=config.URL_LENGTH)
|
||||
)
|
||||
|
||||
|
||||
def generate_secret() -> str:
|
||||
"""
|
||||
Generate hex secret for delete paste.
|
||||
:return: The secret.
|
||||
"""
|
||||
return secrets.token_hex()
|
||||
|
|
Reference in a new issue