Skip to content

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

Add stock and warehouse in populatedb

Mira Szewczyk1185fae1f98c
diff --git a/saleor/core/management/commands/populatedb.py b/saleor/core/management/commands/populatedb.py
index e17cd45143..5075c2ad14 100644
--- a/saleor/core/management/commands/populatedb.py
+++ b/saleor/core/management/commands/populatedb.py
@@ -17,6 +17,7 @@ from ...utils.random_data import (
create_shipping_zones,
create_users,
create_vouchers,
+ create_warehouses,
set_homepage_collection,
)
@@ -87,6 +88,8 @@ class Command(BaseCommand):
create_images = not options["withoutimages"]
for msg in create_shipping_zones():
self.stdout.write(msg)
+ create_warehouses()
+ self.stdout.write("Created warehouses")
create_products_by_schema(self.placeholders_dir, create_images)
self.stdout.write("Created products")
for msg in create_product_sales(5):
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.py
index 337031dff9..cc43036e2a 100644
--- a/saleor/core/utils/random_data.py
+++ b/saleor/core/utils/random_data.py
@@ -56,6 +56,9 @@ from ...product.thumbnails import (
create_product_thumbnails,
)
from ...shipping.models import ShippingMethod, ShippingMethodType, ShippingZone
+from ...stock.models import Stock
+from ...stock.utils.management import increase_stock
+from ...warehouse.models import Warehouse
fake = Factory.create()
PRODUCTS_LIST_DIR = "products-list/"
@@ -209,6 +212,16 @@ def create_products(products_data, placeholder_dir, create_images):
create_product_image(product, placeholder_dir, image_name)
+def create_stocks(variant, warehouse_qs=None, **defaults):
+ if warehouse_qs is None:
+ warehouse_qs = Warehouse.objects.all()
+
+ for warehouse in warehouse_qs:
+ Stock.objects.update_or_create(
+ warehouse=warehouse, product_variant=variant, defaults=defaults
+ )
+
+
def create_product_variants(variants_data):
for variant in variants_data:
pk = variant["pk"]
@@ -221,7 +234,10 @@ def create_product_variants(variants_data):
defaults["product_id"] = product_id
set_field_as_money(defaults, "price_override")
set_field_as_money(defaults, "cost_price")
- ProductVariant.objects.update_or_create(pk=pk, defaults=defaults)
+ quantity = defaults.pop("quantity")
+ quantity_allocated = defaults.pop("quantity_allocated")
+ variant, _ = ProductVariant.objects.update_or_create(pk=pk, defaults=defaults)
+ create_stocks(variant, quantity=quantity, quantity_allocated=quantity_allocated)
def assign_attributes_to_product_types(
@@ -426,12 +442,15 @@ def create_order_lines(order, discounts, how_many=10):
)
variants_iter = itertools.cycle(variants)
lines = []
+ stocks = []
+ country = order.shipping_address.country
for dummy in range(how_many):
variant = next(variants_iter)
product = variant.product
quantity = random.randrange(1, 5)
- variant.quantity += quantity
- variant.quantity_allocated += quantity
+ stocks.append(
+ increase_stock(variant, country, quantity, allocate=True, commit=False)
+ )
unit_price = variant.get_price(discounts)
unit_price = TaxedMoney(net=unit_price, gross=unit_price)
lines.append(
@@ -447,7 +466,7 @@ def create_order_lines(order, discounts, how_many=10):
tax_rate=0,
)
)
- ProductVariant.objects.bulk_update(variants, ["quantity", "quantity_allocated"])
+ Stock.objects.bulk_update(stocks, ["quantity", "quantity_allocated"])
lines = OrderLine.objects.bulk_create(lines)
manager = get_extensions_manager()
for line in lines:
@@ -863,6 +882,15 @@ def create_shipping_zones():
)
+def create_warehouses():
+ for shipping_zone in ShippingZone.objects.all():
+ warehouse, _ = Warehouse.objects.update_or_create(
+ name=shipping_zone.name,
+ defaults={"company_name": fake.company(), "address": create_address()},
+ )
+ warehouse.shipping_zones.add(shipping_zone)
+
+
def create_vouchers():
voucher, created = Voucher.objects.get_or_create(
code="FREESHIPPING",
diff --git a/saleor/stock/utils/management.py b/saleor/stock/utils/management.py
index 337533612c..69e5aa5d15 100644
--- a/saleor/stock/utils/management.py
+++ b/saleor/stock/utils/management.py
@@ -6,21 +6,37 @@ if TYPE_CHECKING:
from ..product.models import ProductVariant
-def allocate_stock(variant: "ProductVariant", country_code: str, quantity: int):
+def allocate_stock(
+ variant: "ProductVariant", country_code: str, quantity: int, commit: bool = True
+) -> Stock:
stock = Stock.objects.for_country(country_code).get(product_variant=variant)
- stock.allocate_stock(quantity)
+ stock.allocate_stock(quantity, commit=commit)
+ return stock
-def deallocate_stock(variant: "ProductVariant", country_code: str, quantity: int):
+def deallocate_stock(
+ variant: "ProductVariant", country_code: str, quantity: int, commit: bool = True
+) -> Stock:
stock = Stock.objects.for_country(country_code).get(product_variant=variant)
- stock.deallocate_stock(quantity)
+ stock.deallocate_stock(quantity, commit=commit)
+ return stock
-def increase_stock(variant: "ProductVariant", country_code: str, quantity: int):
+def increase_stock(
+ variant: "ProductVariant",
+ country_code: str,
+ quantity: int,
+ allocate: bool = False,
+ commit: bool = True,
+) -> Stock:
stock = Stock.objects.for_country(country_code).get(product_variant=variant)
- stock.increase_stock(quantity)
+ stock.increase_stock(quantity, allocate=allocate, commit=commit)
+ return stock
-def decrease_stock(variant: "ProductVariant", country_code: str, quantity: int):
+def decrease_stock(
+ variant: "ProductVariant", country_code: str, quantity: int, commit: bool = True
+) -> Stock:
stock = Stock.objects.for_country(country_code).get(product_variant=variant)
- stock.decrease_stock(quantity)
+ stock.decrease_stock(quantity, commit=commit)
+ return stock