Fix source display formatting and improve missing_map handling in import preview and confirmation

This commit is contained in:
Ross
2025-10-20 12:11:00 +01:00
parent 9dd4fc0402
commit def291c853
2 changed files with 58 additions and 27 deletions
@@ -34,7 +34,7 @@
</ul>
</p>
<p><strong>Feedback:</strong> {{ item.payload.feedback|default:''|safe }}</p>
<p>><strong>Sources:</strong>
<p><strong>Sources:</strong>
{% if item.payload.sources %}
<ul>
{% for source in item.payload.sources %}
@@ -56,14 +56,14 @@
<button type="button" class="m2m-toggle btn btn-sm btn-success ms-1" data-idx="{{ item.index }}" data-model="Finding" data-value="{{ val }}">{{ val }}</button>
{% endfor %}
{% endif %}
{% if item.missing_map.Finding %}
{% for val in item.missing_map.Finding %}
{% if item.missing_map.finding %}
{% for val in item.missing_map.finding %}
<button type="button" class="m2m-toggle btn btn-sm btn-success ms-1" data-idx="{{ item.index }}" data-model="Finding" data-value="{{ val }}">{{ val }}</button>
{% endfor %}
{% endif %}
<span class="text-muted m2m-none" style="display:none">(none)</span>
{% if item.missing_map.Finding %}
<div class="small text-info">Would create: {{ item.missing_map.Finding|join:", " }}</div>
{% if item.missing_map.finding %}
<div class="small text-info">Would create: {{ item.missing_map.finding|join:", " }}</div>
{% endif %}
</div>
</div>
@@ -75,14 +75,14 @@
<button type="button" class="m2m-toggle btn btn-sm btn-success ms-1" data-idx="{{ item.index }}" data-model="Structure" data-value="{{ val }}">{{ val }}</button>
{% endfor %}
{% endif %}
{% if item.missing_map.Structure %}
{% for val in item.missing_map.Structure %}
{% if item.missing_map.structure %}
{% for val in item.missing_map.structure %}
<button type="button" class="m2m-toggle btn btn-sm btn-success ms-1" data-idx="{{ item.index }}" data-model="Structure" data-value="{{ val }}">{{ val }}</button>
{% endfor %}
{% endif %}
<span class="text-muted m2m-none" style="display:none">(none)</span>
{% if item.missing_map.Structure %}
<div class="small text-info">Would create: {{ item.missing_map.Structure|join:", " }}</div>
{% if item.missing_map.structure %}
<div class="small text-info">Would create: {{ item.missing_map.structure|join:", " }}</div>
{% endif %}
</div>
@@ -93,14 +93,14 @@
<button type="button" class="m2m-toggle btn btn-sm btn-success ms-1" data-idx="{{ item.index }}" data-model="Condition" data-value="{{ val }}">{{ val }}</button>
{% endfor %}
{% endif %}
{% if item.missing_map.Condition %}
{% for val in item.missing_map.Condition %}
{% if item.missing_map.condition %}
{% for val in item.missing_map.condition %}
<button type="button" class="m2m-toggle btn btn-sm btn-success ms-1" data-idx="{{ item.index }}" data-model="Condition" data-value="{{ val }}">{{ val }}</button>
{% endfor %}
{% endif %}
<span class="text-muted m2m-none" style="display:none">(none)</span>
{% if item.missing_map.Condition %}
<div class="small text-info">Would create: {{ item.missing_map.Condition|join:", " }}</div>
{% if item.missing_map.condition %}
<div class="small text-info">Would create: {{ item.missing_map.condition|join:", " }}</div>
{% endif %}
</div>
@@ -111,14 +111,14 @@
<button type="button" class="m2m-toggle btn btn-sm btn-success ms-1" data-idx="{{ item.index }}" data-model="Presentation" data-value="{{ val }}">{{ val }}</button>
{% endfor %}
{% endif %}
{% if item.missing_map.Presentation %}
{% for val in item.missing_map.Presentation %}
{% if item.missing_map.presentation %}
{% for val in item.missing_map.presentation %}
<button type="button" class="m2m-toggle btn btn-sm btn-success ms-1" data-idx="{{ item.index }}" data-model="Presentation" data-value="{{ val }}">{{ val }}</button>
{% endfor %}
{% endif %}
<span class="text-muted m2m-none" style="display:none">(none)</span>
{% if item.missing_map.Presentation %}
<div class="small text-info">Would create: {{ item.missing_map.Presentation|join:", " }}</div>
{% if item.missing_map.presentation %}
<div class="small text-info">Would create: {{ item.missing_map.presentation|join:", " }}</div>
{% endif %}
</div>
@@ -129,14 +129,14 @@
<button type="button" class="m2m-toggle btn btn-sm btn-success ms-1" data-idx="{{ item.index }}" data-model="Subspecialty" data-value="{{ val }}">{{ val }}</button>
{% endfor %}
{% endif %}
{% if item.missing_map.Subspecialty %}
{% for val in item.missing_map.Subspecialty %}
{% if item.missing_map.subspecialty %}
{% for val in item.missing_map.subspecialty %}
<button type="button" class="m2m-toggle btn btn-sm btn-success ms-1" data-idx="{{ item.index }}" data-model="Subspecialty" data-value="{{ val }}">{{ val }}</button>
{% endfor %}
{% endif %}
<span class="text-muted m2m-none" style="display:none">(none)</span>
{% if item.missing_map.Subspecialty %}
<div class="small text-info">Would create: {{ item.missing_map.Subspecialty|join:", " }}</div>
{% if item.missing_map.subspecialty %}
<div class="small text-info">Would create: {{ item.missing_map.subspecialty|join:", " }}</div>
{% endif %}
</div>
</div>
+37 -6
View File
@@ -694,11 +694,8 @@ def import_llm_questions(request):
# Build a simple map of missing values per model (stringified) for robust template rendering
# Provide both lowercase and capitalized keys so templates referencing either form will work
missing_map = {}
for k, v in m2m_map.items():
vals = [str(x) for x in v["missing"]]
missing_map[k] = vals
missing_map[k.capitalize()] = vals
# missing_map keys are lowercase (matching m2m_map keys)
missing_map = {k: [str(x) for x in v["missing"]] for k, v in m2m_map.items()}
item_report["missing_map"] = missing_map
# Debug: log missing_map and resolved_m2m for troubleshooting
@@ -844,6 +841,31 @@ def import_llm_confirm(request):
# perform actual import for selected indices
from atlas.models import Finding, Structure, Condition, Presentation, Subspecialty
# Parse exclude inputs of the form exclude_<idx>="Model:::value" (may be multiple per idx)
excluded_map = defaultdict(lambda: defaultdict(set))
for k, vlist in request.POST.lists():
if not k.startswith("exclude_"):
continue
try:
idx_str = k.split("exclude_")[1]
idx_key = int(idx_str)
except Exception:
continue
for entry in vlist:
# entry format: Model:::value
try:
model_name, val = entry.split(":::", 1)
excluded_map[idx_key][model_name].add(val)
except Exception:
continue
# Debug: log exclusions parsed from POST
try:
# convert sets to lists for logging
log_map = {i: {m: list(vals) for m, vals in models.items()} for i, models in excluded_map.items()}
logger.debug("import_llm_confirm: excluded_map=%s", log_map)
except Exception:
logger.exception("Failed to log excluded_map")
created = 0
errors = []
actually_created = defaultdict(list)
@@ -899,9 +921,18 @@ def import_llm_confirm(request):
if not qs.exists():
qs = model_cls.objects.filter(name__icontains=v)
if qs.exists():
final_objs.extend(list(qs[:5]))
# add resolved objects unless excluded for this item
for obj in list(qs[:5]):
if obj.name in excluded_map.get(idx, {}).get(model_cls.__name__, set()):
logger.debug("Skipping resolved attach for idx=%s model=%s name=%s due to exclusion", idx, model_cls.__name__, obj.name)
continue
final_objs.append(obj)
else:
if allow_create_m2m:
# skip creation if excluded for this item
if v in excluded_map.get(idx, {}).get(model_cls.__name__, set()):
logger.debug("Skipping creation for idx=%s model=%s name=%s due to exclusion", idx, model_cls.__name__, v)
continue
obj, created_flag = model_cls.objects.get_or_create(name=v)
final_objs.append(obj)
actually_created[model_cls.__name__].append(obj.pk)