diff --git a/src/registrar/templates/domain_users.html b/src/registrar/templates/domain_users.html index a9a7c0427..4a0d25625 100644 --- a/src/registrar/templates/domain_users.html +++ b/src/registrar/templates/domain_users.html @@ -54,20 +54,36 @@ > Remove - -
-
- {% with email=permission.user.email|force_escape domain_name=domain.name|force_escape %} - {% include 'includes/modal.html' with modal_heading="Are you sure you want to remove <"|add:email|add:">?"|safe modal_description="<"|add:email|add:"> will no longer be able to manage the domain "|add:domain_name|add:"."|safe modal_button=modal_button|safe %} - {% endwith %} -
-
+ > +
+ {% with domain_name=domain.name|force_escape %} + {% include 'includes/modal.html' with modal_heading="Are you sure you want to remove yourself as a domain manager for "|add:domain_name|add:"?"|safe modal_description="You will no longer be able to manage the domain "|add:domain_name|add:"."|safe modal_button=modal_button_self|safe %} + {% endwith %} +
+ + {% else %} +
+
+ {% with email=permission.user.email|force_escape domain_name=domain.name|force_escape %} + {% include 'includes/modal.html' with modal_heading="Are you sure you want to remove <"|add:email|add:">?"|safe modal_description="<"|add:email|add:"> will no longer be able to manage the domain "|add:domain_name|add:"."|safe modal_button=modal_button|safe %} + {% endwith %} +
+
+ {% endif %} {% endif %} diff --git a/src/registrar/views/domain.py b/src/registrar/views/domain.py index c8aa7083f..b55d490ce 100644 --- a/src/registrar/views/domain.py +++ b/src/registrar/views/domain.py @@ -6,6 +6,7 @@ inherit from `DomainPermissionView` (or DomainInvitationPermissionDeleteView). """ import logging +from typing import List from django.contrib import messages from django.contrib.messages.views import SuccessMessageMixin @@ -630,6 +631,20 @@ class DomainUsersView(DomainBaseView): """The initial value for the form (which is a formset here).""" context = super().get_context_data(**kwargs) + # Add conditionals to the context (such as "can_delete_users") + context = self._add_booleans_to_context(context) + + # Add modal buttons to the context (such as for delete) + context = self._add_modal_buttons_to_context(context) + + # Get the email of the current user + context["current_user_email"] = self.request.user.email + + return context + + def _add_booleans_to_context(self, context): + + # Determine if the current user can delete managers domain_pk = None can_delete_users = False if self.kwargs is not None and "pk" in self.kwargs: @@ -639,17 +654,45 @@ class DomainUsersView(DomainBaseView): 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 = ( - '' - ) + return context + def _add_modal_buttons_to_context(self, context): + """Adds modal buttons (and their HTML) to the context""" + # Create HTML for the modal button + modal_button = self._create_modal_button_html( + button_name="delete_domain_manager", + button_text_content="Yes, remove domain manager", + classes=["usa-button", "usa-button--secondary"] + ) context["modal_button"] = modal_button + # Create HTML for the modal button when deleting yourself + modal_button_self= self._create_modal_button_html( + button_name="delete_domain_manager_self", + button_text_content="Yes, remove myself", + classes=["usa-button", "usa-button--secondary"] + ) + context["modal_button_self"] = modal_button_self + return context + def _create_modal_button_html(self, button_name: str, button_text_content: str, classes: List[str] | str): + """Template for modal submit buttons""" + + if isinstance(classes, list): + class_list = " ".join(classes) + elif isinstance(classes, str): + class_list = classes + + html_class = f'class="{class_list}"' if class_list else None + + modal_button = ( + '' + ) + return modal_button + class DomainAddUserView(DomainFormBaseView): """Inside of a domain's user management, a form for adding users. @@ -779,12 +822,30 @@ class DomainDeleteUserView(UserDomainRolePermissionDeleteView): def get_success_url(self): return reverse("domain-users", kwargs={"pk": self.object.domain.id}) - def get_success_message(self, cleaned_data): - return f"Successfully removed manager for {self.object.user.email}." + def get_success_message(self, delete_self = False): + if delete_self: + message = f"You are no longer managing the domain {self.object.domain}." + else: + message = f"Removed {self.object.user.email} as a manager for this domain." + return message def form_valid(self, form): """Delete the specified user on this domain.""" super().form_valid(form) - messages.success(self.request, f"Successfully removed manager for {self.object.user.email}.") - return redirect(self.get_success_url()) \ No newline at end of file + # Is the user deleting themselves? If so, display a different message + delete_self = self.request.user.email == self.object.user.email + + # Add a success message + messages.success(self.request, self.get_success_message(delete_self)) + + return redirect(self.get_success_url()) + + def post(self, request, *args, **kwargs): + response = super().post(request, *args, **kwargs) + + # If the user is deleting themselves, redirect to home + if self.request.user.email == self.object.user.email: + return redirect(reverse("home")) + + return response \ No newline at end of file