Offer the current exercise through an explicit API switch.
Deploy Development / deploy (push) Successful in 53s
Test Suite / pytest-backend (push) Failing after 29s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 16s
Test Suite / k6 /health Baseline (push) Successful in 35s
Test Suite / playwright-tests (push) Failing after 7m42s
Deploy Development / deploy (push) Successful in 53s
Test Suite / pytest-backend (push) Failing after 29s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 16s
Test Suite / k6 /health Baseline (push) Successful in 35s
Test Suite / playwright-tests (push) Failing after 7m42s
The exercise list turns the live catalog on or off for the caller's edit rights. A distribution key reads that current selection and the changes since its last cursor. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
4ad62b6e38
commit
b59b7eae16
13 files changed
+488
-5
No files matched your search
@@ -17,11 +17,19 @@ from distribution_contract import (
|
||||
REASON_CODES,
|
||||
can_withdraw,
|
||||
canonical_json,
|
||||
catalog_available,
|
||||
catalog_changes,
|
||||
catalog_document,
|
||||
catalog_hash,
|
||||
decode_catalog_cursor,
|
||||
encode_catalog_cursor,
|
||||
changed_areas,
|
||||
collect_blockers,
|
||||
cut_hash,
|
||||
decide_confirm,
|
||||
cursor_secret,
|
||||
exercise_offerable,
|
||||
InvalidCursor,
|
||||
key_covers,
|
||||
package_state,
|
||||
scope_denial,
|
||||
@@ -1624,6 +1632,145 @@ def _may_rerelease(cur, exercise: dict, profile_id: int, role: str) -> bool:
|
||||
return _denial_for(cur, exercise, {"kind": kind, "club_id": exercise.get("club_id")}, profile_id, role) is None
|
||||
|
||||
|
||||
def _rows_for_ids(cur, sql: str, ids: list[int]) -> list[dict]:
|
||||
if not ids:
|
||||
return []
|
||||
placeholders = ", ".join(["%s"] * len(ids))
|
||||
cur.execute(sql.format(placeholders=placeholders), tuple(ids))
|
||||
return [r2d(row) for row in _rows(cur)]
|
||||
|
||||
|
||||
def _offered_exercises(cur) -> list[dict]:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT id, public_id, title, summary, goal, execution, preparation, trainer_notes,
|
||||
equipment, duration_min, duration_max, visibility, club_id, created_by, api_offered
|
||||
FROM exercises
|
||||
WHERE api_offered = true
|
||||
ORDER BY title, id
|
||||
"""
|
||||
)
|
||||
return [r2d(row) for row in _rows(cur)]
|
||||
|
||||
|
||||
def _catalog_parts(cur, exercises: list[dict]) -> tuple[dict, dict]:
|
||||
ids = [int(item["id"]) for item in exercises]
|
||||
variants = _rows_for_ids(
|
||||
cur,
|
||||
"""
|
||||
SELECT id, exercise_id, public_id, variant_name, description, execution_changes, sequence_order
|
||||
FROM exercise_variants
|
||||
WHERE exercise_id IN ({placeholders})
|
||||
ORDER BY sequence_order NULLS LAST, id
|
||||
""",
|
||||
ids,
|
||||
)
|
||||
media = _rows_for_ids(
|
||||
cur,
|
||||
"""
|
||||
SELECT id, exercise_id, public_id, media_type, title, sort_order
|
||||
FROM exercise_media
|
||||
WHERE exercise_id IN ({placeholders})
|
||||
ORDER BY sort_order NULLS LAST, id
|
||||
""",
|
||||
ids,
|
||||
)
|
||||
by_variant: dict[int, list] = {}
|
||||
by_media: dict[int, list] = {}
|
||||
for item in variants:
|
||||
by_variant.setdefault(int(item["exercise_id"]), []).append(item)
|
||||
for item in media:
|
||||
by_media.setdefault(int(item["exercise_id"]), []).append(item)
|
||||
return by_variant, by_media
|
||||
|
||||
|
||||
def _catalog_entry(exercise: dict, variants: list[dict], media: list[dict]) -> tuple[dict, str]:
|
||||
document = catalog_document(exercise, variants, media)
|
||||
return document, catalog_hash(document)
|
||||
|
||||
|
||||
def load_catalog(cur, binding: dict, cursor_token: Optional[str]) -> dict:
|
||||
previous: dict = {}
|
||||
if cursor_token:
|
||||
snapshot_id = decode_catalog_cursor(cursor_secret(), binding["binding_id"], cursor_token)
|
||||
cur.execute(
|
||||
"SELECT binding_id, snapshot FROM distribution_catalog_snapshots WHERE id = %s",
|
||||
(snapshot_id,),
|
||||
)
|
||||
stored = _one(cur)
|
||||
if not stored or str(stored.get("binding_id")) != str(binding["binding_id"]):
|
||||
raise InvalidCursor()
|
||||
snapshot = stored.get("snapshot") or {}
|
||||
if isinstance(snapshot, str):
|
||||
import json
|
||||
|
||||
snapshot = json.loads(snapshot)
|
||||
previous = {str(key): value for key, value in dict(snapshot).items()}
|
||||
offered = _offered_exercises(cur)
|
||||
visible = [
|
||||
item
|
||||
for item in offered
|
||||
if catalog_available(True, grant_allows(cur, binding, item))
|
||||
]
|
||||
by_variant, by_media = _catalog_parts(cur, visible)
|
||||
current: dict[str, str] = {}
|
||||
exercises = []
|
||||
for item in visible:
|
||||
document, digest = _catalog_entry(
|
||||
item,
|
||||
by_variant.get(int(item["id"]), []),
|
||||
by_media.get(int(item["id"]), []),
|
||||
)
|
||||
current[document["source_id"]] = digest
|
||||
exercises.append(
|
||||
{"source_id": document["source_id"], "title": document["title"], "content_hash": digest}
|
||||
)
|
||||
snapshot_id = new_id()
|
||||
import json
|
||||
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO distribution_catalog_snapshots (id, binding_id, snapshot)
|
||||
VALUES (%s, %s, %s::jsonb)
|
||||
""",
|
||||
(snapshot_id, binding["binding_id"], json.dumps(current, ensure_ascii=False, sort_keys=True)),
|
||||
)
|
||||
return {
|
||||
"exercises": exercises,
|
||||
"changes": catalog_changes(previous, current),
|
||||
"next_cursor": encode_catalog_cursor(cursor_secret(), binding["binding_id"], snapshot_id),
|
||||
}
|
||||
|
||||
|
||||
def load_catalog_exercise(cur, binding: dict, source_id: str) -> dict:
|
||||
import uuid
|
||||
|
||||
try:
|
||||
uuid.UUID(str(source_id))
|
||||
except ValueError:
|
||||
raise HTTPException(status_code=404, detail={"code": "not_found", "message": "Übung nicht verfügbar."})
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT id, public_id, title, summary, goal, execution, preparation, trainer_notes,
|
||||
equipment, duration_min, duration_max, visibility, club_id, created_by, api_offered
|
||||
FROM exercises
|
||||
WHERE public_id = %s
|
||||
""",
|
||||
(str(source_id),),
|
||||
)
|
||||
exercise = _one(cur)
|
||||
in_scope = bool(exercise) and grant_allows(cur, binding, exercise)
|
||||
if not exercise or not catalog_available(bool(exercise.get("api_offered")), in_scope):
|
||||
raise HTTPException(status_code=404, detail={"code": "not_found", "message": "Übung nicht verfügbar."})
|
||||
by_variant, by_media = _catalog_parts(cur, [exercise])
|
||||
document, digest = _catalog_entry(
|
||||
exercise,
|
||||
by_variant.get(int(exercise["id"]), []),
|
||||
by_media.get(int(exercise["id"]), []),
|
||||
)
|
||||
return {**document, "content_hash": digest}
|
||||
|
||||
|
||||
def list_release_drift(cur, profile_id: int, role: str) -> list[dict]:
|
||||
cur.execute(
|
||||
"""
|
||||
|
||||
Reference in new issue
Block a user