Refactor case prior display: extract prior card markup into a separate partial for improved maintainability and readability

This commit is contained in:
Ross
2025-11-17 09:27:02 +00:00
parent 740d013198
commit 0098fa5dfc
3 changed files with 119 additions and 80 deletions
@@ -38,73 +38,7 @@
<div class="row g-3">
{% for case, added, relation, visibility in available_priors %}
<div class="col-12 col-md-6 col-lg-4" data-case="{{case.id}}">
<div class="card h-100 shadow-sm {% if added %}border-success{% else %}border-secondary{% endif %}">
<div class="card-body d-flex flex-column">
<div class="d-flex justify-content-between align-items-start mb-2">
<h5 class="card-title mb-0">{{case.title}}</h5>
<div>
{% if added %}
<span class="badge bg-success">Added</span>
{% else %}
<span class="badge bg-secondary">Available</span>
{% endif %}
</div>
</div>
<p class="card-text small text-muted mb-2">Relation: <span class="fw-medium">{{ relation }}</span></p>
<div class="mb-3 small">{{case.get_series_blocks|safe}}</div>
<div class="mt-auto">
<div class="d-flex gap-2 mb-2">
<a href="{% url 'atlas:case_detail' case.id %}" class="btn btn-sm btn-outline-primary">View case</a>
</div>
{% if not added %}
<form class="row g-2" hx-post="{% url 'atlas:collection_case_priors' collection.id case_detail.case.pk %}"
hx-target="#htmx-response-{{case.id}}">
{% csrf_token %}
<input type="hidden" name="prior_case_id" value="{{case.id}}" />
<div class="col-12">
<input class="form-control form-control-sm" type="text" name="relation" value="{{relation}}" placeholder="Relation to case" />
</div>
<div class="col-8">
<select class="form-select form-select-sm" name="prior_visibility" id="prior_visibility_{{case.id}}">
<option value="AL" {% if visibility == 'AL' %}selected{% endif %}>Always</option>
<option value="RE" {% if visibility == 'RE' %}selected{% endif %}>Review</option>
<option value="NO" {% if visibility == 'NO' %}selected{% endif %}>None</option>
</select>
</div>
<div class="col-4 d-grid">
<button class="btn btn-sm btn-primary" type="submit">Add</button>
</div>
</form>
{% else %}
<form hx-post="{% url 'atlas:collection_case_priors' collection.id case_detail.case.pk %}"
hx-target="#htmx-response-{{case.id}}">
{% csrf_token %}
<input type="hidden" name="remove" value="{{case.id}}" />
<div class="mb-2 small text-muted">Current relation: <strong>{{relation}}</strong></div>
<div class="d-flex gap-2">
<select class="form-select form-select-sm w-50" name="prior_visibility" id="prior_visibility_{{case.id}}" disabled>
<option value="AL" {% if visibility == 'AL' %}selected{% endif %}>Always</option>
<option value="RE" {% if visibility == 'RE' %}selected{% endif %}>Review</option>
<option value="NO" {% if visibility == 'NO' %}selected{% endif %}>None</option>
</select>
<button class="btn btn-sm btn-danger" type="submit">Remove</button>
</div>
</form>
{% endif %}
</div>
</div>
<div class="card-footer bg-transparent">
<span id="htmx-response-{{case.id}}" class="small"></span>
</div>
</div>
</div>
{% include 'atlas/partials/_prior_card.html' with case=case added=added relation=relation visibility=visibility case_detail=case_detail collection=collection %}
{% endfor %}
</div>
@@ -0,0 +1,67 @@
<div id="prior-card-{{case.id}}" class="col-12 col-md-6 col-lg-4" data-case="{{case.id}}">
<div class="card h-100 shadow-sm {% if added %}border-success{% else %}border-secondary{% endif %}">
<div class="card-body d-flex flex-column">
<div class="d-flex justify-content-between align-items-start mb-2">
<h5 class="card-title mb-0">{{case.title}}</h5>
<div>
{% if added %}
<span class="badge bg-success">Added</span>
{% else %}
<span class="badge bg-secondary">Available</span>
{% endif %}
</div>
</div>
<p class="card-text small text-muted mb-2">Relation: <span class="fw-medium">{{ relation }}</span></p>
<div class="mb-3 small">{{case.get_series_blocks|safe}}</div>
<div class="mt-auto">
<div class="d-flex gap-2 mb-2">
<a href="{% url 'atlas:case_detail' case.id %}" class="btn btn-sm btn-outline-primary">View case</a>
</div>
{% if not added %}
<form class="row g-2" hx-post="{% url 'atlas:collection_case_priors' collection.id case_detail.case.pk %}"
hx-target="#prior-card-{{case.id}}" hx-swap="outerHTML">
{% csrf_token %}
<input type="hidden" name="prior_case_id" value="{{case.id}}" />
<div class="col-12">
<input class="form-control form-control-sm" type="text" name="relation" value="{{relation}}" placeholder="Relation to case" />
</div>
<div class="col-8">
<select class="form-select form-select-sm" name="prior_visibility" id="prior_visibility_{{case.id}}">
<option value="AL" {% if visibility == 'AL' %}selected{% endif %}>Always</option>
<option value="RE" {% if visibility == 'RE' %}selected{% endif %}>Review</option>
<option value="NO" {% if visibility == 'NO' %}selected{% endif %}>None</option>
</select>
</div>
<div class="col-4 d-grid">
<button class="btn btn-sm btn-primary" type="submit">Add</button>
</div>
</form>
{% else %}
<form hx-post="{% url 'atlas:collection_case_priors' collection.id case_detail.case.pk %}"
hx-target="#prior-card-{{case.id}}" hx-swap="outerHTML">
{% csrf_token %}
<input type="hidden" name="remove" value="{{case.id}}" />
<div class="mb-2 small text-muted">Current relation: <strong>{{relation}}</strong></div>
<div class="d-flex gap-2">
<select class="form-select form-select-sm w-50" name="prior_visibility" id="prior_visibility_{{case.id}}" disabled>
<option value="AL" {% if visibility == 'AL' %}selected{% endif %}>Always</option>
<option value="RE" {% if visibility == 'RE' %}selected{% endif %}>Review</option>
<option value="NO" {% if visibility == 'NO' %}selected{% endif %}>None</option>
</select>
<button class="btn btn-sm btn-danger" type="submit">Remove</button>
</div>
</form>
{% endif %}
</div>
</div>
<div class="card-footer bg-transparent">
<small class="text-muted">Case ID: {{ case.id }}</small>
</div>
</div>
</div>
+51 -13
View File
@@ -2758,33 +2758,71 @@ def collection_take_start(request, pk, cid=None, passcode=None):
@user_is_collection_author_or_atlas_editor
def collection_case_priors(request, exam_id, case_id):
case_detail = CaseDetail.objects.get(case=case_id, collection=exam_id)
collection = case_detail.collection
if request.htmx:
# Ensure we can render the updated single-card partial and return it so HTMX
# can swap the card on the client side.
if "remove" in request.POST:
p = CasePrior.objects.get(
case_detail=case_detail, prior_case=request.POST["remove"]
prior_pk = request.POST["remove"]
try:
p = CasePrior.objects.get(case_detail=case_detail, prior_case=prior_pk)
p.delete()
except CasePrior.DoesNotExist:
# Already removed; continue and render the not-added card
pass
prior_case = Case.objects.get(pk=prior_pk)
added = False
relation = ""
visibility = "AL"
html = render_to_string(
"atlas/partials/_prior_card.html",
{
"case": prior_case,
"added": added,
"relation": relation,
"visibility": visibility,
"case_detail": case_detail,
"collection": collection,
},
request=request,
)
p.delete()
return HttpResponse(f"Case removed")
return HttpResponse(html)
elif "prior_case_id" in request.POST:
if not request.POST["relation"]:
if not request.POST.get("relation"):
return HttpResponse(
"You need to enter text to describe the relationship between the cases"
"You need to enter text to describe the relationship between the cases",
status=400,
)
prior_case = Case.objects.get(pk=request.POST["prior_case_id"])
p, created = CasePrior.objects.get_or_create(
case_detail=case_detail, prior_case=prior_case
)
p.relation_text = request.POST["relation"]
p.prior_visibility = request.POST["prior_visibility"]
p.relation_text = request.POST.get("relation", "")
p.prior_visibility = request.POST.get("prior_visibility", "AL")
p.save()
return HttpResponse(f"Case added")
added = True
relation = p.relation_text
visibility = p.prior_visibility
html = render_to_string(
"atlas/partials/_prior_card.html",
{
"case": prior_case,
"added": added,
"relation": relation,
"visibility": visibility,
"case_detail": case_detail,
"collection": collection,
},
request=request,
)
return HttpResponse(html)
else:
return HttpResponse("False")
raise Http404()
return HttpResponse("False", status=400)
collection = case_detail.collection