Add alert for stripped numeric references in import preview and refactor payload cleaning function
This commit is contained in:
@@ -20,6 +20,11 @@
|
|||||||
<strong>Note:</strong> The following unsupported fields were dropped: <em>{{ item.dropped_fields|join:", " }}</em>
|
<strong>Note:</strong> The following unsupported fields were dropped: <em>{{ item.dropped_fields|join:", " }}</em>
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
{% if item.stripped_fields %}
|
||||||
|
<div class="alert alert-info" role="alert">
|
||||||
|
<strong>Note:</strong> Numeric references were removed from fields: <em>{{ item.stripped_fields|join:", " }}</em>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
{% if item.payload %}
|
{% if item.payload %}
|
||||||
<p><strong>Title:</strong> {{ item.payload.title|default:'(No title)'|safe }}</p>
|
<p><strong>Title:</strong> {{ item.payload.title|default:'(No title)'|safe }}</p>
|
||||||
<p><strong>Stem:</strong> {{ item.payload.stem|default:''|safe }}</p>
|
<p><strong>Stem:</strong> {{ item.payload.stem|default:''|safe }}</p>
|
||||||
|
|||||||
+24
-21
@@ -487,6 +487,27 @@ def _resolve_m2m(model_class, value):
|
|||||||
return list(qs[:5])
|
return list(qs[:5])
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
def _strip_numeric_refs_in_payload(p):
|
||||||
|
"""Return a copy of payload with inline numeric refs removed from any '*_feedback' or 'feedback' fields.
|
||||||
|
Also return a list of keys that were modified.
|
||||||
|
"""
|
||||||
|
if not isinstance(p, dict):
|
||||||
|
return p, []
|
||||||
|
cleaned = dict(p)
|
||||||
|
changed = []
|
||||||
|
# pattern matches [1], [1,2,3], or ranges like [6-9], or mixtures like [1,3-5,7]
|
||||||
|
pattern = re.compile(r"\[\s*(?:\d+(?:\s*-\s*\d+)?)(?:\s*,\s*(?:\d+(?:\s*-\s*\d+)?))*\s*\]")
|
||||||
|
for k, v in list(p.items()):
|
||||||
|
if not isinstance(v, str):
|
||||||
|
continue
|
||||||
|
if k.endswith("_feedback") or k == "feedback":
|
||||||
|
newv = pattern.sub("", v).strip()
|
||||||
|
# collapse multiple spaces
|
||||||
|
newv = re.sub(r"\s{2,}", " ", newv)
|
||||||
|
if newv != v:
|
||||||
|
cleaned[k] = newv
|
||||||
|
changed.append(k)
|
||||||
|
return cleaned, changed
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
@user_passes_test(lambda u: u.is_superuser)
|
@user_passes_test(lambda u: u.is_superuser)
|
||||||
@@ -570,27 +591,6 @@ def import_llm_questions(request):
|
|||||||
new_s, n = pattern.subn(r"\\\\", s)
|
new_s, n = pattern.subn(r"\\\\", s)
|
||||||
return new_s, n
|
return new_s, n
|
||||||
|
|
||||||
def _strip_numeric_refs_in_payload(p):
|
|
||||||
"""Return a copy of payload with inline numeric refs removed from any '*_feedback' or 'feedback' fields.
|
|
||||||
Also return a list of keys that were modified.
|
|
||||||
"""
|
|
||||||
if not isinstance(p, dict):
|
|
||||||
return p, []
|
|
||||||
cleaned = dict(p)
|
|
||||||
changed = []
|
|
||||||
# pattern matches [1] or [1,2, 3] with optional spaces
|
|
||||||
pattern = re.compile(r"\[\s*\d+(?:\s*,\s*\d+)*\s*\]")
|
|
||||||
for k, v in list(p.items()):
|
|
||||||
if not isinstance(v, str):
|
|
||||||
continue
|
|
||||||
if k.endswith("_feedback") or k == "feedback":
|
|
||||||
newv = pattern.sub("", v).strip()
|
|
||||||
# collapse multiple spaces
|
|
||||||
newv = re.sub(r"\s{2,}", " ", newv)
|
|
||||||
if newv != v:
|
|
||||||
cleaned[k] = newv
|
|
||||||
changed.append(k)
|
|
||||||
return cleaned, changed
|
|
||||||
|
|
||||||
raw_text_stripped = raw_text.strip()
|
raw_text_stripped = raw_text.strip()
|
||||||
raw_text, sanitized_count = _sanitize_backslashes(raw_text_stripped)
|
raw_text, sanitized_count = _sanitize_backslashes(raw_text_stripped)
|
||||||
@@ -775,6 +775,9 @@ def import_llm_questions(request):
|
|||||||
list((item_report.get("resolved_m2m") or {}).keys()),
|
list((item_report.get("resolved_m2m") or {}).keys()),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Ensure the item report payload reflects any cleaning (dropped fields / stripped refs)
|
||||||
|
item_report['payload'] = payload
|
||||||
|
|
||||||
# If dry-run, do not save; just report what would happen
|
# If dry-run, do not save; just report what would happen
|
||||||
if dry_run:
|
if dry_run:
|
||||||
item_report["status"] = "would_create"
|
item_report["status"] = "would_create"
|
||||||
|
|||||||
Reference in New Issue
Block a user