2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-28 11:36:44 +00:00

Refactor "ready" state into a function

This commit is contained in:
Oliver Walters 2021-04-25 10:52:31 +10:00
parent 6e5fc43105
commit 06afd4d726
8 changed files with 60 additions and 33 deletions

View File

@ -5,6 +5,7 @@ import logging
from django.apps import AppConfig
from django.core.exceptions import AppRegistryNotReady
from InvenTree.ready import canAppAccessDatabase
import InvenTree.tasks
@ -16,7 +17,8 @@ class InvenTreeConfig(AppConfig):
def ready(self):
self.start_background_tasks()
if canAppAccessDatabase():
self.start_background_tasks()
def start_background_tasks(self):

View File

@ -0,0 +1,24 @@
import sys
def canAppAccessDatabase():
"""
Returns True if the apps.py file can access database records.
There are some circumstances where we don't want the ready function in apps.py
to touch the database:
- "flush" command
- "loaddata" command
- "migrate" command
"""
if 'flush' in sys.argv:
return False
if 'loaddata' in sys.argv:
return False
if 'migrate' in sys.argv:
return False
return True

View File

@ -3,11 +3,14 @@ from __future__ import unicode_literals
import os
import logging
from PIL import UnidentifiedImageError
from django.apps import AppConfig
from django.db.utils import OperationalError, ProgrammingError
from django.conf import settings
from PIL import UnidentifiedImageError
from InvenTree.ready import canAppAccessDatabase
logger = logging.getLogger("inventree")
@ -20,7 +23,8 @@ class CompanyConfig(AppConfig):
This function is called whenever the Company app is loaded.
"""
self.generate_company_thumbs()
if canAppAccessDatabase():
self.generate_company_thumbs()
def generate_company_thumbs(self):

View File

@ -1,5 +1,4 @@
import os
import sys
import shutil
import logging
import hashlib
@ -7,6 +6,8 @@ import hashlib
from django.apps import AppConfig
from django.conf import settings
from InvenTree.ready import canAppAccessDatabase
logger = logging.getLogger("inventree")
@ -33,14 +34,7 @@ class LabelConfig(AppConfig):
This function is called whenever the label app is loaded
"""
if 'loaddata' in sys.argv:
"""
In the case we are importing records,
do not automatically copy labels across.
This can cause database conflicts!
"""
pass
else:
if canAppAccessDatabase():
self.create_stock_item_labels()
self.create_stock_location_labels()

View File

@ -9,6 +9,9 @@ from django.conf import settings
from PIL import UnidentifiedImageError
from InvenTree.ready import canAppAccessDatabase
logger = logging.getLogger("inventree")
@ -20,8 +23,9 @@ class PartConfig(AppConfig):
This function is called whenever the Part app is loaded.
"""
self.generate_part_thumbnails()
self.update_trackable_status()
if canAppAccessDatabase():
self.generate_part_thumbnails()
self.update_trackable_status()
def generate_part_thumbnails(self):
"""

View File

@ -1,11 +1,12 @@
import os
import sys
import shutil
import logging
from django.apps import AppConfig
from django.conf import settings
from InvenTree.ready import canAppAccessDatabase
logger = logging.getLogger("inventree")
@ -18,9 +19,7 @@ class ReportConfig(AppConfig):
This function is called whenever the report app is loaded
"""
if 'loaddata' in sys.argv:
pass
else:
if canAppAccessDatabase():
self.create_default_test_reports()
self.create_default_build_reports()

View File

@ -5,21 +5,25 @@ from django.db.utils import OperationalError, ProgrammingError
from django.apps import AppConfig
from InvenTree.ready import canAppAccessDatabase
class UsersConfig(AppConfig):
name = 'users'
def ready(self):
try:
self.assign_permissions()
except (OperationalError, ProgrammingError):
pass
if canAppAccessDatabase():
try:
self.update_owners()
except (OperationalError, ProgrammingError):
pass
try:
self.assign_permissions()
except (OperationalError, ProgrammingError):
pass
try:
self.update_owners()
except (OperationalError, ProgrammingError):
pass
def assign_permissions(self):

View File

@ -13,7 +13,8 @@ from django.dispatch import receiver
from django.db.models.signals import post_save, post_delete
import logging
import sys
from InvenTree.ready import canAppAccessDatabase
logger = logging.getLogger("inventree")
@ -271,12 +272,7 @@ def update_group_roles(group, debug=False):
"""
if 'loaddata' in sys.argv:
"""
In the case that we are importing records,
*do not* update group roles:
This will cause conflicts in the database!
"""
if not canAppAccessDatabase():
return
# List of permissions already associated with this group