Skip to content

Commit

Permalink
Merge pull request #2916 from cisagov/rh/2883-update-agency
Browse files Browse the repository at this point in the history
 #2883: Non Federal Agency Update - [RH]
  • Loading branch information
therealslimhsiehdy authored Oct 15, 2024
2 parents 5e7f246 + 9c46384 commit 8b6d49d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/registrar/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2473,7 +2473,10 @@ def generic_org_type(self, obj):
generic_org_type.admin_order_field = "domain_info__generic_org_type" # type: ignore

def federal_agency(self, obj):
return obj.domain_info.federal_agency if obj.domain_info else None
if obj.domain_info:
return obj.domain_info.federal_agency
else:
return None

federal_agency.admin_order_field = "domain_info__federal_agency" # type: ignore

Expand Down
16 changes: 12 additions & 4 deletions src/registrar/forms/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django import forms
from django.core.validators import MinValueValidator, MaxValueValidator, RegexValidator, MaxLengthValidator
from django.forms import formset_factory
from registrar.models import DomainRequest
from registrar.models import DomainRequest, FederalAgency
from phonenumber_field.widgets import RegionalPhoneNumberWidget
from registrar.models.suborganization import Suborganization
from registrar.models.utility.domain_helper import DomainHelper
Expand Down Expand Up @@ -535,17 +535,25 @@ def __init__(self, *args, **kwargs):

def save(self, commit=True):
"""Override the save() method of the BaseModelForm."""

if self.has_changed():

# This action should be blocked by the UI, as the text fields are readonly.
# If they get past this point, we forbid it this way.
# This could be malicious, so lets reserve information for the backend only.
if self.is_federal and not self._field_unchanged("federal_agency"):
raise ValueError("federal_agency cannot be modified when the generic_org_type is federal")

if self.is_federal:
if not self._field_unchanged("federal_agency"):
raise ValueError("federal_agency cannot be modified when the generic_org_type is federal")

elif self.is_tribal and not self._field_unchanged("organization_name"):
raise ValueError("organization_name cannot be modified when the generic_org_type is tribal")

super().save()
else: # If this error that means Non-Federal Agency is missing
non_federal_agency_instance = FederalAgency.get_non_federal_agency()
self.instance.federal_agency = non_federal_agency_instance

return super().save(commit=commit)

def _field_unchanged(self, field_name) -> bool:
"""
Expand Down

0 comments on commit 8b6d49d

Please sign in to comment.