Coverage for src/backend/InvenTree/order/migrations/0055_auto_20211025_0645.py: 33%
34 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 17:47 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 17:47 +0000
1# Generated by Django 3.2.5 on 2021-10-25 06:45
3from django.db import migrations
6from order.status_codes import SalesOrderStatus
9def add_shipment(apps, schema_editor):
10 """
11 Create a SalesOrderShipment for each existing SalesOrder instance.
13 Any "allocations" are marked against that shipment.
15 For each existing SalesOrder instance, we create a default SalesOrderShipment,
16 and associate each SalesOrderAllocation with this shipment
17 """
19 Allocation = apps.get_model('order', 'salesorderallocation')
20 SalesOrder = apps.get_model('order', 'salesorder')
21 Shipment = apps.get_model('order', 'salesordershipment')
23 n = 0
25 for order in SalesOrder.objects.all(): 25 ↛ 27line 25 didn't jump to line 27 because the loop on line 25 never started
27 """
28 We only create an automatic shipment for "PENDING" orders,
29 as SalesOrderAllocations were historically deleted for "SHIPPED" or "CANCELLED" orders
30 """
32 allocations = Allocation.objects.filter(
33 line__order=order
34 )
36 if allocations.count() == 0 and order.status != SalesOrderStatus.PENDING: # pragma: no cover
37 continue
39 # Create a new Shipment instance against this order
40 shipment = Shipment.objects.create(
41 order=order,
42 )
44 if order.status == SalesOrderStatus.SHIPPED: # pragma: no cover
45 shipment.shipment_date = order.shipment_date
47 shipment.save()
49 # Iterate through each allocation associated with this order
50 for allocation in allocations: # pragma: no cover
51 allocation.shipment = shipment
52 allocation.save()
54 n += 1
56 if n > 0: 56 ↛ 57line 56 didn't jump to line 57 because the condition on line 56 was never true
57 print(f"\nCreated SalesOrderShipment for {n} SalesOrder instances")
60def reverse_add_shipment(apps, schema_editor): # pragma: no cover
61 """
62 Reverse the migration, delete and SalesOrderShipment instances
63 """
65 Allocation = apps.get_model('order', 'salesorderallocation')
67 # First, ensure that all SalesOrderAllocation objects point to a null shipment
68 for allocation in Allocation.objects.exclude(shipment=None):
69 allocation.shipment = None
70 allocation.save()
72 SOS = apps.get_model('order', 'salesordershipment')
74 n = SOS.objects.count()
76 print(f"Deleting {n} SalesOrderShipment instances")
78 SOS.objects.all().delete()
81class Migration(migrations.Migration):
83 dependencies = [
84 ('order', '0054_salesorderallocation_shipment'),
85 ]
87 operations = [
88 migrations.RunPython(
89 add_shipment,
90 reverse_code=reverse_add_shipment,
91 )
92 ]