Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix relation between order and click and collect address (#15697)
* Create a copy of collection point address during checkout completion * Add migration for creating order shipping addresses
- Loading branch information
1 parent
ce4b317
commit 47cedfd
Showing
10 changed files
with
140 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from django.db import migrations | ||
from django.db.models import Exists, OuterRef | ||
from django.forms.models import model_to_dict | ||
|
||
from .tasks.saleor3_19 import update_order_addresses_task | ||
|
||
# The batch of size 250 takes ~0.5 second and consumes ~20MB memory at peak | ||
ADDRESS_UPDATE_BATCH_SIZE = 250 | ||
|
||
|
||
def update_order_addresses(apps, schema_editor): | ||
Order = apps.get_model("order", "Order") | ||
Warehouse = apps.get_model("warehouse", "Warehouse") | ||
Address = apps.get_model("account", "Address") | ||
qs = Order.objects.filter( | ||
Exists(Warehouse.objects.filter(address_id=OuterRef("shipping_address_id"))), | ||
) | ||
order_ids = qs.values_list("pk", flat=True)[:ADDRESS_UPDATE_BATCH_SIZE] | ||
addresses = [] | ||
if order_ids: | ||
orders = Order.objects.filter(id__in=order_ids) | ||
for order in orders: | ||
if cc_address := order.shipping_address: | ||
order_address = Address(**model_to_dict(cc_address, exclude=["id"])) | ||
order.shipping_address = order_address | ||
addresses.append(order_address) | ||
Address.objects.bulk_create(addresses, ignore_conflicts=True) | ||
Order.objects.bulk_update(orders, ["shipping_address"]) | ||
update_order_addresses_task.delay() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("order", "0171_order_order_user_email_user_id_idx"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(update_order_addresses, migrations.RunPython.noop), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Generated by Django 3.2.22 on 2024-03-25 13:15 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("order", "0172_update_order_cc_addresses"), | ||
("order", "0175_merge_20231122_1040"), | ||
] | ||
|
||
operations = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Generated by Django 3.2.22 on 2024-03-25 13:29 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("order", "0176_merge_20231122_1346"), | ||
("order", "0176_merge_20240325_1315"), | ||
] | ||
|
||
operations = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Generated by Django 3.2.22 on 2024-03-25 13:33 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("order", "0177_merge_20240325_1329"), | ||
("order", "0179_merge_20231122_1348"), | ||
] | ||
|
||
operations = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Generated by Django 3.2.22 on 2024-03-25 13:38 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("order", "0180_merge_20240325_1333"), | ||
("order", "0181_order_subtotal_as_a_field"), | ||
] | ||
|
||
operations = [] |
12 changes: 12 additions & 0 deletions
12
saleor/order/migrations/0184_merge_0182_merge_20240325_1338_0183_order_tax_error.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Generated by Django 3.2.22 on 2024-03-25 13:42 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("order", "0182_merge_20240325_1338"), | ||
("order", "0183_order_tax_error"), | ||
] | ||
|
||
operations = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from django.db.models import Exists, OuterRef | ||
from django.forms.models import model_to_dict | ||
|
||
from ....account.models import Address | ||
from ....celeryconf import app | ||
from ....warehouse.models import Warehouse | ||
from ...models import Order | ||
|
||
# The batch of size 250 takes ~0.5 second and consumes ~20MB memory at peak | ||
ADDRESS_UPDATE_BATCH_SIZE = 250 | ||
|
||
|
||
@app.task | ||
def update_order_addresses_task(): | ||
qs = Order.objects.filter( | ||
Exists(Warehouse.objects.filter(address_id=OuterRef("shipping_address_id"))), | ||
) | ||
order_ids = qs.values_list("pk", flat=True)[:ADDRESS_UPDATE_BATCH_SIZE] | ||
addresses = [] | ||
if order_ids: | ||
orders = Order.objects.filter(id__in=order_ids) | ||
for order in orders: | ||
if cc_address := order.shipping_address: | ||
order_address = Address(**model_to_dict(cc_address, exclude=["id"])) | ||
order.shipping_address = order_address | ||
addresses.append(order_address) | ||
Address.objects.bulk_create(addresses, ignore_conflicts=True) | ||
Order.objects.bulk_update(orders, ["shipping_address"]) | ||
update_order_addresses_task.delay() |