From e01f144d93c16a4ecb5053829d018f137a827aa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20N=C3=B8rgaard?= Date: Wed, 22 May 2024 13:38:45 +0100 Subject: [PATCH 1/4] pin requirements and remove unneeded deps --- requirements.txt | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/requirements.txt b/requirements.txt index 0b255d8..e9625dd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,9 +1,7 @@ -git+https://github.com/PythonistaGuild/StarlettePlus.git -uvicorn -asyncpg -asyncpg-stubs -bleach -humanize -python-multipart -pyyaml -aiohttp +starlette_plus @ git+https://github.com/PythonistaGuild/StarlettePlus.git@f21169a +uvicorn==0.29.0 +asyncpg==0.29.0 +asyncpg-stubs==0.29.1 +bleach==6.1.0 +python-multipart==0.0.9 +aiohttp==3.9.5 From 4f2ee75f23533685ee901a63a7fb9b2e77371953 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20N=C3=B8rgaard?= Date: Wed, 22 May 2024 13:39:13 +0100 Subject: [PATCH 2/4] create a util for natural_time to negate the humanize dep --- core/utils.py | 35 +++++++++++++++++++++++++++++++++++ views/htmx.py | 5 ++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/core/utils.py b/core/utils.py index 5552f2e..a1023a5 100644 --- a/core/utils.py +++ b/core/utils.py @@ -20,6 +20,7 @@ import base64 import binascii +import datetime import json import re import secrets @@ -98,3 +99,37 @@ def validate_discord_token(token: str) -> bool: return False else: return True + + +def natural_time( + td: datetime.timedelta, + /, + *, + source: datetime.datetime | None = None, +) -> str: + now = source or datetime.datetime.now(datetime.UTC) + + then = now + td + future = then > now + + ago = "{delta} from now" if future else "{delta} ago" + + seconds = round(td.total_seconds()) + weeks, seconds = divmod(seconds, 60 * 60 * 24 * 7) + days, seconds = divmod(seconds, 60 * 60 * 7) + hours, seconds = divmod(seconds, 60 * 60) + minutes, seconds = divmod(seconds, 60) + + ret = "" + + if weeks: + ret += f"{weeks} weeks," + if days: + ret += f"{days} days," + if hours: + ret += f"{hours} hours," + if minutes: + ret += f"{minutes} minutes and" + ret += f"{seconds} seconds" + + return ago.format(ret) diff --git a/views/htmx.py b/views/htmx.py index b3a68a0..e752ce9 100644 --- a/views/htmx.py +++ b/views/htmx.py @@ -26,11 +26,10 @@ import asyncpg import bleach -import humanize import starlette_plus from core import CONFIG -from core.utils import validate_paste +from core.utils import natural_time, validate_paste if TYPE_CHECKING: @@ -155,7 +154,7 @@ async def paste(self, request: starlette_plus.Request) -> starlette_plus.Respons
/{identifier} - Created {humanize.naturaltime(created_delta)}... + Created {natural_time(created_delta)}...
{security_html}
From 675496e758d1cd0c428c67e237da5ed0e1af3e93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20N=C3=B8rgaard?= Date: Wed, 22 May 2024 13:58:26 +0100 Subject: [PATCH 3/4] resolve dt math --- core/utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/utils.py b/core/utils.py index a1023a5..73098dc 100644 --- a/core/utils.py +++ b/core/utils.py @@ -109,7 +109,7 @@ def natural_time( ) -> str: now = source or datetime.datetime.now(datetime.UTC) - then = now + td + then = now - td future = then > now ago = "{delta} from now" if future else "{delta} ago" @@ -129,7 +129,7 @@ def natural_time( if hours: ret += f"{hours} hours," if minutes: - ret += f"{minutes} minutes and" + ret += f"{minutes} minutes and " ret += f"{seconds} seconds" - return ago.format(ret) + return ago.format(delta=ret) From 4c95411e9d71d53dc6fd66dbcea11c36e739c04f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20N=C3=B8rgaard?= Date: Wed, 22 May 2024 14:41:30 +0100 Subject: [PATCH 4/4] Update core/utils.py Co-authored-by: Lilly Rose Berner --- core/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/utils.py b/core/utils.py index 73098dc..5dcde98 100644 --- a/core/utils.py +++ b/core/utils.py @@ -116,7 +116,7 @@ def natural_time( seconds = round(td.total_seconds()) weeks, seconds = divmod(seconds, 60 * 60 * 24 * 7) - days, seconds = divmod(seconds, 60 * 60 * 7) + days, seconds = divmod(seconds, 60 * 60 * 24) hours, seconds = divmod(seconds, 60 * 60) minutes, seconds = divmod(seconds, 60)