From 98bddd32d0474156f7dc046ba80ba0c8947f8e08 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 13 Jun 2023 12:40:21 +1000 Subject: [PATCH] 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 --- InvenTree/InvenTree/ready.py | 5 +++++ InvenTree/part/models.py | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/InvenTree/InvenTree/ready.py b/InvenTree/InvenTree/ready.py index e6a4ec9ae2..6e16a3b471 100644 --- a/InvenTree/InvenTree/ready.py +++ b/InvenTree/InvenTree/ready.py @@ -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. diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index b775960b70..8e65f95e2f 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -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()