Attribute Devii AI spend to its invoking action, fix quiz question-at-a-time review, DB API/isslop result routes, workspace docs, and drop redundant docstrings

- Route Devii-driven AI gateway cost to the action/tool that triggered
  it instead of a blanket "internal" bucket, so per-feature AI spend
  is attributable.
- Fix the quiz attempt review to show one previously-answered question
  at a time instead of all of them at once, and stop a quiz endpoint
  linked from the quiz flow from responding with raw JSON.
- Add DB API async query result route and AI Usage Analyzer annotated
  source/media routes, with traversal-safe uid/path handling and
  matching tests.
- Add Code Farm action audit logging (plant/harvest/buy-plot/upgrade/
  fertilize) and related admin workspace/services/trash/gateway route
  and doc touch-ups.
- Drop redundant docstrings from access_tokens.py per the no-comments
  convention.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VL9Xn57W5UR3HZbbuuzxdK
This commit is contained in:
2026-09-03 08:47:57 +02:00
co-authored by Claude Sonnet 5
parent 9d7b3db314
commit 57087536e5
76 changed files with 805 additions and 338 deletions
+2 -1
View File
@@ -74,7 +74,7 @@ from .moderation import (
years_between,
)
from .comments import _drop_blocked, _build_comment_items, load_comments, get_recent_comments_by_target_uids, get_recent_comments_by_post_uids, load_comments_by_target_uids
from .content import resolve_by_slug, resolve_object_url, get_uids_by_username_match, text_search_clause, get_daily_topic, get_featured_news, get_trending_topics
from .content import resolve_by_slug, resolve_object_url, get_projects_by_uids, get_uids_by_username_match, text_search_clause, get_daily_topic, get_featured_news, get_trending_topics
from .attachments_data import get_attachments, get_attachments_by_type, get_news_images_by_uids, delete_attachment_record, delete_attachments, _delete_attachment_file, get_user_media, get_user_attachments, get_user_attachment, get_deleted_media
from .stats import _stats_cache, get_site_stats, _analytics_cache, get_platform_analytics, _gist_languages_cache, get_gist_languages
from .schema import BUG_TABLE_RENAMES, migrate_bug_tables_to_issue_tables, init_db, _refresh_query_planner_stats, OLD_GATEWAY_URL, migrate_ai_gateway_settings, backfill_api_keys, _backfill_gamification
@@ -296,6 +296,7 @@ __all__ = [
"load_comments_by_target_uids",
"resolve_by_slug",
"resolve_object_url",
"get_projects_by_uids",
"get_uids_by_username_match",
"text_search_clause",
"get_daily_topic",
+11
View File
@@ -102,6 +102,17 @@ def resolve_object_url(target_type: str, target_uid: str) -> str:
return "/feed"
def get_projects_by_uids(uids):
if not uids or "projects" not in db.tables:
return {}
projects = get_table("projects")
if "uid" not in projects.columns:
return {}
seen = set()
unique = [u for u in uids if u not in seen and not seen.add(u)]
return {p["uid"]: p for p in projects.find(projects.table.columns.uid.in_(unique))}
def get_uids_by_username_match(search, limit=200):
term = (search or "").strip()
if not term or "users" not in db.tables:
+43 -29
View File
@@ -4,7 +4,7 @@ import os
from .core import TTLCache, _in_clause, _now_iso, db, get_table
from .users import get_users_by_uids
from .soft_delete import soft_delete, soft_delete_in
from .soft_delete import soft_delete_in
VOTABLE_TARGETS: dict[str, str] = {
@@ -133,6 +133,26 @@ def update_target_stars(target_type: str, target_uid: str, net_stars: int) -> No
get_table(table_name).update({"uid": target_uid, "stars": net_stars}, ["uid"])
def _child_uids(table_name, parent_column, parent_uids, live_only=False):
if table_name not in db.tables:
return []
table = db[table_name]
if parent_column not in table.columns:
return []
clause = table.table.columns[parent_column].in_(parent_uids)
if live_only:
return [row["uid"] for row in table.find(clause, deleted_at=None)]
return [row["uid"] for row in table.find(clause)]
def _delete_in(table_name, column, uids):
placeholders, params = _in_clause(uids)
with db:
db.query(
f"DELETE FROM {table_name} WHERE {column} IN ({placeholders})", **params
)
def soft_delete_engagement(target_type: str, target_uids: list, deleted_by: str) -> None:
uids = [uid for uid in (target_uids or []) if uid]
if not uids:
@@ -145,21 +165,15 @@ def soft_delete_engagement(target_type: str, target_uids: list, deleted_by: str)
"bookmarks", "target_uid", uids, deleted_by, stamp=stamp, target_type=target_type
)
if target_type == "post" and "polls" in db.tables:
for uid in uids:
for poll in db["polls"].find(post_uid=uid, deleted_at=None):
soft_delete("poll_votes", deleted_by, stamp=stamp, poll_uid=poll["uid"])
soft_delete("poll_options", deleted_by, stamp=stamp, poll_uid=poll["uid"])
soft_delete("polls", deleted_by, stamp=stamp, post_uid=uid)
poll_uids = _child_uids("polls", "post_uid", uids, live_only=True)
soft_delete_in("poll_votes", "poll_uid", poll_uids, deleted_by, stamp=stamp)
soft_delete_in("poll_options", "poll_uid", poll_uids, deleted_by, stamp=stamp)
soft_delete_in("polls", "post_uid", uids, deleted_by, stamp=stamp)
if target_type == "post" and "opinion_wars" in db.tables:
for uid in uids:
for war in db["opinion_wars"].find(post_uid=uid, deleted_at=None):
soft_delete(
"opinion_war_fighters", deleted_by, stamp=stamp, war_uid=war["uid"]
)
soft_delete(
"opinion_war_events", deleted_by, stamp=stamp, war_uid=war["uid"]
)
soft_delete("opinion_wars", deleted_by, stamp=stamp, post_uid=uid)
war_uids = _child_uids("opinion_wars", "post_uid", uids, live_only=True)
soft_delete_in("opinion_war_fighters", "war_uid", war_uids, deleted_by, stamp=stamp)
soft_delete_in("opinion_war_events", "war_uid", war_uids, deleted_by, stamp=stamp)
soft_delete_in("opinion_wars", "post_uid", uids, deleted_by, stamp=stamp)
def delete_engagement(target_type: str, target_uids: list) -> None:
@@ -184,21 +198,21 @@ def delete_engagement(target_type: str, target_uids: list) -> None:
**params,
)
if target_type == "post" and "polls" in tables:
for uid in uids:
for poll in db["polls"].find(post_uid=uid):
if "poll_votes" in tables:
db["poll_votes"].delete(poll_uid=poll["uid"])
if "poll_options" in tables:
db["poll_options"].delete(poll_uid=poll["uid"])
db["polls"].delete(post_uid=uid)
poll_uids = _child_uids("polls", "post_uid", uids)
if poll_uids:
if "poll_votes" in tables:
_delete_in("poll_votes", "poll_uid", poll_uids)
if "poll_options" in tables:
_delete_in("poll_options", "poll_uid", poll_uids)
_delete_in("polls", "post_uid", uids)
if target_type == "post" and "opinion_wars" in tables:
for uid in uids:
for war in db["opinion_wars"].find(post_uid=uid):
if "opinion_war_fighters" in tables:
db["opinion_war_fighters"].delete(war_uid=war["uid"])
if "opinion_war_events" in tables:
db["opinion_war_events"].delete(war_uid=war["uid"])
db["opinion_wars"].delete(post_uid=uid)
war_uids = _child_uids("opinion_wars", "post_uid", uids)
if war_uids:
if "opinion_war_fighters" in tables:
_delete_in("opinion_war_fighters", "war_uid", war_uids)
if "opinion_war_events" in tables:
_delete_in("opinion_war_events", "war_uid", war_uids)
_delete_in("opinion_wars", "post_uid", uids)
def get_target_owner_uid(target_type: str, target_uid: str) -> str | None:
-2
View File
@@ -15,8 +15,6 @@ def get_users_by_uids(uids):
_admins_cache = TTLCache(ttl=300, max_size=4)
# The primary administrator must be an account that can actually authenticate, so scan a
# few of the earliest admins and skip any that are soft-deleted or deactivated.
PRIMARY_ADMIN_CANDIDATES = 50