Add conditional logic

This commit is contained in:
zandercymatics 2024-01-08 09:59:24 -07:00
parent 01a6de2392
commit 4c27186545
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
2 changed files with 38 additions and 25 deletions

View file

@ -31,7 +31,9 @@
<tr>
<th data-sortable scope="col" role="columnheader">Email</th>
<th data-sortable scope="col" role="columnheader">Role</th>
<th scope="col" role="columnheader"><span class="sr-only">Action</span></th>
{% if can_delete_users %}
<th scope="col" role="columnheader"><span class="sr-only">Action</span></th>
{% endif %}
</tr>
</thead>
<tbody>
@ -41,31 +43,33 @@
{{ permission.user.email }}
</th>
<td data-label="Role">{{ permission.role|title }}</td>
<td class="shift-action-button">
<a
id="button-toggle-user-alert-{{ forloop.counter }}"
href="#toggle-user-alert-{{ forloop.counter }}"
class="usa-button--unstyled"
aria-controls="toggle-user-alert-{{ forloop.counter }}"
data-open-modal
>
Remove
</a>
{% if can_delete_users %}
<td class="shift-action-button">
<a
id="button-toggle-user-alert-{{ forloop.counter }}"
href="#toggle-user-alert-{{ forloop.counter }}"
class="usa-button--unstyled"
aria-controls="toggle-user-alert-{{ forloop.counter }}"
data-open-modal
>
Remove
</a>
<div
class="usa-modal"
id="toggle-user-alert-{{ forloop.counter }}"
aria-labelledby="Are you sure you want to continue?"
aria-describedby="User will be removed"
data-force-action
>
<form method="POST" action="{% url "domain-user-delete" pk=domain.id user_pk=permission.user.id %}">
{% with heading="Are you sure you want to remove <"|add:permission.user.email|add:">?" %}
{% include 'includes/modal.html' with modal_heading=heading modal_description="<"|add:permission.user.email|add:"> will no longer be able to manage the domain "|add:domain.name|add:"." modal_button=modal_button|safe %}
{% endwith %}
</form>
</div>
</td>
<div
class="usa-modal"
id="toggle-user-alert-{{ forloop.counter }}"
aria-labelledby="Are you sure you want to continue?"
aria-describedby="User will be removed"
data-force-action
>
<form method="POST" action="{% url "domain-user-delete" pk=domain.id user_pk=permission.user.id %}">
{% with heading="Are you sure you want to remove <"|add:permission.user.email|add:">?" %}
{% include 'includes/modal.html' with modal_heading=heading modal_description="<"|add:permission.user.email|add:"> will no longer be able to manage the domain "|add:domain.name|add:"." modal_button=modal_button|safe %}
{% endwith %}
</form>
</div>
</td>
{% endif %}
</tr>
{% endfor %}
</tbody>

View file

@ -630,6 +630,15 @@ class DomainUsersView(DomainBaseView):
"""The initial value for the form (which is a formset here)."""
context = super().get_context_data(**kwargs)
domain_pk = None
can_delete_users = False
if self.kwargs is not None and "pk" in self.kwargs:
domain_pk = self.kwargs["pk"]
# Prevent the end user from deleting themselves as a manager if they are the
# only manager that exists on a domain.
can_delete_users = UserDomainRole.objects.filter(domain__id=domain_pk).count() > 1
context["can_delete_users"] = can_delete_users
# Create HTML for the modal button
modal_button = (
'<button type="submit" '