From 78b1c7a22bfb2f7ff077554b7d4e5ffb9e1239e9 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 3 Feb 2022 16:03:46 +1100 Subject: [PATCH] Skips some specific steps when importing data - We need to prevent certain operations from running when we are importing data - This is to prevent unique database constraints from being violated - Do not register plugins during data import - Do not launch notification events --- InvenTree/InvenTree/ready.py | 12 +++++++++--- InvenTree/build/tasks.py | 6 ++++++ InvenTree/part/tasks.py | 5 +++++ InvenTree/plugin/apps.py | 21 +++++++++++++-------- 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/InvenTree/InvenTree/ready.py b/InvenTree/InvenTree/ready.py index 7d63861f4b..9f5ad0ea49 100644 --- a/InvenTree/InvenTree/ready.py +++ b/InvenTree/InvenTree/ready.py @@ -6,10 +6,16 @@ def isInTestMode(): Returns True if the database is in testing mode """ - if 'test' in sys.argv: - return True + return 'test' in sys.argv - return False + +def isImportingData(): + """ + Returns True if the database is currently importing data, + e.g. 'loaddata' command is performed + """ + + return 'loaddata' in sys.argv def canAppAccessDatabase(allow_test=False): diff --git a/InvenTree/build/tasks.py b/InvenTree/build/tasks.py index 6fe4be5119..6752bc5501 100644 --- a/InvenTree/build/tasks.py +++ b/InvenTree/build/tasks.py @@ -12,6 +12,8 @@ from allauth.account.models import EmailAddress import build.models import InvenTree.helpers import InvenTree.tasks +from InvenTree.ready import isImportingData + import part.models as part_models @@ -24,6 +26,10 @@ def check_build_stock(build: build.models.Build): and send an email out to any subscribed users if stock is low. """ + # Do not notify if we are importing data + if isImportingData(): + return + # Iterate through each of the parts required for this build lines = [] diff --git a/InvenTree/part/tasks.py b/InvenTree/part/tasks.py index a7c24b385b..a004b8a8cf 100644 --- a/InvenTree/part/tasks.py +++ b/InvenTree/part/tasks.py @@ -13,6 +13,7 @@ from common.models import NotificationEntry import InvenTree.helpers import InvenTree.tasks +from InvenTree.ready import isImportingData import part.models @@ -24,6 +25,10 @@ def notify_low_stock(part: part.models.Part): Notify users who have starred a part when its stock quantity falls below the minimum threshold """ + # Do not notify if we are importing data + if isImportingData(): + return + # Check if we have notified recently... delta = timedelta(days=1) diff --git a/InvenTree/plugin/apps.py b/InvenTree/plugin/apps.py index c297b84ca0..cfc20ec1aa 100644 --- a/InvenTree/plugin/apps.py +++ b/InvenTree/plugin/apps.py @@ -8,6 +8,7 @@ from django.conf import settings from maintenance_mode.core import set_maintenance_mode +from InvenTree.ready import isImportingData from plugin import registry @@ -19,13 +20,17 @@ class PluginAppConfig(AppConfig): def ready(self): if settings.PLUGINS_ENABLED: - logger.info('Loading InvenTree plugins') + + if isImportingData(): + logger.info('Skipping plugin loading for data import') + else: + logger.info('Loading InvenTree plugins') - if not registry.is_loading: - # this is the first startup - registry.collect_plugins() - registry.load_plugins() + if not registry.is_loading: + # this is the first startup + registry.collect_plugins() + registry.load_plugins() - # drop out of maintenance - # makes sure we did not have an error in reloading and maintenance is still active - set_maintenance_mode(False) + # drop out of maintenance + # makes sure we did not have an error in reloading and maintenance is still active + set_maintenance_mode(False)