Replace ~250 render_to_sx calls across all services with sync sx_call, converting many async functions to sync where no other awaits remained. Make render_to_sx/render_to_sx_with_env private (_render_to_sx). Add (post-header-ctx) IO primitive and shared post/post-admin defmacros. Convert built-in post/post-admin layouts from Python to register_sx_layout with .sx defcomps. Remove dead post_admin_mobile_nav_sx. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
63 lines
1.7 KiB
Python
63 lines
1.7 KiB
Python
from __future__ import annotations
|
|
|
|
from quart import Blueprint, request, g, abort
|
|
|
|
from shared.browser.app.authz import require_login
|
|
from shared.sx.helpers import sx_response, sx_call
|
|
from models import Snippet
|
|
|
|
|
|
VALID_VISIBILITY = frozenset({"private", "shared", "admin"})
|
|
|
|
|
|
async def _render_snippets():
|
|
"""Render snippets list via service data + .sx defcomp."""
|
|
from shared.services.registry import services
|
|
data = await services.blog_page.snippets_data(g.s)
|
|
return sx_call("blog-snippets-content", **data)
|
|
|
|
|
|
def register():
|
|
bp = Blueprint("snippets", __name__, url_prefix="/settings/snippets")
|
|
|
|
@bp.delete("/<int:snippet_id>/")
|
|
@require_login
|
|
async def delete_snippet(snippet_id: int):
|
|
"""Delete a snippet. Owners delete their own; admins can delete any."""
|
|
snippet = await g.s.get(Snippet, snippet_id)
|
|
if not snippet:
|
|
abort(404)
|
|
|
|
is_admin = g.rights.get("admin")
|
|
if snippet.user_id != g.user.id and not is_admin:
|
|
abort(403)
|
|
|
|
await g.s.delete(snippet)
|
|
await g.s.flush()
|
|
|
|
return sx_response(await _render_snippets())
|
|
|
|
@bp.patch("/<int:snippet_id>/visibility/")
|
|
@require_login
|
|
async def patch_visibility(snippet_id: int):
|
|
"""Change snippet visibility. Admin only."""
|
|
if not g.rights.get("admin"):
|
|
abort(403)
|
|
|
|
snippet = await g.s.get(Snippet, snippet_id)
|
|
if not snippet:
|
|
abort(404)
|
|
|
|
form = await request.form
|
|
visibility = form.get("visibility", "").strip()
|
|
|
|
if visibility not in VALID_VISIBILITY:
|
|
abort(400)
|
|
|
|
snippet.visibility = visibility
|
|
await g.s.flush()
|
|
|
|
return sx_response(await _render_snippets())
|
|
|
|
return bp
|