Add global + page-scoped market listings with infinite scroll
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 1m4s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 1m4s
- New all_markets blueprint at / with paginated grid and HTMX infinite scroll - New page_markets blueprint at /<slug>/ for page-scoped market listing - list_marketplaces service method (via shared submodule update) - Updated slug preprocessor to handle both /<slug>/ and /<page_slug>/<market_slug>/ - Removed inline markets_listing() route (replaced by all_markets blueprint) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
70
app.py
70
app.py
@@ -3,14 +3,14 @@ import path_setup # noqa: F401 # adds shared/ to sys.path
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from quart import g, abort, render_template, make_response
|
||||
from quart import g, abort
|
||||
from jinja2 import FileSystemLoader, ChoiceLoader
|
||||
from sqlalchemy import select
|
||||
|
||||
from shared.infrastructure.factory import create_base_app
|
||||
from shared.config import config
|
||||
|
||||
from bp import register_market_bp
|
||||
from bp import register_market_bp, register_all_markets, register_page_markets
|
||||
|
||||
|
||||
async def market_context() -> dict:
|
||||
@@ -77,6 +77,18 @@ def create_app() -> "Quart":
|
||||
app.jinja_loader,
|
||||
])
|
||||
|
||||
# All markets: / — global view across all pages
|
||||
app.register_blueprint(
|
||||
register_all_markets(),
|
||||
url_prefix="/",
|
||||
)
|
||||
|
||||
# Page markets: /<slug>/ — markets for a single page
|
||||
app.register_blueprint(
|
||||
register_page_markets(),
|
||||
url_prefix="/<slug>",
|
||||
)
|
||||
|
||||
# Market blueprint nested under post slug: /<page_slug>/<market_slug>/
|
||||
app.register_blueprint(
|
||||
register_market_bp(
|
||||
@@ -86,10 +98,14 @@ def create_app() -> "Quart":
|
||||
url_prefix="/<page_slug>/<market_slug>",
|
||||
)
|
||||
|
||||
# --- Auto-inject page_slug and market_slug into url_for() calls ---
|
||||
# --- Auto-inject slugs into url_for() calls ---
|
||||
@app.url_value_preprocessor
|
||||
def pull_slugs(endpoint, values):
|
||||
if values:
|
||||
# page_markets blueprint uses "slug"
|
||||
if "slug" in values:
|
||||
g.post_slug = values.pop("slug")
|
||||
# market blueprint uses "page_slug" / "market_slug"
|
||||
if "page_slug" in values:
|
||||
g.post_slug = values.pop("page_slug")
|
||||
if "market_slug" in values:
|
||||
@@ -97,18 +113,22 @@ def create_app() -> "Quart":
|
||||
|
||||
@app.url_defaults
|
||||
def inject_slugs(endpoint, values):
|
||||
for attr, param in [("post_slug", "page_slug"), ("market_slug", "market_slug")]:
|
||||
val = g.get(attr)
|
||||
if val and param not in values:
|
||||
if app.url_map.is_endpoint_expecting(endpoint, param):
|
||||
values[param] = val
|
||||
slug = g.get("post_slug")
|
||||
if slug:
|
||||
for param in ("slug", "page_slug"):
|
||||
if param not in values and app.url_map.is_endpoint_expecting(endpoint, param):
|
||||
values[param] = slug
|
||||
market_slug = g.get("market_slug")
|
||||
if market_slug and "market_slug" not in values:
|
||||
if app.url_map.is_endpoint_expecting(endpoint, "market_slug"):
|
||||
values["market_slug"] = market_slug
|
||||
|
||||
# --- Load post and market data ---
|
||||
@app.before_request
|
||||
async def hydrate_market():
|
||||
post_slug = getattr(g, "post_slug", None)
|
||||
market_slug = getattr(g, "market_slug", None)
|
||||
if not post_slug or not market_slug:
|
||||
if not post_slug:
|
||||
return
|
||||
|
||||
# Load post by slug via blog service
|
||||
@@ -129,7 +149,10 @@ def create_app() -> "Quart":
|
||||
},
|
||||
}
|
||||
|
||||
# Load market scoped to post (container pattern)
|
||||
# Only load market when market_slug is present (/<page_slug>/<market_slug>/)
|
||||
if not market_slug:
|
||||
return
|
||||
|
||||
market = (
|
||||
await g.s.execute(
|
||||
select(MarketPlace).where(
|
||||
@@ -151,33 +174,6 @@ def create_app() -> "Quart":
|
||||
return {}
|
||||
return {**post_data}
|
||||
|
||||
# --- Root route: market listing ---
|
||||
@app.get("/")
|
||||
async def markets_listing():
|
||||
result = await g.s.execute(
|
||||
select(MarketPlace)
|
||||
.where(MarketPlace.deleted_at.is_(None), MarketPlace.container_type == "page")
|
||||
.order_by(MarketPlace.name)
|
||||
)
|
||||
all_markets = result.scalars().all()
|
||||
|
||||
# Resolve page posts via blog service
|
||||
post_ids = list({m.container_id for m in all_markets})
|
||||
posts_by_id = {
|
||||
p.id: p
|
||||
for p in await services.blog.get_posts_by_ids(g.s, post_ids)
|
||||
}
|
||||
markets = []
|
||||
for market in all_markets:
|
||||
market.page = posts_by_id.get(market.container_id)
|
||||
markets.append(market)
|
||||
|
||||
html = await render_template(
|
||||
"_types/market/markets_listing.html",
|
||||
markets=markets,
|
||||
)
|
||||
return await make_response(html)
|
||||
|
||||
return app
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user