Skip pricing updates when importing data (#5026)

* Skip pricing updates when importing data

- Depending on migration state, pricing table might not exist
- post-save hooks can call update_pricing
- So, ignore if running data migration or import

* Typo fix
This commit is contained in:
Oliver 2023-06-13 12:40:21 +10:00 committed by GitHub
parent 9117c2234b
commit 98bddd32d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 0 deletions

View File

@ -13,6 +13,11 @@ def isImportingData():
return 'loaddata' in sys.argv
def isRunningMigrations():
"""Return True if the database is currently running migrations."""
return 'migrate' in sys.argv or 'makemigrations' in sys.argv
def canAppAccessDatabase(allow_test: bool = False, allow_plugins: bool = False, allow_shell: bool = False):
"""Returns True if the apps.py file can access database records.

View File

@ -2363,6 +2363,16 @@ class PartPricing(common.models.MetaMixin):
def schedule_for_update(self, counter: int = 0):
"""Schedule this pricing to be updated"""
import InvenTree.ready
# If importing data, skip pricing update
if InvenTree.ready.isImportingData():
return
# If running data migrations, skip pricing update
if InvenTree.ready.isRunningMigrations():
return
if not self.part or not self.part.pk or not Part.objects.filter(pk=self.part.pk).exists():
logger.warning("Referenced part instance does not exist - skipping pricing update.")
return
@ -2415,6 +2425,14 @@ class PartPricing(common.models.MetaMixin):
def update_pricing(self, counter: int = 0, cascade: bool = True):
"""Recalculate all cost data for the referenced Part instance"""
# If importing data, skip pricing update
if InvenTree.ready.isImportingData():
return
# If running data migrations, skip pricing update
if InvenTree.ready.isRunningMigrations():
return
if self.pk is not None:
try:
self.refresh_from_db()