Skip to content

Commit

Permalink
Cleanup and pin MystBin dependencies (#45)
Browse files Browse the repository at this point in the history
* pin requirements and remove unneeded deps

* create a util for natural_time to negate the humanize dep

* resolve dt math

* Update core/utils.py

Co-authored-by: Lilly Rose Berner <[email protected]>

---------

Co-authored-by: Lilly Rose Berner <[email protected]>
  • Loading branch information
AbstractUmbra and LostLuma authored May 22, 2024
1 parent 5cb705a commit e2f92b8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
35 changes: 35 additions & 0 deletions core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import base64
import binascii
import datetime
import json
import re
import secrets
Expand Down Expand Up @@ -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 * 24)
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(delta=ret)
16 changes: 7 additions & 9 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
5 changes: 2 additions & 3 deletions views/htmx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -155,7 +154,7 @@ async def paste(self, request: starlette_plus.Request) -> starlette_plus.Respons
<div class="identifierHeader">
<div class="identifierHeaderLeft">
<a href="{url}">/{identifier}</a>
<span>Created {humanize.naturaltime(created_delta)}...</span>
<span>Created {natural_time(created_delta)}...</span>
</div>
{security_html}
<div class="identifierHeaderSection">
Expand Down

0 comments on commit e2f92b8

Please sign in to comment.