From d4bcd03a96eedab83c9d05fa938c6963625c77b6 Mon Sep 17 00:00:00 2001 From: zandercymatics <141044360+zandercymatics@users.noreply.github.com> Date: Wed, 5 Mar 2025 10:23:37 -0700 Subject: [PATCH] Override error in certain contexts --- src/registrar/forms/portfolio.py | 23 +++++++++++++++++++ .../models/utility/portfolio_helper.py | 13 +++++++---- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/registrar/forms/portfolio.py b/src/registrar/forms/portfolio.py index 5aef09389..f0d41af3b 100644 --- a/src/registrar/forms/portfolio.py +++ b/src/registrar/forms/portfolio.py @@ -299,6 +299,29 @@ class BasePortfolioMemberForm(forms.ModelForm): cleaned_data["additional_permissions"] = list(additional_permissions - role_permissions) return cleaned_data + + def _post_clean(self): + """ + Override _post_clean to customize model validation errors. + This runs after form clean is complete, but before the errors are displayed. + """ + try: + super()._post_clean() + self.instance.clean() + except forms.ValidationError as e: + override_error = False + if hasattr(e, "code"): + field = "email" if "email" in self.fields else None + if e.code == "has_existing_permissions": + self.add_error(field, "This user is already a member of another .gov organization.") + override_error = True + elif e.code == "has_existing_invitations": + self.add_error(field, "This user has already been invited to another .gov organization.") + override_error = True + + if override_error: + if "__all__" in self._errors: + del self._errors["__all__"] def map_instance_to_initial(self): """ diff --git a/src/registrar/models/utility/portfolio_helper.py b/src/registrar/models/utility/portfolio_helper.py index e94733fb6..67b0b8259 100644 --- a/src/registrar/models/utility/portfolio_helper.py +++ b/src/registrar/models/utility/portfolio_helper.py @@ -285,7 +285,8 @@ def validate_user_portfolio_permission(user_portfolio_permission): if existing_permissions.exists(): raise ValidationError( "This user is already assigned to a portfolio. " - "Based on current waffle flag settings, users cannot be assigned to multiple portfolios." + "Based on current waffle flag settings, users cannot be assigned to multiple portfolios.", + code="has_existing_permissions" ) existing_invitations = PortfolioInvitation.objects.filter(email=user_portfolio_permission.user.email).exclude( @@ -295,7 +296,8 @@ def validate_user_portfolio_permission(user_portfolio_permission): if existing_invitations.exists(): raise ValidationError( "This user is already assigned to a portfolio invitation. " - "Based on current waffle flag settings, users cannot be assigned to multiple portfolios." + "Based on current waffle flag settings, users cannot be assigned to multiple portfolios.", + code="has_existing_invitations" ) @@ -343,6 +345,7 @@ def validate_portfolio_invitation(portfolio_invitation): # == Validate the multiple_porfolios flag. == # user = User.objects.filter(email=portfolio_invitation.email).first() + # If user returns None, then we check for global assignment of multiple_portfolios. # Otherwise we just check on the user. if not flag_is_active_for_user(user, "multiple_portfolios"): @@ -355,13 +358,15 @@ def validate_portfolio_invitation(portfolio_invitation): if existing_permissions.exists(): raise ValidationError( "This user is already assigned to a portfolio. " - "Based on current waffle flag settings, users cannot be assigned to multiple portfolios." + "Based on current waffle flag settings, users cannot be assigned to multiple portfolios.", + code="has_existing_permissions" ) if existing_invitations.exists(): raise ValidationError( "This user is already assigned to a portfolio invitation. " - "Based on current waffle flag settings, users cannot be assigned to multiple portfolios." + "Based on current waffle flag settings, users cannot be assigned to multiple portfolios.", + code="has_existing_invitations" )