unique requested domains, portfolio appropriate suborg

This commit is contained in:
Rachid Mrad 2024-11-18 12:19:47 -05:00
parent 9565731a93
commit d032d76c75
No known key found for this signature in database

View file

@ -6,8 +6,10 @@ from faker import Faker
from django.db import transaction from django.db import transaction
from registrar.fixtures.fixtures_portfolios import PortfolioFixture from registrar.fixtures.fixtures_portfolios import PortfolioFixture
from registrar.fixtures.fixtures_suborganizations import SuborganizationFixture
from registrar.fixtures.fixtures_users import UserFixture from registrar.fixtures.fixtures_users import UserFixture
from registrar.models import User, DomainRequest, DraftDomain, Contact, Website, FederalAgency from registrar.models import User, DomainRequest, DraftDomain, Contact, Website, FederalAgency
from registrar.models.domain import Domain
from registrar.models.portfolio import Portfolio from registrar.models.portfolio import Portfolio
from registrar.models.suborganization import Suborganization from registrar.models.suborganization import Suborganization
@ -189,7 +191,13 @@ class DomainRequestFixture:
if not request.requested_domain: if not request.requested_domain:
if "requested_domain" in request_dict and request_dict["requested_domain"] is not None: if "requested_domain" in request_dict and request_dict["requested_domain"] is not None:
return DraftDomain.objects.get_or_create(name=request_dict["requested_domain"])[0] return DraftDomain.objects.get_or_create(name=request_dict["requested_domain"])[0]
return DraftDomain.objects.create(name=cls.fake_dot_gov())
# Generate a unique fake domain
# This will help us avoid domain already approved log warnings
while True:
fake_name = cls.fake_dot_gov()
if not Domain.objects.filter(name=fake_name).exists():
return DraftDomain.objects.create(name=fake_name)
return request.requested_domain return request.requested_domain
@classmethod @classmethod
@ -213,7 +221,7 @@ class DomainRequestFixture:
if not request.sub_organization: if not request.sub_organization:
if "sub_organization" in request_dict and request_dict["sub_organization"] is not None: if "sub_organization" in request_dict and request_dict["sub_organization"] is not None:
return Suborganization.objects.get_or_create(name=request_dict["sub_organization"])[0] return Suborganization.objects.get_or_create(name=request_dict["sub_organization"])[0]
return cls._get_random_sub_organization() return cls._get_random_sub_organization(request)
return request.sub_organization return request.sub_organization
@classmethod @classmethod
@ -226,12 +234,21 @@ class DomainRequestFixture:
except Exception as e: except Exception as e:
logger.warning(f"Expected fixture portfolio, did not find it: {e}") logger.warning(f"Expected fixture portfolio, did not find it: {e}")
return None return None
@classmethod @classmethod
def _get_random_sub_organization(cls): def _get_random_sub_organization(cls, request):
try: try:
suborg_options = [Suborganization.objects.first(), Suborganization.objects.last()] # Filter Suborganizations by the request's portfolio
return random.choice(suborg_options) # nosec portfolio_suborganizations = Suborganization.objects.filter(portfolio=request.portfolio)
# Assuming SuborganizationFixture.SUBORGS is a list of dictionaries with a "name" key
suborganization_names = [suborg["name"] for suborg in SuborganizationFixture.SUBORGS]
# Further filter by names in suborganization_names
suborganization_options = portfolio_suborganizations.filter(name__in=suborganization_names)
# Randomly choose one if any exist
return random.choice(suborganization_options) if suborganization_options.exists() else None # nosec
except Exception as e: except Exception as e:
logger.warning(f"Expected fixture sub_organization, did not find it: {e}") logger.warning(f"Expected fixture sub_organization, did not find it: {e}")
return None return None
@ -273,6 +290,9 @@ class DomainRequestFixture:
# Lumped under .atomic to ensure we don't make redundant DB calls. # Lumped under .atomic to ensure we don't make redundant DB calls.
# This bundles them all together, and then saves it in a single call. # This bundles them all together, and then saves it in a single call.
# The atomic block will cause the code to stop executing if one instance in the
# nested iteration fails, which will cause an early exit and make it hard to debug.
# Comment out with transaction.atomic() when debugging.
with transaction.atomic(): with transaction.atomic():
try: try:
# Get the usernames of users created in the UserFixture # Get the usernames of users created in the UserFixture