Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup and pin MystBin dependencies #45

Merged
merged 4 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading