Browse Source

Merge pull request #4315 from django-oscar/fix-line-allocations

[Fix] Set num_allocated to quantity if None
master
Voxin Muyli 1 year ago
parent
commit
c818efbe5b
No account linked to committer's email address

+ 1
- 0
sandbox/fixtures/orders.json View File

@@ -59,6 +59,7 @@
59 59
         "unit_price_incl_tax": "7.99",
60 60
         "partner": 3,
61 61
         "order": 1,
62
+        "num_allocated": 1,
62 63
         "quantity": 1
63 64
     },
64 65
     "model": "order.line",

+ 2
- 4
src/oscar/apps/order/abstract_models.py View File

@@ -623,9 +623,7 @@ class AbstractLine(models.Model):
623 623
     # own business processes.
624 624
     status = models.CharField(_("Status"), max_length=255, blank=True)
625 625
 
626
-    num_allocated = models.PositiveIntegerField(
627
-        _("Number allocated"), blank=True, null=True
628
-    )
626
+    num_allocated = models.PositiveIntegerField(_("Number allocated"))
629 627
 
630 628
     # Checks whether line allocation was cancelled or not
631 629
     allocation_cancelled = models.BooleanField(default=False)
@@ -888,7 +886,7 @@ class AbstractLine(models.Model):
888 886
         if self.allocation_cancelled:
889 887
             return False
890 888
 
891
-        return quantity <= (self.num_allocated or 0)
889
+        return quantity <= self.num_allocated
892 890
 
893 891
     def consume_allocation(self, quantity):
894 892
         if not self.can_track_allocations:

+ 20
- 0
src/oscar/apps/order/migrations/0017_set_num_allocated_to_quantity.py View File

@@ -0,0 +1,20 @@
1
+# Generated by Django 4.2.9 on 2024-06-03 8:06
2
+from django.db import migrations
3
+
4
+
5
+def update_line_num_allocated(apps, schema_editor):
6
+    Line = apps.get_model("order", "Line")
7
+    for line in Line.objects.all():
8
+        if line.num_allocated is None:
9
+            line.num_allocated = line.quantity
10
+            line.save(update_fields=["num_allocated"])
11
+
12
+
13
+class Migration(migrations.Migration):
14
+    dependencies = [
15
+        ('order', '0016_line_allocation_cancelled_line_num_allocated'),
16
+    ]
17
+
18
+    operations = [
19
+        migrations.RunPython(update_line_num_allocated, migrations.RunPython.noop),
20
+    ]

+ 18
- 0
src/oscar/apps/order/migrations/0018_alter_line_num_allocated.py View File

@@ -0,0 +1,18 @@
1
+# Generated by Django 4.2.11 on 2024-06-14 15:44
2
+
3
+from django.db import migrations, models
4
+
5
+
6
+class Migration(migrations.Migration):
7
+
8
+    dependencies = [
9
+        ("order", "0017_set_num_allocated_to_quantity"),
10
+    ]
11
+
12
+    operations = [
13
+        migrations.AlterField(
14
+            model_name="line",
15
+            name="num_allocated",
16
+            field=models.PositiveIntegerField(verbose_name="Number allocated"),
17
+        ),
18
+    ]

+ 1
- 0
src/oscar/test/factories/order.py View File

@@ -106,6 +106,7 @@ class OrderLineFactory(factory.django.DjangoModelFactory):
106 106
     partner_sku = factory.LazyAttribute(lambda x: x.product.upc)
107 107
     stockrecord = factory.LazyAttribute(lambda s: s.product.stockrecords.first())
108 108
     quantity = 1
109
+    num_allocated = 1
109 110
 
110 111
     line_price_incl_tax = factory.LazyAttribute(
111 112
         lambda obj: tax_add(obj.stockrecord.price) * obj.quantity

+ 6
- 9
tests/integration/order/test_models.py View File

@@ -193,13 +193,6 @@ class LineTests(TestCase):
193 193
 
194 194
         self.assertTrue(actual)
195 195
 
196
-    def test_is_allocation_consumption_possible_when_num_allocated_is_null(self):
197
-        self.line.num_allocated = None
198
-
199
-        actual = self.line.is_allocation_consumption_possible(1)
200
-
201
-        self.assertFalse(actual)
202
-
203 196
     def event(self, event_type, quantity=None):
204 197
         """
205 198
         Creates a shipping event for the test line
@@ -447,8 +440,12 @@ class OrderTests(TestCase):
447 440
     def test_shipping_status(self):
448 441
         order = OrderFactory()
449 442
 
450
-        line_1 = OrderLineFactory(order=order, partner_sku="SKU1234", quantity=2)
451
-        line_2 = OrderLineFactory(order=order, partner_sku="SKU5678", quantity=1)
443
+        line_1 = OrderLineFactory(
444
+            order=order, partner_sku="SKU1234", quantity=2, num_allocated=2
445
+        )
446
+        line_2 = OrderLineFactory(
447
+            order=order, partner_sku="SKU5678", quantity=1, num_allocated=1
448
+        )
452 449
         self.assertEqual(order.shipping_status, "")
453 450
 
454 451
         event_1 = ShippingEventFactory(order=order, event_type__name="Shipped")

Loading…
Cancel
Save