mirror of
https://github.com/inventree/InvenTree.git
synced 2025-07-01 03:00:54 +00:00
Refactor states/status (#4857)
* add file for states * move general definition out * add some tests and docs * add tests for invalid definitions * make status_label tag generic * move templatetags * remove unused tag * rename test file * make status label a lookup * rename tags * move import structure * add missing tag * collect states dynamically * fix context function * move api function out * add tests for tags * rename tests * refactor imports * Add test for API function * improve errors and add tests for imporved errors * make test calls simpler * refactor definitions to use enums * switch to enum * refactor definitions to use enums * fix lookup * fix tag name * make _TAG lookup a function * cleanup BaseEnum * make _TAG definition simpler * restructure status codes to enum * reduce LoC * type status codes as int * add specific function for template context * Add definition for lookups * fix filter lookup * TEST: "fix" action lookup * Add missing migrations * Make all group code references explict * change default on models to value * switch to IntEnum * move groups into a seperate class * only request _TAG if it exsists * use value and list * use dedicated groups * fix stock assigment * fix order code * more fixes * fix borked change * fix render lookup * add group * fix import * fix syntax * clenup * fix migrations * fix typo * fix wrong value usage * fix test * remove group section * remove group section * add more test cases * Add more docstring * move choices out of migrations * change import ordeR? * last try before I revert * Update part.migrations.0112 - Add custom migration class which handles errors * Add unit test for migration - Ensure that the new fields are added to the model * Update reference to PR --------- Co-authored-by: Oliver Walters <oliver.henry.walters@gmail.com>
This commit is contained in:
@ -22,8 +22,9 @@ from build.models import Build
|
||||
from build.serializers import BuildSerializer
|
||||
from company.models import Company, SupplierPart
|
||||
from company.serializers import CompanySerializer, SupplierPartSerializer
|
||||
from generic.states import StatusView
|
||||
from InvenTree.api import (APIDownloadMixin, AttachmentMixin,
|
||||
ListCreateDestroyAPIView, MetadataView, StatusView)
|
||||
ListCreateDestroyAPIView, MetadataView)
|
||||
from InvenTree.filters import (ORDER_FILTER, SEARCH_ORDER_FILTER,
|
||||
SEARCH_ORDER_FILTER_ALIAS)
|
||||
from InvenTree.helpers import (DownloadFile, extract_serial_numbers, isNull,
|
||||
|
@ -190,7 +190,7 @@ def update_history(apps, schema_editor):
|
||||
tracking_type = StockHistoryCode.RECEIVED_AGAINST_PURCHASE_ORDER
|
||||
|
||||
if tracking_type is not None:
|
||||
entry.tracking_type = tracking_type
|
||||
entry.tracking_type = tracking_type.value
|
||||
updated = True
|
||||
|
||||
if updated:
|
||||
|
@ -43,7 +43,7 @@ def update_stock_history(apps, schema_editor):
|
||||
history.deltas['salesorder'] = item.sales_order.pk
|
||||
|
||||
# Change the history type
|
||||
history.tracking_type = StockHistoryCode.SHIPPED_AGAINST_SALES_ORDER
|
||||
history.tracking_type = StockHistoryCode.SHIPPED_AGAINST_SALES_ORDER.value
|
||||
|
||||
history.save()
|
||||
n += 1
|
||||
|
20
InvenTree/stock/migrations/0102_alter_stockitem_status.py
Normal file
20
InvenTree/stock/migrations/0102_alter_stockitem_status.py
Normal file
@ -0,0 +1,20 @@
|
||||
# Generated by Django 3.2.19 on 2023-06-04 17:43
|
||||
|
||||
import django.core.validators
|
||||
from django.db import migrations, models
|
||||
import InvenTree.status_codes
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('stock', '0101_stockitemtestresult_metadata'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='stockitem',
|
||||
name='status',
|
||||
field=models.PositiveIntegerField(choices=InvenTree.status_codes.StockStatus.items(), default=10, validators=[django.core.validators.MinValueValidator(0)]),
|
||||
),
|
||||
]
|
@ -34,8 +34,8 @@ from InvenTree.fields import InvenTreeModelMoneyField, InvenTreeURLField
|
||||
from InvenTree.models import (InvenTreeAttachment, InvenTreeBarcodeMixin,
|
||||
InvenTreeNotesMixin, InvenTreeTree,
|
||||
MetadataMixin, extract_int)
|
||||
from InvenTree.status_codes import (SalesOrderStatus, StockHistoryCode,
|
||||
StockStatus)
|
||||
from InvenTree.status_codes import (SalesOrderStatusGroups, StockHistoryCode,
|
||||
StockStatus, StockStatusGroups)
|
||||
from part import models as PartModels
|
||||
from plugin.events import trigger_event
|
||||
from users.models import Owner
|
||||
@ -334,7 +334,7 @@ class StockItem(InvenTreeBarcodeMixin, InvenTreeNotesMixin, MetadataMixin, commo
|
||||
customer=None,
|
||||
consumed_by=None,
|
||||
is_building=False,
|
||||
status__in=StockStatus.AVAILABLE_CODES
|
||||
status__in=StockStatusGroups.AVAILABLE_CODES
|
||||
)
|
||||
|
||||
# A query filter which can be used to filter StockItem objects which have expired
|
||||
@ -806,7 +806,7 @@ class StockItem(InvenTreeBarcodeMixin, InvenTreeNotesMixin, MetadataMixin, commo
|
||||
)
|
||||
|
||||
status = models.PositiveIntegerField(
|
||||
default=StockStatus.OK,
|
||||
default=StockStatus.OK.value,
|
||||
choices=StockStatus.items(),
|
||||
validators=[MinValueValidator(0)])
|
||||
|
||||
@ -1082,12 +1082,12 @@ class StockItem(InvenTreeBarcodeMixin, InvenTreeNotesMixin, MetadataMixin, commo
|
||||
|
||||
if active is True:
|
||||
query = query.filter(
|
||||
line__order__status__in=SalesOrderStatus.OPEN,
|
||||
line__order__status__in=SalesOrderStatusGroups.OPEN,
|
||||
shipment__shipment_date=None
|
||||
)
|
||||
elif active is False:
|
||||
query = query.exclude(
|
||||
line__order__status__in=SalesOrderStatus.OPEN
|
||||
line__order__status__in=SalesOrderStatusGroups.OPEN,
|
||||
).exclude(
|
||||
shipment__shipment_date=None
|
||||
)
|
||||
@ -1346,7 +1346,7 @@ class StockItem(InvenTreeBarcodeMixin, InvenTreeNotesMixin, MetadataMixin, commo
|
||||
|
||||
entry = StockItemTracking.objects.create(
|
||||
item=self,
|
||||
tracking_type=entry_type,
|
||||
tracking_type=entry_type.value,
|
||||
user=user,
|
||||
date=datetime.now(),
|
||||
notes=notes,
|
||||
|
@ -2,7 +2,7 @@
|
||||
{% load static %}
|
||||
{% load plugin_extras %}
|
||||
{% load inventree_extras %}
|
||||
{% load status_codes %}
|
||||
{% load generic %}
|
||||
{% load i18n %}
|
||||
{% load l10n %}
|
||||
|
||||
@ -421,7 +421,7 @@
|
||||
<tr>
|
||||
<td><span class='fas fa-info'></span></td>
|
||||
<td>{% trans "Status" %}</td>
|
||||
<td>{% stock_status_label item.status %}</td>
|
||||
<td>{% status_label 'stock' item.status %}</td>
|
||||
</tr>
|
||||
{% if item.expiry_date %}
|
||||
<tr>
|
||||
|
@ -385,11 +385,11 @@ class StockItemListTest(StockAPITestCase):
|
||||
def test_filter_by_status(self):
|
||||
"""Filter StockItem by 'status' field."""
|
||||
codes = {
|
||||
StockStatus.OK: 27,
|
||||
StockStatus.DESTROYED: 1,
|
||||
StockStatus.LOST: 1,
|
||||
StockStatus.DAMAGED: 0,
|
||||
StockStatus.REJECTED: 0,
|
||||
StockStatus.OK.value: 27,
|
||||
StockStatus.DESTROYED.value: 1,
|
||||
StockStatus.LOST.value: 1,
|
||||
StockStatus.DAMAGED.value: 0,
|
||||
StockStatus.REJECTED.value: 0,
|
||||
}
|
||||
|
||||
for code in codes.keys():
|
||||
@ -1465,7 +1465,7 @@ class StockAssignTest(StockAPITestCase):
|
||||
|
||||
stock_item = StockItem.objects.create(
|
||||
part=part.models.Part.objects.get(pk=1),
|
||||
status=StockStatus.DESTROYED,
|
||||
status=StockStatus.DESTROYED.value,
|
||||
quantity=5,
|
||||
)
|
||||
|
||||
|
@ -112,7 +112,7 @@ class StockOwnershipTest(StockViewTestCase):
|
||||
"""Helper function to get response to API change."""
|
||||
return self.client.patch(
|
||||
reverse('api-stock-detail', args=(self.test_item_id,)),
|
||||
{'status': StockStatus.DAMAGED},
|
||||
{'status': StockStatus.DAMAGED.value},
|
||||
content_type='application/json',
|
||||
)
|
||||
|
||||
@ -156,7 +156,7 @@ class StockOwnershipTest(StockViewTestCase):
|
||||
# Check that user is allowed to change item
|
||||
self.assertTrue(item.check_ownership(self.user)) # Owner is group -> True
|
||||
self.assertTrue(location.check_ownership(self.user)) # Owner is group -> True
|
||||
self.assertContains(self.assert_api_change(), f'"status":{StockStatus.DAMAGED}', status_code=200)
|
||||
self.assertContains(self.assert_api_change(), f'"status":{StockStatus.DAMAGED.value}', status_code=200)
|
||||
|
||||
# Change group
|
||||
new_group = Group.objects.create(name='new_group')
|
||||
|
Reference in New Issue
Block a user