mirror of
https://github.com/inventree/InvenTree.git
synced 2025-07-01 03:00:54 +00:00
Tweaks to fix circular imports (#9888)
This commit is contained in:
@ -11,13 +11,11 @@ import structlog
|
|||||||
from allauth.account.models import EmailAddress
|
from allauth.account.models import EmailAddress
|
||||||
from opentelemetry import trace
|
from opentelemetry import trace
|
||||||
|
|
||||||
import build.models as build_models
|
|
||||||
import common.notifications
|
import common.notifications
|
||||||
import InvenTree.helpers
|
import InvenTree.helpers
|
||||||
import InvenTree.helpers_email
|
import InvenTree.helpers_email
|
||||||
import InvenTree.helpers_model
|
import InvenTree.helpers_model
|
||||||
import InvenTree.tasks
|
import InvenTree.tasks
|
||||||
import part.models as part_models
|
|
||||||
from build.events import BuildEvents
|
from build.events import BuildEvents
|
||||||
from build.status_codes import BuildStatusGroups
|
from build.status_codes import BuildStatusGroups
|
||||||
from InvenTree.ready import isImportingData
|
from InvenTree.ready import isImportingData
|
||||||
@ -30,7 +28,9 @@ logger = structlog.get_logger('inventree')
|
|||||||
@tracer.start_as_current_span('auto_allocate_build')
|
@tracer.start_as_current_span('auto_allocate_build')
|
||||||
def auto_allocate_build(build_id: int, **kwargs):
|
def auto_allocate_build(build_id: int, **kwargs):
|
||||||
"""Run auto-allocation for a specified BuildOrder."""
|
"""Run auto-allocation for a specified BuildOrder."""
|
||||||
build_order = build_models.Build.objects.filter(pk=build_id).first()
|
from build.models import Build
|
||||||
|
|
||||||
|
build_order = Build.objects.filter(pk=build_id).first()
|
||||||
|
|
||||||
if not build_order:
|
if not build_order:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
@ -45,7 +45,9 @@ def auto_allocate_build(build_id: int, **kwargs):
|
|||||||
@tracer.start_as_current_span('complete_build_allocations')
|
@tracer.start_as_current_span('complete_build_allocations')
|
||||||
def complete_build_allocations(build_id: int, user_id: int):
|
def complete_build_allocations(build_id: int, user_id: int):
|
||||||
"""Complete build allocations for a specified BuildOrder."""
|
"""Complete build allocations for a specified BuildOrder."""
|
||||||
build_order = build_models.Build.objects.filter(pk=build_id).first()
|
from build.models import Build
|
||||||
|
|
||||||
|
build_order = Build.objects.filter(pk=build_id).first()
|
||||||
|
|
||||||
if user_id:
|
if user_id:
|
||||||
try:
|
try:
|
||||||
@ -75,9 +77,12 @@ def update_build_order_lines(bom_item_pk: int):
|
|||||||
|
|
||||||
This task is triggered when a BomItem is created or updated.
|
This task is triggered when a BomItem is created or updated.
|
||||||
"""
|
"""
|
||||||
|
from build.models import Build, BuildLine
|
||||||
|
from part.models import BomItem
|
||||||
|
|
||||||
logger.info('Updating build order lines for BomItem %s', bom_item_pk)
|
logger.info('Updating build order lines for BomItem %s', bom_item_pk)
|
||||||
|
|
||||||
bom_item = part_models.BomItem.objects.filter(pk=bom_item_pk).first()
|
bom_item = BomItem.objects.filter(pk=bom_item_pk).first()
|
||||||
|
|
||||||
# If the BomItem has been deleted, there is nothing to do
|
# If the BomItem has been deleted, there is nothing to do
|
||||||
if not bom_item:
|
if not bom_item:
|
||||||
@ -86,16 +91,14 @@ def update_build_order_lines(bom_item_pk: int):
|
|||||||
assemblies = bom_item.get_assemblies()
|
assemblies = bom_item.get_assemblies()
|
||||||
|
|
||||||
# Find all active builds which reference any of the parts
|
# Find all active builds which reference any of the parts
|
||||||
builds = build_models.Build.objects.filter(
|
builds = Build.objects.filter(
|
||||||
part__in=list(assemblies), status__in=BuildStatusGroups.ACTIVE_CODES
|
part__in=list(assemblies), status__in=BuildStatusGroups.ACTIVE_CODES
|
||||||
)
|
)
|
||||||
|
|
||||||
# Iterate through each build, and update the relevant line items
|
# Iterate through each build, and update the relevant line items
|
||||||
for bo in builds:
|
for bo in builds:
|
||||||
# Try to find a matching build order line
|
# Try to find a matching build order line
|
||||||
line = build_models.BuildLine.objects.filter(
|
line = BuildLine.objects.filter(build=bo, bom_item=bom_item).first()
|
||||||
build=bo, bom_item=bom_item
|
|
||||||
).first()
|
|
||||||
|
|
||||||
q = bom_item.get_required_quantity(bo.quantity)
|
q = bom_item.get_required_quantity(bo.quantity)
|
||||||
|
|
||||||
@ -106,9 +109,7 @@ def update_build_order_lines(bom_item_pk: int):
|
|||||||
line.save()
|
line.save()
|
||||||
else:
|
else:
|
||||||
# Create a new line item
|
# Create a new line item
|
||||||
build_models.BuildLine.objects.create(
|
BuildLine.objects.create(build=bo, bom_item=bom_item, quantity=q)
|
||||||
build=bo, bom_item=bom_item, quantity=q
|
|
||||||
)
|
|
||||||
|
|
||||||
if builds.count() > 0:
|
if builds.count() > 0:
|
||||||
logger.info(
|
logger.info(
|
||||||
@ -117,11 +118,13 @@ def update_build_order_lines(bom_item_pk: int):
|
|||||||
|
|
||||||
|
|
||||||
@tracer.start_as_current_span('check_build_stock')
|
@tracer.start_as_current_span('check_build_stock')
|
||||||
def check_build_stock(build: build_models.Build):
|
def check_build_stock(build):
|
||||||
"""Check the required stock for a newly created build order.
|
"""Check the required stock for a newly created build order.
|
||||||
|
|
||||||
Send an email out to any subscribed users if stock is low.
|
Send an email out to any subscribed users if stock is low.
|
||||||
"""
|
"""
|
||||||
|
from part.models import Part
|
||||||
|
|
||||||
# Do not notify if we are importing data
|
# Do not notify if we are importing data
|
||||||
if isImportingData():
|
if isImportingData():
|
||||||
return
|
return
|
||||||
@ -136,7 +139,7 @@ def check_build_stock(build: build_models.Build):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
part = build.part
|
part = build.part
|
||||||
except part_models.Part.DoesNotExist:
|
except Part.DoesNotExist:
|
||||||
# Note: This error may be thrown during unit testing...
|
# Note: This error may be thrown during unit testing...
|
||||||
logger.exception("Invalid build.part passed to 'build.tasks.check_build_stock'")
|
logger.exception("Invalid build.part passed to 'build.tasks.check_build_stock'")
|
||||||
return
|
return
|
||||||
@ -203,7 +206,7 @@ def check_build_stock(build: build_models.Build):
|
|||||||
|
|
||||||
|
|
||||||
@tracer.start_as_current_span('notify_overdue_build_order')
|
@tracer.start_as_current_span('notify_overdue_build_order')
|
||||||
def notify_overdue_build_order(bo: build_models.Build):
|
def notify_overdue_build_order(bo):
|
||||||
"""Notify appropriate users that a Build has just become 'overdue'."""
|
"""Notify appropriate users that a Build has just become 'overdue'."""
|
||||||
targets = []
|
targets = []
|
||||||
|
|
||||||
@ -245,9 +248,11 @@ def check_overdue_build_orders():
|
|||||||
- Look at the 'target_date' of any outstanding BuildOrder objects
|
- Look at the 'target_date' of any outstanding BuildOrder objects
|
||||||
- If the 'target_date' expired *yesterday* then the order is just out of date
|
- If the 'target_date' expired *yesterday* then the order is just out of date
|
||||||
"""
|
"""
|
||||||
|
from build.models import Build
|
||||||
|
|
||||||
yesterday = InvenTree.helpers.current_date() - timedelta(days=1)
|
yesterday = InvenTree.helpers.current_date() - timedelta(days=1)
|
||||||
|
|
||||||
overdue_orders = build_models.Build.objects.filter(
|
overdue_orders = Build.objects.filter(
|
||||||
target_date=yesterday, status__in=BuildStatusGroups.ACTIVE_CODES
|
target_date=yesterday, status__in=BuildStatusGroups.ACTIVE_CODES
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user