From f731c45ce872d442239b8a47eb5369c38e6d13b9 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 4 Jun 2019 23:38:52 +1000 Subject: [PATCH] Replace other choice fields with commonized status code --- InvenTree/InvenTree/status_codes.py | 50 +++++++++++++++++++++++++++++ InvenTree/build/models.py | 24 ++++---------- InvenTree/part/models.py | 8 +++-- InvenTree/stock/api.py | 3 +- InvenTree/stock/models.py | 21 +++--------- 5 files changed, 67 insertions(+), 39 deletions(-) diff --git a/InvenTree/InvenTree/status_codes.py b/InvenTree/InvenTree/status_codes.py index 061d0da99e..c8fddc5b2c 100644 --- a/InvenTree/InvenTree/status_codes.py +++ b/InvenTree/InvenTree/status_codes.py @@ -7,6 +7,11 @@ class StatusCode: def items(cls): return cls.options.items() + @classmethod + def label(cls, value): + """ Return the status code label associated with the provided value """ + return cls.options.get(value, '') + class OrderStatus(StatusCode): @@ -26,3 +31,48 @@ class OrderStatus(StatusCode): LOST: _("Lost"), RETURNED: _("Returned"), } + + +class StockStatus(StatusCode): + + OK = 10 # Item is OK + ATTENTION = 50 # Item requires attention + DAMAGED = 55 # Item is damaged + DESTROYED = 60 # Item is destroyed + LOST = 70 # Item has been lost + + options = { + OK: _("OK"), + ATTENTION: _("Attention needed"), + DAMAGED: _("Damaged"), + DESTROYED: _("Destroyed"), + LOST: _("Lost"), + } + + # The following codes correspond to parts that are 'available' + AVAILABLE_CODES = [ + OK, + ATTENTION, + DAMAGED + ] + + +class BuildStatus(StatusCode): + + # Build status codes + PENDING = 10 # Build is pending / active + ALLOCATED = 20 # Parts have been removed from stock + CANCELLED = 30 # Build was cancelled + COMPLETE = 40 # Build is complete + + options = { + PENDING: _("Pending"), + ALLOCATED: _("Allocated"), + CANCELLED: _("Cancelled"), + COMPLETE: _("Complete"), + } + + ACTIVE_CODES = [ + PENDING, + ALLOCATED + ] diff --git a/InvenTree/build/models.py b/InvenTree/build/models.py index f9ffc99953..b40f270fbc 100644 --- a/InvenTree/build/models.py +++ b/InvenTree/build/models.py @@ -16,6 +16,8 @@ from django.db import models, transaction from django.db.models import Sum from django.core.validators import MinValueValidator +from InvenTree.status_codes import BuildStatus + from stock.models import StockItem from part.models import Part, BomItem @@ -68,22 +70,10 @@ class Build(models.Model): validators=[MinValueValidator(1)], help_text='Number of parts to build' ) - - # Build status codes - PENDING = 10 # Build is pending / active - ALLOCATED = 20 # Parts have been removed from stock - CANCELLED = 30 # Build was cancelled - COMPLETE = 40 # Build is complete - #: Build status codes - BUILD_STATUS_CODES = {PENDING: _("Pending"), - ALLOCATED: _("Allocated"), - CANCELLED: _("Cancelled"), - COMPLETE: _("Complete"), - } - status = models.PositiveIntegerField(default=PENDING, - choices=BUILD_STATUS_CODES.items(), + status = models.PositiveIntegerField(default=BuildStatus.PENDING, + choices=BuildStatus.items(), validators=[MinValueValidator(0)], help_text='Build status') @@ -325,14 +315,12 @@ class Build(models.Model): - HOLDING """ - return self.status in [ - self.PENDING, - ] + return self.status in BuildStatus.ACTIVE_CODES @property def is_complete(self): """ Returns True if the build status is COMPLETE """ - return self.status == self.COMPLETE + return self.status == self.BuildStatus.COMPLETE class BuildItem(models.Model): diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index f311aea1f1..29a11125ab 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -33,6 +33,8 @@ from InvenTree import helpers from InvenTree import validators from InvenTree.models import InvenTreeTree +from InvenTree.status_codes import BuildStatus, StockStatus + from company.models import SupplierPart @@ -454,14 +456,14 @@ class Part(models.Model): Builds marked as 'complete' or 'cancelled' are ignored """ - return [b for b in self.builds.all() if b.is_active] + return self.builds.filter(status__in=BuildStatus.ACTIVE_CODES) @property def inactive_builds(self): """ Return a list of inactive builds """ - return [b for b in self.builds.all() if not b.is_active] + return self.builds.exclude(status__in=BuildStatus.ACTIVE_CODES) @property def quantity_being_built(self): @@ -531,7 +533,7 @@ class Part(models.Model): if self.is_template: total = sum([variant.total_stock for variant in self.variants.all()]) else: - total = self.stock_entries.aggregate(total=Sum('quantity'))['total'] + total = self.stock_entries.filter(status__in=StockStatus.AVAILABLE_CODES).aggregate(total=Sum('quantity'))['total'] if total: return total diff --git a/InvenTree/stock/api.py b/InvenTree/stock/api.py index b1c746cdd6..092724407f 100644 --- a/InvenTree/stock/api.py +++ b/InvenTree/stock/api.py @@ -20,6 +20,7 @@ from .serializers import StockTrackingSerializer from InvenTree.views import TreeSerializer from InvenTree.helpers import str2bool +from InvenTree.status_codes import StockStatus import os @@ -311,7 +312,7 @@ class StockList(generics.ListCreateAPIView): else: item['location__path'] = None - item['status_text'] = StockItem.ITEM_STATUS_CODES[item['status']] + item['status_text'] = StockStatus.label(item['status']) return Response(data) diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 4434741186..b9773f9d77 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -19,6 +19,7 @@ from django.dispatch import receiver from datetime import datetime from InvenTree import helpers +from InvenTree.status_codes import StockStatus from InvenTree.models import InvenTreeTree from part.models import Part @@ -93,7 +94,7 @@ class StockItem(models.Model): stocktake_user: User that performed the most recent stocktake review_needed: Flag if StockItem needs review delete_on_deplete: If True, StockItem will be deleted when the stock level gets to zero - status: Status of this StockItem (ref: ITEM_STATUS_CODES) + status: Status of this StockItem (ref: InvenTree.status_codes.StockStatus) notes: Extra notes field infinite: If True this StockItem can never be exhausted """ @@ -256,23 +257,9 @@ class StockItem(models.Model): delete_on_deplete = models.BooleanField(default=True, help_text='Delete this Stock Item when stock is depleted') - ITEM_OK = 10 - ITEM_ATTENTION = 50 - ITEM_DAMAGED = 55 - ITEM_DESTROYED = 60 - ITEM_LOST = 70 - - ITEM_STATUS_CODES = { - ITEM_OK: _("OK"), - ITEM_ATTENTION: _("Attention needed"), - ITEM_DAMAGED: _("Damaged"), - ITEM_DESTROYED: _("Destroyed"), - ITEM_LOST: _("Lost") - } - status = models.PositiveIntegerField( - default=ITEM_OK, - choices=ITEM_STATUS_CODES.items(), + default=StockStatus.OK, + choices=StockStatus.items(), validators=[MinValueValidator(0)]) notes = models.CharField(max_length=250, blank=True, help_text='Stock Item Notes')