fix: nest market blueprint under post slug /<slug>/<market_slug>/
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 43s
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:
67
app.py
67
app.py
@@ -54,53 +54,49 @@ 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:
|
||||||
|
if "slug" in values:
|
||||||
|
g.post_slug = values.pop("slug")
|
||||||
|
if "market_slug" in values:
|
||||||
g.market_slug = values.pop("market_slug")
|
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
|
||||||
market = (
|
|
||||||
await g.s.execute(
|
|
||||||
select(MarketPlace).where(
|
|
||||||
MarketPlace.slug == slug,
|
|
||||||
MarketPlace.deleted_at.is_(None),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
).scalar_one_or_none()
|
|
||||||
if not market:
|
|
||||||
abort(404)
|
|
||||||
g.market = market
|
|
||||||
|
|
||||||
# Load associated Post for context
|
# Load post by slug
|
||||||
post = (
|
post = (
|
||||||
await g.s.execute(
|
await g.s.execute(
|
||||||
select(Post).where(Post.id == market.post_id)
|
select(Post).where(Post.slug == post_slug)
|
||||||
)
|
)
|
||||||
).scalar_one_or_none()
|
).scalar_one_or_none()
|
||||||
if post:
|
if not post:
|
||||||
|
abort(404)
|
||||||
|
|
||||||
g.post_data = {
|
g.post_data = {
|
||||||
"post": {
|
"post": {
|
||||||
"id": post.id,
|
"id": post.id,
|
||||||
@@ -114,13 +110,30 @@ def create_app() -> "Quart":
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Load market scoped to post
|
||||||
|
market = (
|
||||||
|
await g.s.execute(
|
||||||
|
select(MarketPlace).where(
|
||||||
|
MarketPlace.slug == market_slug,
|
||||||
|
MarketPlace.post_id == post.id,
|
||||||
|
MarketPlace.deleted_at.is_(None),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
).scalar_one_or_none()
|
||||||
|
if not market:
|
||||||
|
abort(404)
|
||||||
|
g.market = market
|
||||||
|
|
||||||
# --- 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()
|
||||||
|
|||||||
@@ -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 = {
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
@@ -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/")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Submodule shared_lib updated: 42f4a8b68f...a420bfa7f0
@@ -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 %}
|
||||||
|
|||||||
Reference in New Issue
Block a user