fix: nest market blueprint under post slug /<slug>/<market_slug>/
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 43s

Market app now follows the events app pattern — URLs include
the post slug prefix (e.g. /market/suma-market/). Hydrate
loads both post and market, verifying market belongs to post.
Scraper default URLs updated accordingly.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
giles
2026-02-10 20:15:45 +00:00
parent b831d01113
commit 6202b31e8e
6 changed files with 53 additions and 40 deletions

83
app.py
View File

@@ -54,38 +54,68 @@ def create_app() -> "Quart":
app.jinja_loader, app.jinja_loader,
]) ])
# Market blueprint scoped under /<market_slug>/ # Market blueprint nested under post slug: /<slug>/<market_slug>/
app.register_blueprint( app.register_blueprint(
register_market_bp( register_market_bp(
url_prefix="/", url_prefix="/",
title=config()["coop_title"], title=config()["coop_title"],
), ),
url_prefix="/<market_slug>", url_prefix="/<slug>/<market_slug>",
) )
# --- Auto-inject market_slug into url_for() calls --- # --- Auto-inject slug and market_slug into url_for() calls ---
@app.url_value_preprocessor @app.url_value_preprocessor
def pull_market_slug(endpoint, values): def pull_slugs(endpoint, values):
if values and "market_slug" in values: if values:
g.market_slug = values.pop("market_slug") if "slug" in values:
g.post_slug = values.pop("slug")
if "market_slug" in values:
g.market_slug = values.pop("market_slug")
@app.url_defaults @app.url_defaults
def inject_market_slug(endpoint, values): def inject_slugs(endpoint, values):
slug = g.get("market_slug") for attr, param in [("post_slug", "slug"), ("market_slug", "market_slug")]:
if slug and "market_slug" not in values: val = g.get(attr)
if app.url_map.is_endpoint_expecting(endpoint, "market_slug"): if val and param not in values:
values["market_slug"] = slug if app.url_map.is_endpoint_expecting(endpoint, param):
values[param] = val
# --- Load market data for market_slug --- # --- Load post and market data ---
@app.before_request @app.before_request
async def hydrate_market(): async def hydrate_market():
slug = getattr(g, "market_slug", None) post_slug = getattr(g, "post_slug", None)
if not slug: market_slug = getattr(g, "market_slug", None)
if not post_slug or not market_slug:
return return
# Load post by slug
post = (
await g.s.execute(
select(Post).where(Post.slug == post_slug)
)
).scalar_one_or_none()
if not post:
abort(404)
g.post_data = {
"post": {
"id": post.id,
"title": post.title,
"slug": post.slug,
"feature_image": post.feature_image,
"html": post.html,
"status": post.status,
"visibility": post.visibility,
"is_page": post.is_page,
},
}
# Load market scoped to post
market = ( market = (
await g.s.execute( await g.s.execute(
select(MarketPlace).where( select(MarketPlace).where(
MarketPlace.slug == slug, MarketPlace.slug == market_slug,
MarketPlace.post_id == post.id,
MarketPlace.deleted_at.is_(None), MarketPlace.deleted_at.is_(None),
) )
) )
@@ -94,33 +124,16 @@ def create_app() -> "Quart":
abort(404) abort(404)
g.market = market g.market = market
# Load associated Post for context
post = (
await g.s.execute(
select(Post).where(Post.id == market.post_id)
)
).scalar_one_or_none()
if post:
g.post_data = {
"post": {
"id": post.id,
"title": post.title,
"slug": post.slug,
"feature_image": post.feature_image,
"html": post.html,
"status": post.status,
"visibility": post.visibility,
"is_page": post.is_page,
},
}
# --- Root route: market listing --- # --- Root route: market listing ---
@app.get("/") @app.get("/")
async def markets_listing(): async def markets_listing():
from sqlalchemy.orm import selectinload
markets = ( markets = (
await g.s.execute( await g.s.execute(
select(MarketPlace) select(MarketPlace)
.where(MarketPlace.deleted_at.is_(None)) .where(MarketPlace.deleted_at.is_(None))
.options(selectinload(MarketPlace.post))
.order_by(MarketPlace.name) .order_by(MarketPlace.name)
) )
).scalars().all() ).scalars().all()

View File

@@ -11,7 +11,7 @@ async def capture_listing(
total_pages: int total_pages: int
): ):
sync_url = os.getenv("CAPTURE_LISTING_URL", "http://localhost:8001/suma-market/api/products/listing/") sync_url = os.getenv("CAPTURE_LISTING_URL", "http://localhost:8001/market/suma-market/api/products/listing/")
async with httpx.AsyncClient(timeout=httpx.Timeout(20.0, connect=10.0)) as client: async with httpx.AsyncClient(timeout=httpx.Timeout(20.0, connect=10.0)) as client:
_d = { _d = {

View File

@@ -8,7 +8,7 @@ from typing import Dict
async def save_nav( async def save_nav(
nav: Dict, nav: Dict,
): ):
sync_url = os.getenv("SAVE_NAV_URL", "http://localhost:8001/suma-market/api/products/nav/") sync_url = os.getenv("SAVE_NAV_URL", "http://localhost:8001/market/suma-market/api/products/nav/")
async with httpx.AsyncClient(timeout=httpx.Timeout(20.0, connect=10.0)) as client: async with httpx.AsyncClient(timeout=httpx.Timeout(20.0, connect=10.0)) as client:
resp = await client.post(sync_url, json=nav) resp = await client.post(sync_url, json=nav)

View File

@@ -21,7 +21,7 @@ async def upsert_product(
d["slug"] = slug d["slug"] = slug
# Where to post; override via env if needed # Where to post; override via env if needed
sync_url = os.getenv("PRODUCT_SYNC_URL", "http://localhost:8001/suma-market/api/products/sync/") sync_url = os.getenv("PRODUCT_SYNC_URL", "http://localhost:8001/market/suma-market/api/products/sync/")

View File

@@ -7,7 +7,7 @@
{% if markets %} {% if markets %}
<div class="grid gap-4"> <div class="grid gap-4">
{% for m in markets %} {% for m in markets %}
<a href="/{{ m.slug }}/" <a href="/{{ m.post.slug }}/{{ m.slug }}/"
class="block p-6 bg-white border border-stone-200 rounded-lg hover:border-stone-400 transition-colors"> class="block p-6 bg-white border border-stone-200 rounded-lg hover:border-stone-400 transition-colors">
<h2 class="text-lg font-semibold">{{ m.name }}</h2> <h2 class="text-lg font-semibold">{{ m.name }}</h2>
{% if m.description %} {% if m.description %}