Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Remove preorders, discounts and addresses in clearorders command (#11805)

Cezary Miącz450d5417549d
diff --git a/saleor/core/management/commands/clearorders.py b/saleor/core/management/commands/clearorders.py
index c58aee356f..33f5aa2cea 100644
--- a/saleor/core/management/commands/clearorders.py
+++ b/saleor/core/management/commands/clearorders.py
@@ -9,11 +9,17 @@ from django.core.management.base import BaseCommand
from ....account.models import Address, CustomerEvent, CustomerNote, User
from ....checkout.models import Checkout, CheckoutLine, CheckoutMetadata
+from ....discount.models import OrderDiscount
from ....giftcard.models import GiftCard, GiftCardEvent, GiftCardTag
from ....invoice.models import Invoice, InvoiceEvent
from ....order.models import Fulfillment, FulfillmentLine, Order, OrderEvent, OrderLine
from ....payment.models import Payment, Transaction, TransactionEvent, TransactionItem
-from ....warehouse.models import Allocation, Reservation
+from ....warehouse.models import (
+ Allocation,
+ PreorderAllocation,
+ PreorderReservation,
+ Reservation,
+)
class Command(BaseCommand):
@@ -23,27 +29,45 @@ class Command(BaseCommand):
parser.add_argument(
"--delete-customers",
action="store_true",
- help="Delete cutomers user accounts (doesn't delete staff accounts).",
+ help="Delete customers user accounts (doesn't delete staff accounts).",
)
def handle(self, **options):
self.delete_payments()
+ self.delete_allocations()
+ self.delete_reservations()
self.delete_checkouts()
self.delete_invoices()
self.delete_gift_cards()
self.delete_orders()
+ self.delete_unassigned_addresses()
should_delete_customers = options.get("delete_customers")
if should_delete_customers:
self.delete_customers()
- def delete_checkouts(self):
- metadata = CheckoutMetadata.objects.all()
- metadata._raw_delete(metadata.db) # type: ignore[attr-defined] # raw access # noqa: E501
+ def delete_allocations(self):
+ allocations = Allocation.objects.all()
+ allocations._raw_delete(allocations.db) # type: ignore[attr-defined] # raw access # noqa: E501
+ preorder_allocations = PreorderAllocation.objects.all()
+ preorder_allocations._raw_delete(preorder_allocations.db) # type: ignore[attr-defined] # raw access # noqa: E501
+
+ self.stdout.write("Removed allocations")
+
+ def delete_reservations(self):
reservations = Reservation.objects.all()
reservations._raw_delete(reservations.db) # type: ignore[attr-defined] # raw access # noqa: E501
+ preorder_reservations = PreorderReservation.objects.all()
+ preorder_reservations._raw_delete(preorder_reservations.db) # type: ignore[attr-defined] # raw access # noqa: E501
+
+ self.stdout.write("Removed reservations")
+
+ def delete_checkouts(self):
+ metadata = CheckoutMetadata.objects.all()
+ metadata._raw_delete(metadata.db) # type: ignore[attr-defined] # raw access # noqa: E501
+
checkout_lines = CheckoutLine.objects.all()
checkout_lines._raw_delete(checkout_lines.db) # type: ignore[attr-defined] # raw access # noqa: E501
@@ -85,15 +109,15 @@ class Command(BaseCommand):
self.stdout.write("Removed gift cards")
def delete_orders(self):
+ discounts = OrderDiscount.objects.all()
+ discounts._raw_delete(discounts.db) # type: ignore[attr-defined] # raw access # noqa: E501
+
fulfillment_lines = FulfillmentLine.objects.all()
fulfillment_lines._raw_delete(fulfillment_lines.db) # type: ignore[attr-defined] # raw access # noqa: E501
fulfillments = Fulfillment.objects.all()
fulfillments._raw_delete(fulfillments.db) # type: ignore[attr-defined] # raw access # noqa: E501
- allocations = Allocation.objects.all()
- allocations._raw_delete(allocations.db) # type: ignore[attr-defined] # raw access # noqa: E501
-
order_lines = OrderLine.objects.all()
order_lines._raw_delete(order_lines.db) # type: ignore[attr-defined] # raw access # noqa: E501
@@ -107,6 +131,15 @@ class Command(BaseCommand):
orders._raw_delete(orders.db) # type: ignore[attr-defined] # raw access # noqa: E501
self.stdout.write("Removed orders")
+ def delete_unassigned_addresses(self):
+ addresses = Address.objects.filter(
+ user_addresses__isnull=True,
+ warehouse__isnull=True,
+ sitesettings__isnull=True,
+ )
+ addresses._raw_delete(addresses.db) # type: ignore[attr-defined] # raw access # noqa: E501
+ self.stdout.write("Removed unassigned addresses")
+
def delete_customers(self):
customers = User.objects.filter(is_staff=False, is_superuser=False)
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.py
index 295e3540a9..31b190a178 100644
--- a/saleor/core/utils/random_data.py
+++ b/saleor/core/utils/random_data.py
@@ -951,6 +951,7 @@ def _create_staff_user(staff_password, email=None, superuser=False):
User(email=email, first_name=first_name, last_name=last_name), address
),
)
+ staff_user.addresses.add(address)
return staff_user