mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-27 13:06:30 +02:00
refactored method to make more readable
This commit is contained in:
parent
6d0b9d1a9e
commit
9dd357c81f
1 changed files with 47 additions and 30 deletions
|
@ -119,63 +119,80 @@ class PortfolioMemberDeleteView(PortfolioMemberPermission, View):
|
||||||
"""
|
"""
|
||||||
portfolio_member_permission = get_object_or_404(UserPortfolioPermission, pk=pk)
|
portfolio_member_permission = get_object_or_404(UserPortfolioPermission, pk=pk)
|
||||||
member = portfolio_member_permission.user
|
member = portfolio_member_permission.user
|
||||||
|
portfolio = portfolio_member_permission.portfolio
|
||||||
|
|
||||||
|
# Validate if the member can be removed
|
||||||
|
error_message = self._validate_member_removal(request, member, portfolio)
|
||||||
|
if error_message:
|
||||||
|
return self._handle_error_response(request, error_message, pk)
|
||||||
|
|
||||||
|
# Attempt to send notification emails
|
||||||
|
self._send_removal_notifications(request, portfolio_member_permission)
|
||||||
|
|
||||||
|
# Passed all error conditions, proceed with deletion
|
||||||
|
portfolio_member_permission.delete()
|
||||||
|
|
||||||
|
# Return success response
|
||||||
|
return self._handle_success_response(request, member.email)
|
||||||
|
|
||||||
|
def _validate_member_removal(self, request, member, portfolio):
|
||||||
|
"""
|
||||||
|
Check whether the member can be removed from the portfolio.
|
||||||
|
Returns an error message if removal is not allowed; otherwise, returns None.
|
||||||
|
"""
|
||||||
active_requests_count = member.get_active_requests_count_in_portfolio(request)
|
active_requests_count = member.get_active_requests_count_in_portfolio(request)
|
||||||
|
|
||||||
support_url = "https://get.gov/contact/"
|
support_url = "https://get.gov/contact/"
|
||||||
|
|
||||||
error_message = ""
|
|
||||||
|
|
||||||
if active_requests_count > 0:
|
if active_requests_count > 0:
|
||||||
# If they have any in progress requests
|
return mark_safe( # nosec
|
||||||
error_message = mark_safe( # nosec
|
|
||||||
"This member can't be removed from the organization because they have an active domain request. "
|
"This member can't be removed from the organization because they have an active domain request. "
|
||||||
f"Please <a class='usa-link' href='{support_url}' target='_blank'>contact us</a> to remove this member."
|
f"Please <a class='usa-link' href='{support_url}' target='_blank'>contact us</a> to remove this member."
|
||||||
)
|
)
|
||||||
elif member.is_only_admin_of_portfolio(portfolio_member_permission.portfolio):
|
if member.is_only_admin_of_portfolio(portfolio):
|
||||||
# If they are the last manager of a domain
|
return (
|
||||||
error_message = (
|
|
||||||
"There must be at least one admin in your organization. Give another member admin "
|
"There must be at least one admin in your organization. Give another member admin "
|
||||||
"permissions, make sure they log into the registrar, and then remove this member."
|
"permissions, make sure they log into the registrar, and then remove this member."
|
||||||
)
|
)
|
||||||
|
return None
|
||||||
|
|
||||||
# From the Members Table page Else the Member Page
|
def _handle_error_response(self, request, error_message, pk):
|
||||||
if error_message:
|
"""
|
||||||
|
Return an error response (JSON or redirect with messages).
|
||||||
|
"""
|
||||||
if request.headers.get("X-Requested-With") == "XMLHttpRequest":
|
if request.headers.get("X-Requested-With") == "XMLHttpRequest":
|
||||||
return JsonResponse(
|
return JsonResponse({"error": error_message}, status=400)
|
||||||
{"error": error_message},
|
|
||||||
status=400,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
messages.error(request, error_message)
|
messages.error(request, error_message)
|
||||||
return redirect(reverse("member", kwargs={"pk": pk}))
|
return redirect(reverse("member", kwargs={"pk": pk}))
|
||||||
|
|
||||||
|
def _send_removal_notifications(self, request, portfolio_member_permission):
|
||||||
|
"""
|
||||||
|
Attempt to send notification emails about the member's removal.
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
# if member being removed is an admin
|
# Notify other portfolio admins if removing an admin
|
||||||
if UserPortfolioRoleChoices.ORGANIZATION_ADMIN in portfolio_member_permission.roles:
|
if UserPortfolioRoleChoices.ORGANIZATION_ADMIN in portfolio_member_permission.roles:
|
||||||
# attempt to send notification emails of the removal to other portfolio admins
|
|
||||||
if not send_portfolio_admin_removal_emails(
|
if not send_portfolio_admin_removal_emails(
|
||||||
email=portfolio_member_permission.user.email,
|
email=portfolio_member_permission.user.email,
|
||||||
requestor=request.user,
|
requestor=request.user,
|
||||||
portfolio=portfolio_member_permission.portfolio,
|
portfolio=portfolio_member_permission.portfolio,
|
||||||
):
|
):
|
||||||
messages.warning(request, "Could not send email notification to existing organization admins.")
|
messages.warning(request, "Could not send email notification to existing organization admins.")
|
||||||
# send notification email to member being removed
|
|
||||||
|
# Notify the member being removed
|
||||||
if not send_portfolio_member_permission_remove_email(
|
if not send_portfolio_member_permission_remove_email(
|
||||||
requestor=request.user, permissions=portfolio_member_permission
|
requestor=request.user, permissions=portfolio_member_permission
|
||||||
):
|
):
|
||||||
messages.warning(request, f"Could not send email notification to {member.email}")
|
messages.warning(request, f"Could not send email notification to {portfolio_member_permission.user.email}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self._handle_exceptions(e)
|
self._handle_exceptions(e)
|
||||||
|
|
||||||
# passed all error conditions
|
def _handle_success_response(self, request, member_email):
|
||||||
portfolio_member_permission.delete()
|
"""
|
||||||
|
Return a success response (JSON or redirect with messages).
|
||||||
# From the Members Table page Else the Member Page
|
"""
|
||||||
success_message = f"You've removed {member.email} from the organization."
|
success_message = f"You've removed {member_email} from the organization."
|
||||||
if request.headers.get("X-Requested-With") == "XMLHttpRequest":
|
if request.headers.get("X-Requested-With") == "XMLHttpRequest":
|
||||||
return JsonResponse({"success": success_message}, status=200)
|
return JsonResponse({"success": success_message}, status=200)
|
||||||
else:
|
|
||||||
messages.success(request, success_message)
|
messages.success(request, success_message)
|
||||||
return redirect(reverse("members"))
|
return redirect(reverse("members"))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue