feat: restructure market app with per-market URL scoping
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 41s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 41s
- URL structure changes from /<route> to /<market_slug>/<route> - Root / shows markets listing page - app.py: url_value_preprocessor, url_defaults, hydrate_market (events app pattern) - Browse queries (db_nav, db_products_nocounts, db_products_counts) accept market_id - _productInfo reads g.market.id to scope all queries - save_nav accepts market_id, sets on new NavTop rows - API save_nav passes g.market.id - Scraper default URLs point to /suma-market/ on port 8001 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
90
app.py
90
app.py
@@ -1,7 +1,11 @@
|
||||
from __future__ import annotations
|
||||
import path_setup # noqa: F401 # adds shared_lib to sys.path
|
||||
|
||||
from quart import g
|
||||
from pathlib import Path
|
||||
|
||||
from quart import g, abort, render_template, make_response
|
||||
from jinja2 import FileSystemLoader, ChoiceLoader
|
||||
from sqlalchemy import select
|
||||
|
||||
from shared.factory import create_base_app
|
||||
from config import config
|
||||
@@ -38,17 +42,95 @@ async def market_context() -> dict:
|
||||
|
||||
|
||||
def create_app() -> "Quart":
|
||||
from models.market_place import MarketPlace
|
||||
from models.ghost_content import Post
|
||||
|
||||
app = create_base_app("market", context_fn=market_context)
|
||||
|
||||
# Market blueprint at root (was /market in monolith)
|
||||
# App-specific templates override shared templates
|
||||
app_templates = str(Path(__file__).resolve().parent / "templates")
|
||||
app.jinja_loader = ChoiceLoader([
|
||||
FileSystemLoader(app_templates),
|
||||
app.jinja_loader,
|
||||
])
|
||||
|
||||
# Market blueprint scoped under /<market_slug>/
|
||||
app.register_blueprint(
|
||||
register_market_bp(
|
||||
url_prefix="/",
|
||||
url_prefix="/<market_slug>",
|
||||
title=config()["coop_title"],
|
||||
),
|
||||
url_prefix="/",
|
||||
url_prefix="/<market_slug>",
|
||||
)
|
||||
|
||||
# --- Auto-inject market_slug into url_for() calls ---
|
||||
@app.url_value_preprocessor
|
||||
def pull_market_slug(endpoint, values):
|
||||
if values and "market_slug" in values:
|
||||
g.market_slug = values.pop("market_slug")
|
||||
|
||||
@app.url_defaults
|
||||
def inject_market_slug(endpoint, values):
|
||||
slug = g.get("market_slug")
|
||||
if slug and "market_slug" not in values:
|
||||
if app.url_map.is_endpoint_expecting(endpoint, "market_slug"):
|
||||
values["market_slug"] = slug
|
||||
|
||||
# --- Load market data for market_slug ---
|
||||
@app.before_request
|
||||
async def hydrate_market():
|
||||
slug = getattr(g, "market_slug", None)
|
||||
if not slug:
|
||||
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
|
||||
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 ---
|
||||
@app.get("/")
|
||||
async def markets_listing():
|
||||
markets = (
|
||||
await g.s.execute(
|
||||
select(MarketPlace)
|
||||
.where(MarketPlace.deleted_at.is_(None))
|
||||
.order_by(MarketPlace.name)
|
||||
)
|
||||
).scalars().all()
|
||||
|
||||
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