feat: initial shared library extraction
Contains shared infrastructure for all coop services: - shared/ (factory, urls, user_loader, context, internal_api, jinja_setup) - models/ (User, Order, Calendar, Ticket, Product, Ghost CMS) - db/ (SQLAlchemy async session, base) - suma_browser/app/ (csrf, middleware, errors, authz, redis_cacher, payments, filters, utils) - suma_browser/templates/ (shared base layouts, macros, error pages) - static/ (CSS, JS, fonts, images) - alembic/ (database migrations) - config/ (app-config.yaml) - editor/ (Lexical editor Node.js build) - requirements.txt Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
12
suma_browser/app/utils/__init__.py
Normal file
12
suma_browser/app/utils/__init__.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from .parse import (
|
||||
parse_time,
|
||||
parse_cost,
|
||||
parse_dt
|
||||
)
|
||||
from .utils import (
|
||||
current_route_relative_path,
|
||||
current_url_without_page,
|
||||
vary,
|
||||
)
|
||||
|
||||
from .utc import utcnow
|
||||
46
suma_browser/app/utils/htmx.py
Normal file
46
suma_browser/app/utils/htmx.py
Normal file
@@ -0,0 +1,46 @@
|
||||
"""HTMX utilities for detecting and handling HTMX requests."""
|
||||
|
||||
from quart import request
|
||||
|
||||
|
||||
def is_htmx_request() -> bool:
|
||||
"""
|
||||
Check if the current request is an HTMX request.
|
||||
|
||||
Returns:
|
||||
bool: True if HX-Request header is present and true
|
||||
"""
|
||||
return request.headers.get("HX-Request", "").lower() == "true"
|
||||
|
||||
|
||||
def get_htmx_target() -> str | None:
|
||||
"""
|
||||
Get the target element ID from HTMX request headers.
|
||||
|
||||
Returns:
|
||||
str | None: Target element ID or None
|
||||
"""
|
||||
return request.headers.get("HX-Target")
|
||||
|
||||
|
||||
def get_htmx_trigger() -> str | None:
|
||||
"""
|
||||
Get the trigger element ID from HTMX request headers.
|
||||
|
||||
Returns:
|
||||
str | None: Trigger element ID or None
|
||||
"""
|
||||
return request.headers.get("HX-Trigger")
|
||||
|
||||
|
||||
def should_return_fragment() -> bool:
|
||||
"""
|
||||
Determine if we should return a fragment vs full page.
|
||||
|
||||
For HTMX requests, return fragment.
|
||||
For normal requests, return full page.
|
||||
|
||||
Returns:
|
||||
bool: True if fragment should be returned
|
||||
"""
|
||||
return is_htmx_request()
|
||||
36
suma_browser/app/utils/parse.py
Normal file
36
suma_browser/app/utils/parse.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from datetime import datetime, timezone
|
||||
|
||||
def parse_time(val: str | None):
|
||||
if not val:
|
||||
return None
|
||||
try:
|
||||
h,m = val.split(':', 1)
|
||||
from datetime import time
|
||||
return time(int(h), int(m))
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def parse_cost(val: str | None):
|
||||
if not val:
|
||||
return None
|
||||
try:
|
||||
return float(val)
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
if not val:
|
||||
return None
|
||||
dt = datetime.fromisoformat(val)
|
||||
# make TZ-aware (assume local if naive; convert to UTC)
|
||||
if dt.tzinfo is None:
|
||||
dt = dt.replace(tzinfo=timezone.utc)
|
||||
return dt
|
||||
|
||||
def parse_dt(val: str | None) -> datetime | None:
|
||||
if not val:
|
||||
return None
|
||||
dt = datetime.fromisoformat(val)
|
||||
if dt.tzinfo is None:
|
||||
dt = dt.replace(tzinfo=timezone.utc)
|
||||
return dt
|
||||
|
||||
6
suma_browser/app/utils/utc.py
Normal file
6
suma_browser/app/utils/utc.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from datetime import datetime, timezone
|
||||
|
||||
|
||||
def utcnow() -> datetime:
|
||||
return datetime.now(timezone.utc)
|
||||
|
||||
51
suma_browser/app/utils/utils.py
Normal file
51
suma_browser/app/utils/utils.py
Normal file
@@ -0,0 +1,51 @@
|
||||
from quart import (
|
||||
Response,
|
||||
request,
|
||||
g,
|
||||
)
|
||||
from utils import host_url
|
||||
from urllib.parse import urlencode
|
||||
|
||||
def current_route_relative_path() -> str:
|
||||
"""
|
||||
Returns the current request path relative to the app's mount point (script_root).
|
||||
"""
|
||||
|
||||
(request.script_root or "").rstrip("/")
|
||||
path = request.path # excludes query string
|
||||
|
||||
|
||||
|
||||
if g.root and path.startswith(f"/{g.root}"):
|
||||
rel = path[len(g.root+1):]
|
||||
return rel if rel.startswith("/") else "/" + rel
|
||||
return path # app at /
|
||||
|
||||
|
||||
def current_url_without_page() -> str:
|
||||
"""
|
||||
Build current URL (host+path+qs) but with ?page= removed.
|
||||
Used for Hx-Push-Url.
|
||||
"""
|
||||
base = host_url(current_route_relative_path())
|
||||
|
||||
params = request.args.to_dict(flat=False) # keep multivals
|
||||
params.pop("page", None)
|
||||
qs = urlencode(params, doseq=True)
|
||||
|
||||
return f"{base}?{qs}" if qs else base
|
||||
|
||||
def vary(resp: Response) -> Response:
|
||||
"""
|
||||
Ensure caches/CDNs vary on HX headers so htmx/non-htmx versions don't get mixed.
|
||||
"""
|
||||
v = resp.headers.get("Vary", "")
|
||||
parts = [p.strip() for p in v.split(",") if p.strip()]
|
||||
for h in ("HX-Request", "X-Origin"):
|
||||
if h not in parts:
|
||||
parts.append(h)
|
||||
if parts:
|
||||
resp.headers["Vary"] = ", ".join(parts)
|
||||
return resp
|
||||
|
||||
|
||||
Reference in New Issue
Block a user