mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-28 19:46:46 +00:00
Refactor "ready" state into a function
This commit is contained in:
parent
6e5fc43105
commit
06afd4d726
@ -5,6 +5,7 @@ import logging
|
|||||||
from django.apps import AppConfig
|
from django.apps import AppConfig
|
||||||
from django.core.exceptions import AppRegistryNotReady
|
from django.core.exceptions import AppRegistryNotReady
|
||||||
|
|
||||||
|
from InvenTree.ready import canAppAccessDatabase
|
||||||
import InvenTree.tasks
|
import InvenTree.tasks
|
||||||
|
|
||||||
|
|
||||||
@ -16,7 +17,8 @@ class InvenTreeConfig(AppConfig):
|
|||||||
|
|
||||||
def ready(self):
|
def ready(self):
|
||||||
|
|
||||||
self.start_background_tasks()
|
if canAppAccessDatabase():
|
||||||
|
self.start_background_tasks()
|
||||||
|
|
||||||
def start_background_tasks(self):
|
def start_background_tasks(self):
|
||||||
|
|
||||||
|
24
InvenTree/InvenTree/ready.py
Normal file
24
InvenTree/InvenTree/ready.py
Normal 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
|
@ -3,11 +3,14 @@ from __future__ import unicode_literals
|
|||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from PIL import UnidentifiedImageError
|
||||||
|
|
||||||
from django.apps import AppConfig
|
from django.apps import AppConfig
|
||||||
from django.db.utils import OperationalError, ProgrammingError
|
from django.db.utils import OperationalError, ProgrammingError
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
from PIL import UnidentifiedImageError
|
from InvenTree.ready import canAppAccessDatabase
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger("inventree")
|
logger = logging.getLogger("inventree")
|
||||||
|
|
||||||
@ -20,7 +23,8 @@ class CompanyConfig(AppConfig):
|
|||||||
This function is called whenever the Company app is loaded.
|
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):
|
def generate_company_thumbs(self):
|
||||||
|
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import os
|
import os
|
||||||
import sys
|
|
||||||
import shutil
|
import shutil
|
||||||
import logging
|
import logging
|
||||||
import hashlib
|
import hashlib
|
||||||
@ -7,6 +6,8 @@ import hashlib
|
|||||||
from django.apps import AppConfig
|
from django.apps import AppConfig
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
|
from InvenTree.ready import canAppAccessDatabase
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger("inventree")
|
logger = logging.getLogger("inventree")
|
||||||
|
|
||||||
@ -33,14 +34,7 @@ class LabelConfig(AppConfig):
|
|||||||
This function is called whenever the label app is loaded
|
This function is called whenever the label app is loaded
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if 'loaddata' in sys.argv:
|
if canAppAccessDatabase():
|
||||||
"""
|
|
||||||
In the case we are importing records,
|
|
||||||
do not automatically copy labels across.
|
|
||||||
This can cause database conflicts!
|
|
||||||
"""
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
self.create_stock_item_labels()
|
self.create_stock_item_labels()
|
||||||
self.create_stock_location_labels()
|
self.create_stock_location_labels()
|
||||||
|
|
||||||
|
@ -9,6 +9,9 @@ from django.conf import settings
|
|||||||
|
|
||||||
from PIL import UnidentifiedImageError
|
from PIL import UnidentifiedImageError
|
||||||
|
|
||||||
|
from InvenTree.ready import canAppAccessDatabase
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger("inventree")
|
logger = logging.getLogger("inventree")
|
||||||
|
|
||||||
|
|
||||||
@ -20,8 +23,9 @@ class PartConfig(AppConfig):
|
|||||||
This function is called whenever the Part app is loaded.
|
This function is called whenever the Part app is loaded.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self.generate_part_thumbnails()
|
if canAppAccessDatabase():
|
||||||
self.update_trackable_status()
|
self.generate_part_thumbnails()
|
||||||
|
self.update_trackable_status()
|
||||||
|
|
||||||
def generate_part_thumbnails(self):
|
def generate_part_thumbnails(self):
|
||||||
"""
|
"""
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
import os
|
import os
|
||||||
import sys
|
|
||||||
import shutil
|
import shutil
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from django.apps import AppConfig
|
from django.apps import AppConfig
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
|
from InvenTree.ready import canAppAccessDatabase
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger("inventree")
|
logger = logging.getLogger("inventree")
|
||||||
|
|
||||||
@ -18,9 +19,7 @@ class ReportConfig(AppConfig):
|
|||||||
This function is called whenever the report app is loaded
|
This function is called whenever the report app is loaded
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if 'loaddata' in sys.argv:
|
if canAppAccessDatabase():
|
||||||
pass
|
|
||||||
else:
|
|
||||||
self.create_default_test_reports()
|
self.create_default_test_reports()
|
||||||
self.create_default_build_reports()
|
self.create_default_build_reports()
|
||||||
|
|
||||||
|
@ -5,21 +5,25 @@ from django.db.utils import OperationalError, ProgrammingError
|
|||||||
|
|
||||||
from django.apps import AppConfig
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
from InvenTree.ready import canAppAccessDatabase
|
||||||
|
|
||||||
|
|
||||||
class UsersConfig(AppConfig):
|
class UsersConfig(AppConfig):
|
||||||
name = 'users'
|
name = 'users'
|
||||||
|
|
||||||
def ready(self):
|
def ready(self):
|
||||||
|
|
||||||
try:
|
if canAppAccessDatabase():
|
||||||
self.assign_permissions()
|
|
||||||
except (OperationalError, ProgrammingError):
|
|
||||||
pass
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.update_owners()
|
self.assign_permissions()
|
||||||
except (OperationalError, ProgrammingError):
|
except (OperationalError, ProgrammingError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.update_owners()
|
||||||
|
except (OperationalError, ProgrammingError):
|
||||||
|
pass
|
||||||
|
|
||||||
def assign_permissions(self):
|
def assign_permissions(self):
|
||||||
|
|
||||||
|
@ -13,7 +13,8 @@ from django.dispatch import receiver
|
|||||||
from django.db.models.signals import post_save, post_delete
|
from django.db.models.signals import post_save, post_delete
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import sys
|
|
||||||
|
from InvenTree.ready import canAppAccessDatabase
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger("inventree")
|
logger = logging.getLogger("inventree")
|
||||||
@ -271,12 +272,7 @@ def update_group_roles(group, debug=False):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if 'loaddata' in sys.argv:
|
if not canAppAccessDatabase():
|
||||||
"""
|
|
||||||
In the case that we are importing records,
|
|
||||||
*do not* update group roles:
|
|
||||||
This will cause conflicts in the database!
|
|
||||||
"""
|
|
||||||
return
|
return
|
||||||
|
|
||||||
# List of permissions already associated with this group
|
# List of permissions already associated with this group
|
||||||
|
Loading…
x
Reference in New Issue
Block a user