mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-26 04:28:39 +02:00
Remove transaction.atomic() from fixtures
This commit is contained in:
parent
7c613a01f8
commit
c2eacbfcdb
5 changed files with 26 additions and 42 deletions
|
@ -30,12 +30,11 @@ class DomainFixture(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.
|
||||||
try:
|
try:
|
||||||
with transaction.atomic():
|
# Get the usernames of users created in the UserFixture
|
||||||
# Get the usernames of users created in the UserFixture
|
created_usernames = [user_data["username"] for user_data in UserFixture.ADMINS + UserFixture.STAFF]
|
||||||
created_usernames = [user_data["username"] for user_data in UserFixture.ADMINS + UserFixture.STAFF]
|
|
||||||
|
|
||||||
# Filter users to only include those created by the fixture
|
# Filter users to only include those created by the fixture
|
||||||
users = list(User.objects.filter(username__in=created_usernames))
|
users = list(User.objects.filter(username__in=created_usernames))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(e)
|
logger.warning(e)
|
||||||
return
|
return
|
||||||
|
|
|
@ -84,12 +84,8 @@ class PortfolioFixture:
|
||||||
def load(cls):
|
def load(cls):
|
||||||
"""Creates portfolios."""
|
"""Creates portfolios."""
|
||||||
logger.info("Going to load %s portfolios" % len(cls.PORTFOLIOS))
|
logger.info("Going to load %s portfolios" % len(cls.PORTFOLIOS))
|
||||||
|
|
||||||
# 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.
|
|
||||||
try:
|
try:
|
||||||
with transaction.atomic():
|
user = User.objects.all().last()
|
||||||
user = User.objects.all().last()
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(e)
|
logger.warning(e)
|
||||||
return
|
return
|
||||||
|
@ -106,14 +102,13 @@ class PortfolioFixture:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with transaction.atomic():
|
portfolio = Portfolio(
|
||||||
portfolio = Portfolio(
|
creator=user,
|
||||||
creator=user,
|
organization_name=portfolio_data["organization_name"],
|
||||||
organization_name=portfolio_data["organization_name"],
|
)
|
||||||
)
|
cls._set_non_foreign_key_fields(portfolio, portfolio_data)
|
||||||
cls._set_non_foreign_key_fields(portfolio, portfolio_data)
|
cls._set_foreign_key_fields(portfolio, portfolio_data, user)
|
||||||
cls._set_foreign_key_fields(portfolio, portfolio_data, user)
|
portfolios_to_create.append(portfolio)
|
||||||
portfolios_to_create.append(portfolio)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(e)
|
logger.warning(e)
|
||||||
|
|
||||||
|
|
|
@ -303,19 +303,12 @@ class DomainRequestFixture:
|
||||||
def load(cls):
|
def load(cls):
|
||||||
"""Creates domain requests for each user in the database."""
|
"""Creates domain requests for each user in the database."""
|
||||||
logger.info("Going to load %s domain requests" % len(cls.DOMAINREQUESTS))
|
logger.info("Going to load %s domain requests" % len(cls.DOMAINREQUESTS))
|
||||||
|
|
||||||
# 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.
|
|
||||||
# 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.
|
|
||||||
try:
|
try:
|
||||||
with transaction.atomic():
|
# Get the usernames of users created in the UserFixture
|
||||||
# Get the usernames of users created in the UserFixture
|
created_usernames = [user_data["username"] for user_data in UserFixture.ADMINS + UserFixture.STAFF]
|
||||||
created_usernames = [user_data["username"] for user_data in UserFixture.ADMINS + UserFixture.STAFF]
|
|
||||||
|
|
||||||
# Filter users to only include those created by the fixture
|
# Filter users to only include those created by the fixture
|
||||||
users = list(User.objects.filter(username__in=created_usernames))
|
users = list(User.objects.filter(username__in=created_usernames))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(e)
|
logger.warning(e)
|
||||||
return
|
return
|
||||||
|
|
|
@ -34,14 +34,12 @@ class SuborganizationFixture:
|
||||||
def load(cls):
|
def load(cls):
|
||||||
"""Creates suborganizations."""
|
"""Creates suborganizations."""
|
||||||
logger.info(f"Going to load {len(cls.SUBORGS)} suborgs")
|
logger.info(f"Going to load {len(cls.SUBORGS)} suborgs")
|
||||||
|
portfolios = cls._get_portfolios()
|
||||||
|
if not portfolios:
|
||||||
|
return
|
||||||
|
|
||||||
with transaction.atomic():
|
suborgs_to_create = cls._prepare_suborgs_to_create(portfolios)
|
||||||
portfolios = cls._get_portfolios()
|
cls._bulk_create_suborgs(suborgs_to_create)
|
||||||
if not portfolios:
|
|
||||||
return
|
|
||||||
|
|
||||||
suborgs_to_create = cls._prepare_suborgs_to_create(portfolios)
|
|
||||||
cls._bulk_create_suborgs(suborgs_to_create)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _get_portfolios(cls):
|
def _get_portfolios(cls):
|
||||||
|
|
|
@ -430,10 +430,9 @@ class UserFixture:
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def load(cls):
|
def load(cls):
|
||||||
with transaction.atomic():
|
cls.load_users(cls.ADMINS, "full_access_group", are_superusers=True)
|
||||||
cls.load_users(cls.ADMINS, "full_access_group", are_superusers=True)
|
cls.load_users(cls.STAFF, "cisa_analysts_group")
|
||||||
cls.load_users(cls.STAFF, "cisa_analysts_group")
|
|
||||||
|
|
||||||
# Combine ADMINS and STAFF lists
|
# Combine ADMINS and STAFF lists
|
||||||
all_users = cls.ADMINS + cls.STAFF
|
all_users = cls.ADMINS + cls.STAFF
|
||||||
cls.load_allowed_emails(cls, all_users, additional_emails=cls.ADDITIONAL_ALLOWED_EMAILS)
|
cls.load_allowed_emails(cls, all_users, additional_emails=cls.ADDITIONAL_ALLOWED_EMAILS)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue