mirror of
https://github.com/inventree/InvenTree.git
synced 2025-11-03 06:35:44 +00:00
Push BOM validation to background worker (#10727)
- Can take a significant amount of time - Closes https://github.com/inventree/InvenTree/issues/10725
This commit is contained in:
@@ -15,6 +15,7 @@ from rest_framework import serializers
|
|||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
|
||||||
import part.filters
|
import part.filters
|
||||||
|
import part.tasks as part_tasks
|
||||||
from data_exporter.mixins import DataExportViewMixin
|
from data_exporter.mixins import DataExportViewMixin
|
||||||
from InvenTree.api import (
|
from InvenTree.api import (
|
||||||
BulkCreateMixin,
|
BulkCreateMixin,
|
||||||
@@ -47,6 +48,7 @@ from InvenTree.mixins import (
|
|||||||
SerializerContextMixin,
|
SerializerContextMixin,
|
||||||
UpdateAPI,
|
UpdateAPI,
|
||||||
)
|
)
|
||||||
|
from InvenTree.tasks import offload_task
|
||||||
from stock.models import StockLocation
|
from stock.models import StockLocation
|
||||||
|
|
||||||
from . import serializers as part_serializers
|
from . import serializers as part_serializers
|
||||||
@@ -633,7 +635,14 @@ class PartValidateBOM(RetrieveUpdateAPI):
|
|||||||
|
|
||||||
valid = str2bool(serializer.validated_data.get('valid', False))
|
valid = str2bool(serializer.validated_data.get('valid', False))
|
||||||
|
|
||||||
part.validate_bom(request.user, valid=valid)
|
# BOM validation may take some time, so we offload it to a background task
|
||||||
|
offload_task(
|
||||||
|
part_tasks.validate_bom,
|
||||||
|
part.pk,
|
||||||
|
valid,
|
||||||
|
user_id=request.user.pk if request and request.user else None,
|
||||||
|
group='part',
|
||||||
|
)
|
||||||
|
|
||||||
# Re-serialize the response
|
# Re-serialize the response
|
||||||
serializer = self.get_serializer(part, many=False)
|
serializer = self.get_serializer(part, many=False)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
"""Background task definitions for the 'part' app."""
|
"""Background task definitions for the 'part' app."""
|
||||||
|
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.db.models import Model
|
from django.db.models import Model
|
||||||
@@ -437,3 +438,35 @@ def check_bom_valid(part_id: int):
|
|||||||
if valid != part.bom_validated:
|
if valid != part.bom_validated:
|
||||||
part.bom_validated = valid
|
part.bom_validated = valid
|
||||||
part.save()
|
part.save()
|
||||||
|
|
||||||
|
|
||||||
|
@tracer.start_as_current_span('validate_bom')
|
||||||
|
def validate_bom(part_id: int, valid: bool, user_id: Optional[int] = None):
|
||||||
|
"""Run BOM validation for the specified Part.
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
part_id: The ID of the part for which to validate the BOM.
|
||||||
|
valid: Boolean indicating whether the BOM is valid or not.
|
||||||
|
user_id: Optional ID of the user performing the validation.
|
||||||
|
"""
|
||||||
|
from django.contrib.auth import get_user_model
|
||||||
|
|
||||||
|
from part.models import Part
|
||||||
|
|
||||||
|
User = get_user_model()
|
||||||
|
|
||||||
|
try:
|
||||||
|
part = Part.objects.get(pk=part_id)
|
||||||
|
except Part.DoesNotExist:
|
||||||
|
logger.warning('validate_bom: Part with ID %s does not exist', part_id)
|
||||||
|
return
|
||||||
|
|
||||||
|
if user_id:
|
||||||
|
try:
|
||||||
|
user = User.objects.get(pk=user_id)
|
||||||
|
except User.DoesNotExist:
|
||||||
|
user = None
|
||||||
|
else:
|
||||||
|
user = None
|
||||||
|
|
||||||
|
part.validate_bom(user, valid=valid)
|
||||||
|
|||||||
@@ -184,7 +184,7 @@ function BomValidationInformation({
|
|||||||
<Text>{t`Do you want to validate the bill of materials for this assembly?`}</Text>
|
<Text>{t`Do you want to validate the bill of materials for this assembly?`}</Text>
|
||||||
</Alert>
|
</Alert>
|
||||||
),
|
),
|
||||||
successMessage: t`BOM validated`,
|
successMessage: t`Bill of materials scheduled for validation`,
|
||||||
onFormSuccess: () => {
|
onFormSuccess: () => {
|
||||||
bomInformationQuery.refetch();
|
bomInformationQuery.refetch();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user