feat: remove unused image retrieval endpoint and add human-readable time formatting filters
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
"""Flask frontend application."""
|
||||
import functools
|
||||
from datetime import datetime, timezone
|
||||
|
||||
import httpx
|
||||
from flask import (
|
||||
@@ -24,6 +25,42 @@ app.config.from_object(Config)
|
||||
# Helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@app.template_filter("fromisoformat")
|
||||
def from_iso_format(s: str) -> datetime:
|
||||
"""Convert ISO 8601 string to datetime object."""
|
||||
return datetime.fromisoformat(s)
|
||||
|
||||
|
||||
@app.template_filter("humantime")
|
||||
def human_time(dt: datetime) -> str:
|
||||
"""Format a datetime object into a human-readable relative time."""
|
||||
now = datetime.now(timezone.utc)
|
||||
# Ensure dt is aware for comparison
|
||||
if dt.tzinfo is None:
|
||||
dt = dt.replace(tzinfo=timezone.utc)
|
||||
|
||||
diff = now - dt
|
||||
seconds = diff.total_seconds()
|
||||
|
||||
if seconds < 60:
|
||||
return "just now"
|
||||
elif seconds < 3600:
|
||||
minutes = int(seconds / 60)
|
||||
return f"{minutes} minute{'s' if minutes > 1 else ''} ago"
|
||||
elif seconds < 86400:
|
||||
hours = int(seconds / 3600)
|
||||
return f"{hours} hour{'s' if hours > 1 else ''} ago"
|
||||
elif seconds < 2592000:
|
||||
days = int(seconds / 86400)
|
||||
return f"{days} day{'s' if days > 1 else ''} ago"
|
||||
elif seconds < 31536000:
|
||||
months = int(seconds / 2592000)
|
||||
return f"{months} month{'s' if months > 1 else ''} ago"
|
||||
else:
|
||||
years = int(seconds / 31536000)
|
||||
return f"{years} year{'s' if years > 1 else ''} ago"
|
||||
|
||||
|
||||
def _backend(path: str) -> str:
|
||||
return f"{app.config['BACKEND_URL']}{path}"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user