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:
+17
-1
@@ -298,6 +298,22 @@ class NormalCaseForm(ModelForm):
|
|||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*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()
|
self.helper = FormHelper()
|
||||||
# Do not render a <form> tag from crispy here because the template
|
# Do not render a <form> tag from crispy here because the template
|
||||||
# already provides the surrounding form element (we inject this
|
# already provides the surrounding form element (we inject this
|
||||||
@@ -315,7 +331,7 @@ class NormalCaseForm(ModelForm):
|
|||||||
css_class="row",
|
css_class="row",
|
||||||
),
|
),
|
||||||
Field("notes"),
|
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):
|
def save(self, commit=True):
|
||||||
|
|||||||
@@ -32,18 +32,18 @@
|
|||||||
{% for normal in page_obj.object_list %}
|
{% for normal in page_obj.object_list %}
|
||||||
<div class="list-group-item d-flex justify-content-between align-items-start">
|
<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 %}">
|
<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">
|
<div class="d-flex w-100 justify-content-between">
|
||||||
<h5 class="mb-1">{{ normal.case.title }}</h5>
|
<h5 class="mb-1">{{ normal.case.title }}</h5>
|
||||||
<small class="text-muted">{{ normal.added_date|date:"Y-m-d" }}</small>
|
<small class="text-muted">{{ normal.added_date|date:"Y-m-d" }}</small>
|
||||||
</div>
|
</div>
|
||||||
<p class="mb-1">
|
<p class="mb-1">
|
||||||
Age: {{ normal.display_age }}
|
Age: {{ normal.display_age }}
|
||||||
{% if normal.examination %} • Exam: {{ normal.examination }}{% endif %}
|
{% if normal.examination %} • Exam: {{ normal.examination }}{% endif %}
|
||||||
{% if normal.modality %} • Modality: {{ normal.modality }}{% endif %}
|
{% if normal.modality %} • Modality: {{ normal.modality }}{% endif %}
|
||||||
</p>
|
</p>
|
||||||
{% if normal.notes %}
|
{% if normal.notes %}
|
||||||
<p class="mb-0 text-muted">{{ normal.notes }}</p>
|
<p class="mb-0 text-muted">{{ normal.notes }}</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</a>
|
</a>
|
||||||
<div class="ms-2">
|
<div class="ms-2">
|
||||||
{% if request.user.is_superuser or request.user in normal.author.all %}
|
{% if request.user.is_superuser or request.user in normal.author.all %}
|
||||||
|
|||||||
@@ -15,8 +15,7 @@
|
|||||||
</button>
|
</button>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{# Allow NormalCase authors (or atlas editors/superusers) to edit the Normal metadata #}
|
{% if request.user.is_superuser or can_edit or request.user in case.normal_case.author.all %}
|
||||||
{% if case.normal_case and (request.user.is_superuser or request.user in case.normal_case.author.all or can_edit) %}
|
|
||||||
<button class="btn btn-sm btn-outline-secondary ms-2"
|
<button class="btn btn-sm btn-outline-secondary ms-2"
|
||||||
hx-get="{% url 'atlas:case_normal_edit' case.normal_case.pk %}"
|
hx-get="{% url 'atlas:case_normal_edit' case.normal_case.pk %}"
|
||||||
hx-target="body"
|
hx-target="body"
|
||||||
|
|||||||
+3
-1
@@ -847,8 +847,10 @@ def create_case_normal(request, pk):
|
|||||||
logger.debug("NormalCaseForm.cleaned_data: {}", form.cleaned_data)
|
logger.debug("NormalCaseForm.cleaned_data: {}", form.cleaned_data)
|
||||||
nc = form.save(commit=False)
|
nc = form.save(commit=False)
|
||||||
nc.case = case
|
nc.case = case
|
||||||
nc.author.add(request.user)
|
# Save instance before modifying many-to-many relations
|
||||||
nc.save()
|
nc.save()
|
||||||
|
form.save_m2m()
|
||||||
|
nc.author.add(request.user)
|
||||||
|
|
||||||
# Ensure the case reflects the newly created NormalCase when rendering
|
# Ensure the case reflects the newly created NormalCase when rendering
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user