From 7d3cd03d6c0c028107024e0c51b20f34d04d9298 Mon Sep 17 00:00:00 2001
From: Oliver <oliver.henry.walters@gmail.com>
Date: Tue, 7 Sep 2021 16:28:57 +1000
Subject: [PATCH] Add "scheduled_for_deletion" field to StockItem

- If set to True, this StockItem will be deleted (soon) by the background worker
- As deletion takes significant time, this prevents delete operations from blocking the UI
---
 InvenTree/stock/api.py                         |  3 +++
 .../0066_stockitem_scheduled_for_deletion.py   | 18 ++++++++++++++++++
 InvenTree/stock/models.py                      |  9 ++++++++-
 3 files changed, 29 insertions(+), 1 deletion(-)
 create mode 100644 InvenTree/stock/migrations/0066_stockitem_scheduled_for_deletion.py

diff --git a/InvenTree/stock/api.py b/InvenTree/stock/api.py
index 7ff5c55bbe..017457f600 100644
--- a/InvenTree/stock/api.py
+++ b/InvenTree/stock/api.py
@@ -653,6 +653,9 @@ class StockList(generics.ListCreateAPIView):
 
         queryset = StockItemSerializer.annotate_queryset(queryset)
 
+        # Do not expose StockItem objects which are scheduled for deletion
+        queryset = queryset.filter(scheduled_for_deletion=False)
+
         return queryset
 
     def filter_queryset(self, queryset):
diff --git a/InvenTree/stock/migrations/0066_stockitem_scheduled_for_deletion.py b/InvenTree/stock/migrations/0066_stockitem_scheduled_for_deletion.py
new file mode 100644
index 0000000000..45922d2f9f
--- /dev/null
+++ b/InvenTree/stock/migrations/0066_stockitem_scheduled_for_deletion.py
@@ -0,0 +1,18 @@
+# Generated by Django 3.2.4 on 2021-09-07 06:27
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('stock', '0065_auto_20210701_0509'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='stockitem',
+            name='scheduled_for_deletion',
+            field=models.BooleanField(default=False, help_text='This StockItem will be deleted by the background worker', verbose_name='Scheduled for deletion'),
+        ),
+    ]
diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py
index 11d5de9bf1..992008b10e 100644
--- a/InvenTree/stock/models.py
+++ b/InvenTree/stock/models.py
@@ -209,7 +209,8 @@ class StockItem(MPTTModel):
         belongs_to=None,
         customer=None,
         is_building=False,
-        status__in=StockStatus.AVAILABLE_CODES
+        status__in=StockStatus.AVAILABLE_CODES,
+        scheduled_for_deletion=False,
     )
 
     # A query filter which can be used to filter StockItem objects which have expired
@@ -588,6 +589,12 @@ class StockItem(MPTTModel):
                               help_text=_('Select Owner'),
                               related_name='stock_items')
 
+    scheduled_for_deletion = models.BooleanField(
+        default=False,
+        verbose_name=_('Scheduled for deletion'),
+        help_text=_('This StockItem will be deleted by the background worker'),
+    )
+
     def is_stale(self):
         """
         Returns True if this Stock item is "stale".