Refactor case prior display: extract prior card markup into a separate partial for improved maintainability and readability
This commit is contained in:
+51
-13
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user