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

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') }}">
<input name="content">
<button>Create</button>
<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() %}
{% if messages %}
<p>{{ messages[0] }}</p>
<p>Paste available at : <a href="{{ messages[0] }}">{{ messages[0] }}</a></p>
{% endif %}
{% endwith %}

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

@ -1,3 +1,4 @@
# ruff: noqa: S311
"""Shared utils function."""
import random
import string
@ -11,5 +12,6 @@ def generate_id() -> str:
:return: Url id.
"""
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