Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3464: Update domain requests fixtures to cumulatively create objects #3593

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions src/registrar/fixtures/fixtures_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,35 +315,42 @@ def load(cls):
cls._create_domain_requests(users)

@classmethod
def _create_domain_requests(cls, users): # noqa: C901
def _create_domain_requests(cls, users, total_requests=None): # noqa: C901
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@allly-b I added the stretch param mentioned in the ticket and wanted to check if there is any use case for this. If not, I can remove it to clean up any code that isn't likely to be used.

"""Creates DomainRequests given a list of users."""
total_domain_requests_to_make = len(users) # 100000

# Check if the database is already populated with the desired
# number of entries.
# (Prevents re-adding more entries to an already populated database,
# which happens when restarting Docker src)
domain_requests_already_made = DomainRequest.objects.count()
total_existing_requests = DomainRequest.objects.count()

domain_requests_to_create = []
if domain_requests_already_made < total_domain_requests_to_make:
for user in users:
for request_data in cls.DOMAINREQUESTS:
# Prepare DomainRequest objects
try:
domain_request = DomainRequest(
creator=user,
organization_name=request_data["organization_name"],
)
cls._set_non_foreign_key_fields(domain_request, request_data)
cls._set_foreign_key_fields(domain_request, request_data, user)
domain_requests_to_create.append(domain_request)
except Exception as e:
logger.warning(e)

num_additional_requests_to_make = (
total_domain_requests_to_make - domain_requests_already_made - len(domain_requests_to_create)
)
if total_requests and total_requests <= total_existing_requests:
total_domain_requests_to_make = total_requests - total_existing_requests
if total_domain_requests_to_make >= 0:
total_domains_to_delete = total_domain_requests_to_make
DomainRequest.objects.filter(
id__in=list(DomainRequest.objects.values_list("pk", flat=True)[:total_domains_to_delete])
).delete()
if total_domain_requests_to_make == 0:
return

for user in users:
for request_data in cls.DOMAINREQUESTS:
# Prepare DomainRequest objects
try:
domain_request = DomainRequest(
creator=user,
organization_name=request_data["organization_name"],
)
cls._set_non_foreign_key_fields(domain_request, request_data)
cls._set_foreign_key_fields(domain_request, request_data, user)
domain_requests_to_create.append(domain_request)
except Exception as e:
logger.warning(e)

num_additional_requests_to_make = total_domain_requests_to_make - len(domain_requests_to_create)
if num_additional_requests_to_make > 0:
for _ in range(num_additional_requests_to_make):
random_user = random.choice(users) # nosec
Expand Down
Loading