From 725a64c29dff06950298762124e6b8e49743cecd Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 11 May 2021 18:11:27 +1000 Subject: [PATCH] Extract more information from legacy tracking data --- .../migrations/0061_auto_20210511_0911.py | 43 +++++++++++++++++-- InvenTree/stock/serializers.py | 6 +-- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/InvenTree/stock/migrations/0061_auto_20210511_0911.py b/InvenTree/stock/migrations/0061_auto_20210511_0911.py index 0a11fe1fae..b3bcb5b88e 100644 --- a/InvenTree/stock/migrations/0061_auto_20210511_0911.py +++ b/InvenTree/stock/migrations/0061_auto_20210511_0911.py @@ -1,5 +1,7 @@ # Generated by Django 3.2 on 2021-05-10 23:11 +import re + from django.db import migrations from InvenTree.status_codes import StockHistoryCode @@ -27,17 +29,19 @@ def update_history(apps, schema_editor): for entry in history: + deltas = {} updated = False q = entry.quantity if not q == quantity: - entry.deltas = { - 'quantity': float(q), - } + try: + deltas['quantity']: float(q) + updated = True + except: + print(f"WARNING: Error converting quantity '{q}'") - updated = True quantity = q @@ -51,6 +55,21 @@ def update_history(apps, schema_editor): elif 'removed' in title and 'item' in title: tracking_type = StockHistoryCode.STOCK_REMOVE + + # Extract the number of removed items + result = re.search("^removed ([\d\.]+) items$", title) + + if result: + + removed = result.groups()[0] + + try: + deltas['removed'] = float(removed) + + # Ensure that 'quantity' is stored too in this case + deltas['quantity'] = float(q) + except: + print(f"WARNING: Error converting removed quantity '{removed}'") elif 'split from existing' in title: tracking_type = StockHistoryCode.SPLIT_FROM_PARENT @@ -73,6 +92,21 @@ def update_history(apps, schema_editor): elif 'added' in title: tracking_type = StockHistoryCode.STOCK_ADD + # Extract the number of added items + result = re.search("^added ([\d\.]+) items$", title) + + if result: + + added = result.groups()[0] + + try: + deltas['added'] = float(added) + + # Ensure that 'quantity' is stored too in this case + deltas['quantity'] = float(q) + except: + print(f"WARNING: Error converting added quantity '{added}'") + elif 'assigned to customer' in title: tracking_type = StockHistoryCode.SENT_TO_CUSTOMER @@ -93,6 +127,7 @@ def update_history(apps, schema_editor): updated = True if updated: + entry.deltas = deltas entry.save() update_count += 1 diff --git a/InvenTree/stock/serializers.py b/InvenTree/stock/serializers.py index c5ad3c7100..d46dc7b3dc 100644 --- a/InvenTree/stock/serializers.py +++ b/InvenTree/stock/serializers.py @@ -349,8 +349,6 @@ class StockTrackingSerializer(InvenTreeModelSerializer): if user_detail is not True: self.fields.pop('user_detail') - url = serializers.CharField(source='get_absolute_url', read_only=True) - label = serializers.CharField(read_only=True) item_detail = StockItemSerializerBrief(source='item', many=False, read_only=True) @@ -363,7 +361,6 @@ class StockTrackingSerializer(InvenTreeModelSerializer): model = StockItemTracking fields = [ 'pk', - 'url', 'item', 'item_detail', 'date', @@ -376,7 +373,6 @@ class StockTrackingSerializer(InvenTreeModelSerializer): 'tracking_type', 'user', 'user_detail', - 'system', ] read_only_fields = [ @@ -384,4 +380,6 @@ class StockTrackingSerializer(InvenTreeModelSerializer): 'user', 'system', 'quantity', + 'label', + 'tracking_type', ]