2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-05 13:10:57 +00:00

Log errors during tree rebuild (#9927)

* Log errors during tree rebuild

* Ensure stock tree is rebuilt

* Update models.py

Fix call to rebuild_stock_tree

* Update models.py

style fixes
This commit is contained in:
Oliver
2025-07-02 00:07:54 +10:00
committed by GitHub
parent e693c93c08
commit e6f91517c3
3 changed files with 14 additions and 6 deletions

View File

@ -21,6 +21,7 @@ from mptt.exceptions import InvalidMove
from mptt.models import MPTTModel, TreeForeignKey
import common.settings
import InvenTree.exceptions
import InvenTree.fields
import InvenTree.format
import InvenTree.helpers
@ -590,6 +591,9 @@ class InvenTreeTree(MetadataMixin, PluginValidationMixin, MPTTModel):
try:
self.__class__.objects.partial_rebuild(tree_id)
except Exception:
InvenTree.exceptions.log_error(
f'{self.__class__.__name__}.partial_rebuild'
)
logger.warning(
'Failed to rebuild tree for %s <%s>',
self.__class__.__name__,

View File

@ -571,8 +571,10 @@ class StockItem(
for key in ['parent_id', 'part_id', 'build_id']:
data.pop(key, None)
tree_id = kwargs.pop('tree_id', 0)
data['parent'] = kwargs.pop('parent', None)
data['tree_id'] = kwargs.pop('tree_id', 0)
data['tree_id'] = tree_id
data['level'] = kwargs.pop('level', 0)
data['rght'] = kwargs.pop('rght', 0)
data['lft'] = kwargs.pop('lft', 0)
@ -589,6 +591,11 @@ class StockItem(
# Create the StockItem objects in bulk
StockItem.objects.bulk_create(items)
# We will need to rebuild the stock item tree manually, due to the bulk_create operation
InvenTree.tasks.offload_task(
stock.tasks.rebuild_stock_item_tree, tree_id=tree_id, group='stock'
)
# Return the newly created StockItem objects
return StockItem.objects.filter(part=part, serial__in=serials)
@ -1810,11 +1817,6 @@ class StockItem(
# Remove the equivalent number of items
self.take_stock(quantity, user, notes=notes)
# Rebuild the stock tree
InvenTree.tasks.offload_task(
stock.tasks.rebuild_stock_item_tree, tree_id=self.tree_id, group='stock'
)
return items
@transaction.atomic

View File

@ -13,12 +13,14 @@ def rebuild_stock_item_tree(tree_id=None):
The StockItem tree uses the MPTT library to manage the tree structure.
"""
from InvenTree.exceptions import log_error
from stock.models import StockItem
if tree_id:
try:
StockItem.objects.partial_rebuild(tree_id)
except Exception:
log_error('rebuild_stock_item_tree')
logger.warning('Failed to rebuild StockItem tree')
# If the partial rebuild fails, rebuild the entire tree
StockItem.objects.rebuild()