mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-23 11:16:07 +02:00
Override error in certain contexts
This commit is contained in:
parent
40502e909e
commit
d4bcd03a96
2 changed files with 32 additions and 4 deletions
|
@ -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):
|
||||
"""
|
||||
|
|
|
@ -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"
|
||||
)
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue