Implement deeper analysis of people and access to the underlying data in the database

This commit is contained in:
2026-02-15 18:02:52 +00:00
parent a94bbff655
commit e7aac36ef9
4 changed files with 398 additions and 96 deletions

View File

@@ -260,6 +260,57 @@ def _best_engage_source(plan):
return (None, "")
def _engage_source_options(plan):
if plan is None:
return []
options = []
for rule in plan.rules.order_by("created_at"):
options.append(
{
"value": f"rule:{rule.id}",
"label": f"Rule: {rule.title}",
}
)
for game in plan.games.order_by("created_at"):
options.append(
{
"value": f"game:{game.id}",
"label": f"Game: {game.title}",
}
)
for correction in plan.corrections.order_by("created_at"):
options.append(
{
"value": f"correction:{correction.id}",
"label": f"Correction: {correction.title}",
}
)
return options
def _engage_source_from_ref(plan, source_ref):
if plan is None:
return (None, "", "")
ref = str(source_ref or "").strip()
if ":" not in ref:
return (None, "", "")
kind, raw_id = ref.split(":", 1)
kind = kind.strip().lower()
raw_id = raw_id.strip()
model_by_kind = {
"rule": plan.rules,
"game": plan.games,
"correction": plan.corrections,
}
queryset = model_by_kind.get(kind)
if queryset is None:
return (None, "", "")
obj = queryset.filter(id=raw_id).first()
if obj is None:
return (None, "", "")
return (obj, kind, f"{kind}:{obj.id}")
def _context_base(user, service, identifier, person):
person_identifier = None
if person is not None:
@@ -672,12 +723,54 @@ class ComposeEngagePreview(LoginRequiredMixin, View):
owner_name = _owner_name(request.user)
recipient_name = base["person"].name if base["person"] else "Other"
plan = _latest_plan_for_person(request.user, base["person"])
source_obj, source_kind = _best_engage_source(plan)
source_options = _engage_source_options(plan)
source_options_with_custom = (
[{"value": "auto", "label": "Auto"}]
+ source_options
+ [{"value": "custom", "label": "Custom"}]
)
source_ref = str(request.GET.get("source_ref") or "auto").strip().lower()
custom_text = str(request.GET.get("custom_text") or "").strip()
source_obj = None
source_kind = ""
selected_source = source_ref if source_ref else "auto"
if selected_source == "custom":
selected_source = "custom"
else:
if selected_source == "auto":
fallback_obj, fallback_kind = _best_engage_source(plan)
if fallback_obj is not None:
source_obj = fallback_obj
source_kind = fallback_kind
else:
source_obj, source_kind, explicit_ref = _engage_source_from_ref(
plan,
selected_source,
)
if source_obj is None:
selected_source = "auto"
fallback_obj, fallback_kind = _best_engage_source(plan)
if fallback_obj is not None:
source_obj = fallback_obj
source_kind = fallback_kind
else:
selected_source = explicit_ref
preview = ""
outbound = ""
artifact_label = "AI-generated"
if source_obj is not None:
if selected_source == "custom":
outbound = _plain_text(custom_text)
if outbound:
preview = f"**Custom Engage** (Correction)\n\nGuidance:\n{outbound}"
artifact_label = "Custom"
else:
preview = (
"**Custom Engage** (Correction)\n\nGuidance:\n"
"Enter your custom engagement text to preview."
)
elif source_obj is not None:
payload = _build_engage_payload(
source_obj=source_obj,
source_kind=source_kind,
@@ -707,17 +800,19 @@ class ComposeEngagePreview(LoginRequiredMixin, View):
)
preview = f"**Shared Engage** (Correction)\n\nGuidance:\n{outbound}"
token = signing.dumps(
{
"u": request.user.id,
"s": base["service"],
"i": base["identifier"],
"p": str(base["person"].id) if base["person"] else "",
"outbound": outbound,
"exp": int(time.time()) + (60 * 10),
},
salt=COMPOSE_ENGAGE_TOKEN_SALT,
)
token = ""
if outbound:
token = signing.dumps(
{
"u": request.user.id,
"s": base["service"],
"i": base["identifier"],
"p": str(base["person"].id) if base["person"] else "",
"outbound": outbound,
"exp": int(time.time()) + (60 * 10),
},
salt=COMPOSE_ENGAGE_TOKEN_SALT,
)
return JsonResponse(
{
"ok": True,
@@ -725,6 +820,9 @@ class ComposeEngagePreview(LoginRequiredMixin, View):
"outbound": outbound,
"token": token,
"artifact": artifact_label,
"options": source_options_with_custom,
"selected_source": selected_source,
"custom_text": custom_text,
}
)
@@ -744,7 +842,7 @@ class ComposeEngageSend(LoginRequiredMixin, View):
failsafe_confirm = str(request.POST.get("failsafe_confirm") or "").strip()
if failsafe_arm != "1" or failsafe_confirm != "1":
return JsonResponse(
{"ok": False, "error": "Enable both send safety switches first."}
{"ok": False, "error": "Enable send confirmation before sending."}
)
token = str(request.POST.get("engage_token") or "").strip()
@@ -814,7 +912,7 @@ class ComposeSend(LoginRequiredMixin, View):
request,
"partials/compose-send-status.html",
{
"notice_message": "Enable both send safety switches before sending.",
"notice_message": "Enable send confirmation before sending.",
"notice_level": "warning",
},
)