add(front): expiration time field

This commit is contained in:
Ada 2023-09-26 11:32:42 +02:00
parent 316eeaa9ba
commit cfa77463ea
11 changed files with 17 additions and 9 deletions

0
LICENSE Normal file → Executable file
View file

1
paste/__init__.py Normal file → Executable file
View file

@ -0,0 +1 @@
"""Paste package."""

0
paste/app.py Normal file → Executable file
View file

0
paste/config.py Normal file → Executable file
View file

2
paste/db.py Normal file → Executable file
View file

@ -39,4 +39,4 @@ def insert_content_db(url_id: str, expiration: int, content: str) -> None:
db.set(url_id, content) db.set(url_id, content)
db.expire(url_id, expiration) db.expire(url_id, expiration)
else: else:
logging.error(f"Key : {url_id} already exist") logging.error(f"Key : {url_id} already exist") # noqa: G004

11
paste/home.py Normal file → Executable file
View file

@ -15,7 +15,7 @@ from paste.utils import generate_id
home = Blueprint("home", __name__, url_prefix="/") home = Blueprint("home", __name__, url_prefix="/")
def create_paste(content: str) -> str: def create_paste(content: str, time:int) -> str:
""" """
Create paste in DB. Create paste in DB.
:param content: Content to add in redis. :param content: Content to add in redis.
@ -24,7 +24,7 @@ def create_paste(content: str) -> str:
url_id = generate_id() url_id = generate_id()
while check_content_exist(url_id): while check_content_exist(url_id):
url_id = generate_id() url_id = generate_id()
insert_content_db(url_id, 3600, content) insert_content_db(url_id, time, content)
return url_id return url_id
@ -44,8 +44,11 @@ def create() -> Response:
:return: Redirection to homapage. :return: Redirection to homapage.
""" """
content = request.form.get("content") content = request.form.get("content")
url_id = create_paste(content) time = request.form.get("time")
flash(f"Paste available at {request.host_url}{url_id}") 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")) return redirect(url_for("home.homepage"))

0
paste/templates/content.html.j2 Normal file → Executable file
View file

8
paste/templates/home.html.j2 Normal file → Executable file
View file

@ -1,9 +1,11 @@
<form id="paste-content" method="POST" accept-charset="UTF-8" action="{{ url_for('home.create') }}"> <form id="paste-content" method="POST" accept-charset="UTF-8" action="{{ url_for('home.create') }}">
<input name="content"> <textarea name="content" form="paste-content">Enter text here...</textarea>
<button>Create</button> <label for="time">Time in second</label>
<input id="time" name="time">
<input type="submit">
</form> </form>
{% with messages = get_flashed_messages() %} {% with messages = get_flashed_messages() %}
{% if messages %} {% if messages %}
<p>{{ messages[0] }}</p> <p>Paste available at : <a href="{{ messages[0] }}">{{ messages[0] }}</a></p>
{% endif %} {% endif %}
{% endwith %} {% endwith %}

4
paste/utils.py Normal file → Executable file
View file

@ -1,3 +1,4 @@
# ruff: noqa: S311
"""Shared utils function.""" """Shared utils function."""
import random import random
import string import string
@ -11,5 +12,6 @@ def generate_id() -> str:
:return: Url id. :return: Url id.
""" """
return "".join( return "".join(
random.choices(string.ascii_letters + string.digits, k=config.URL_LENGTH) random.choices(string.ascii_letters + string.digits,
k=config.URL_LENGTH)
) )

0
pyproject.toml Normal file → Executable file
View file

0
requirements.txt Normal file → Executable file
View file