mirror of
https://github.com/inventree/InvenTree.git
synced 2025-08-07 12:22:11 +00:00
Clear out 'custom key' if the base key does not match
This commit is contained in:
@@ -96,10 +96,31 @@ class StatusCodeMixin:
|
|||||||
"""Return the status class associated with this model."""
|
"""Return the status class associated with this model."""
|
||||||
return self.STATUS_CLASS
|
return self.STATUS_CLASS
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
"""Custom save method for StatusCodeMixin.
|
||||||
|
|
||||||
|
- Ensure custom status code values are correctly updated
|
||||||
|
"""
|
||||||
|
if self.status_class:
|
||||||
|
# Check that the current 'logical key' actually matches the current status code
|
||||||
|
custom_values = self.status_class.custom_queryset().filter(
|
||||||
|
logical_key=self.get_status(), key=self.get_custom_status()
|
||||||
|
)
|
||||||
|
|
||||||
|
if not custom_values.exists():
|
||||||
|
# No match - null out the custom value
|
||||||
|
setattr(self, f'{self.STATUS_FIELD}_custom_key', None)
|
||||||
|
|
||||||
|
super().save()
|
||||||
|
|
||||||
def get_status(self) -> int:
|
def get_status(self) -> int:
|
||||||
"""Return the status code for this object."""
|
"""Return the status code for this object."""
|
||||||
return self.status
|
return self.status
|
||||||
|
|
||||||
|
def get_custom_status(self) -> int:
|
||||||
|
"""Return the custom status code for this object."""
|
||||||
|
return getattr(self, f'{self.STATUS_FIELD}_custom_key', None)
|
||||||
|
|
||||||
def set_status(self, status: int) -> bool:
|
def set_status(self, status: int) -> bool:
|
||||||
"""Set the status code for this object."""
|
"""Set the status code for this object."""
|
||||||
if not self.status_class:
|
if not self.status_class:
|
||||||
@@ -126,6 +147,9 @@ class StatusCodeMixin:
|
|||||||
result = True
|
result = True
|
||||||
break
|
break
|
||||||
|
|
||||||
|
if not result:
|
||||||
|
logger.warning(f'Failed to set status {status} for class {self.__class__}')
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
@@ -103,18 +103,23 @@ class StatusCode(BaseEnum):
|
|||||||
return isinstance(value.value, int)
|
return isinstance(value.value, int)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def custom_values(cls):
|
def custom_queryset(cls):
|
||||||
"""Return all user-defined custom values for this status class."""
|
"""Return a queryset of all custom values for this status class."""
|
||||||
from common.models import InvenTreeCustomUserStateModel
|
from common.models import InvenTreeCustomUserStateModel
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return list(
|
return InvenTreeCustomUserStateModel.objects.filter(
|
||||||
InvenTreeCustomUserStateModel.objects.filter(
|
reference_status=cls.__name__
|
||||||
reference_status=cls.__name__
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
except Exception:
|
except Exception:
|
||||||
return []
|
return None
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def custom_values(cls):
|
||||||
|
"""Return all user-defined custom values for this status class."""
|
||||||
|
if query := cls.custom_queryset():
|
||||||
|
return list(query)
|
||||||
|
return []
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def values(cls, key=None):
|
def values(cls, key=None):
|
||||||
|
@@ -2144,7 +2144,7 @@ class StockItem(
|
|||||||
status = kwargs.pop('status', None) or kwargs.pop('status_custom_key', None)
|
status = kwargs.pop('status', None) or kwargs.pop('status_custom_key', None)
|
||||||
|
|
||||||
if status and status != self.status:
|
if status and status != self.status:
|
||||||
self.status_custom_key = status
|
self.set_status(status)
|
||||||
tracking_info['status'] = status
|
tracking_info['status'] = status
|
||||||
|
|
||||||
# Optional fields which can be supplied in a 'move' call
|
# Optional fields which can be supplied in a 'move' call
|
||||||
@@ -2229,7 +2229,7 @@ class StockItem(
|
|||||||
status = kwargs.pop('status', None) or kwargs.pop('status_custom_key', None)
|
status = kwargs.pop('status', None) or kwargs.pop('status_custom_key', None)
|
||||||
|
|
||||||
if status and status != self.status:
|
if status and status != self.status:
|
||||||
self.status_custom_key = status
|
self.set_status(status)
|
||||||
tracking_info['status'] = status
|
tracking_info['status'] = status
|
||||||
|
|
||||||
if self.updateQuantity(count):
|
if self.updateQuantity(count):
|
||||||
@@ -2292,7 +2292,7 @@ class StockItem(
|
|||||||
status = kwargs.pop('status', None) or kwargs.pop('status_custom_key', None)
|
status = kwargs.pop('status', None) or kwargs.pop('status_custom_key', None)
|
||||||
|
|
||||||
if status and status != self.status:
|
if status and status != self.status:
|
||||||
self.status_custom_key = status
|
self.set_status(status)
|
||||||
tracking_info['status'] = status
|
tracking_info['status'] = status
|
||||||
|
|
||||||
if self.updateQuantity(self.quantity + quantity):
|
if self.updateQuantity(self.quantity + quantity):
|
||||||
@@ -2346,7 +2346,7 @@ class StockItem(
|
|||||||
status = kwargs.pop('status', None) or kwargs.pop('status_custom_key', None)
|
status = kwargs.pop('status', None) or kwargs.pop('status_custom_key', None)
|
||||||
|
|
||||||
if status and status != self.status:
|
if status and status != self.status:
|
||||||
self.status_custom_key = status
|
self.set_status(status)
|
||||||
deltas['status'] = status
|
deltas['status'] = status
|
||||||
|
|
||||||
if self.updateQuantity(self.quantity - quantity):
|
if self.updateQuantity(self.quantity - quantity):
|
||||||
|
Reference in New Issue
Block a user