Enhance NormalCase functionality: prepopulate age fields on edit, update author handling in create case normal view, and refine template logic for editing permissions

This commit is contained in:
Ross
2026-02-16 13:45:10 +00:00
parent 0b6661c018
commit 36da045d71
4 changed files with 33 additions and 16 deletions
+17 -1
View File
@@ -298,6 +298,22 @@ class NormalCaseForm(ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# If editing an existing NormalCase, prepopulate age_value/age_unit
try:
instance = kwargs.get('instance')
except Exception:
instance = None
if instance is None:
# also support self.instance when ModelForm sets it
instance = getattr(self, 'instance', None)
if instance is not None and getattr(instance, 'age_days', None) is not None:
try:
val, unit = NormalCase.days_to_value_unit(instance.age_days)
if val is not None and unit is not None:
self.fields['age_value'].initial = val
self.fields['age_unit'].initial = unit
except Exception:
pass
self.helper = FormHelper()
# Do not render a <form> tag from crispy here because the template
# already provides the surrounding form element (we inject this
@@ -315,7 +331,7 @@ class NormalCaseForm(ModelForm):
css_class="row",
),
Field("notes"),
Submit("submit", "Save", css_class="btn btn-primary"),
# Submit button rendered in modal footer; avoid duplicate button here
)
def save(self, commit=True):
+12 -12
View File
@@ -32,18 +32,18 @@
{% for normal in page_obj.object_list %}
<div class="list-group-item d-flex justify-content-between align-items-start">
<a class="flex-grow-1 text-decoration-none text-reset" href="{% url 'atlas:case_detail' normal.case.pk %}">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1">{{ normal.case.title }}</h5>
<small class="text-muted">{{ normal.added_date|date:"Y-m-d" }}</small>
</div>
<p class="mb-1">
Age: {{ normal.display_age }}
{% if normal.examination %} • Exam: {{ normal.examination }}{% endif %}
{% if normal.modality %} • Modality: {{ normal.modality }}{% endif %}
</p>
{% if normal.notes %}
<p class="mb-0 text-muted">{{ normal.notes }}</p>
{% endif %}
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-1">{{ normal.case.title }}</h5>
<small class="text-muted">{{ normal.added_date|date:"Y-m-d" }}</small>
</div>
<p class="mb-1">
Age: {{ normal.display_age }}
{% if normal.examination %} • Exam: {{ normal.examination }}{% endif %}
{% if normal.modality %} • Modality: {{ normal.modality }}{% endif %}
</p>
{% if normal.notes %}
<p class="mb-0 text-muted">{{ normal.notes }}</p>
{% endif %}
</a>
<div class="ms-2">
{% if request.user.is_superuser or request.user in normal.author.all %}
@@ -15,8 +15,7 @@
</button>
{% endif %}
{# Allow NormalCase authors (or atlas editors/superusers) to edit the Normal metadata #}
{% if case.normal_case and (request.user.is_superuser or request.user in case.normal_case.author.all or can_edit) %}
{% if request.user.is_superuser or can_edit or request.user in case.normal_case.author.all %}
<button class="btn btn-sm btn-outline-secondary ms-2"
hx-get="{% url 'atlas:case_normal_edit' case.normal_case.pk %}"
hx-target="body"
+3 -1
View File
@@ -847,8 +847,10 @@ def create_case_normal(request, pk):
logger.debug("NormalCaseForm.cleaned_data: {}", form.cleaned_data)
nc = form.save(commit=False)
nc.case = case
nc.author.add(request.user)
# Save instance before modifying many-to-many relations
nc.save()
form.save_m2m()
nc.author.add(request.user)
# Ensure the case reflects the newly created NormalCase when rendering
try: