Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Populate shipping methods
Michał Ociepkabca4148e103f
diff --git a/saleor/core/management/commands/populatedb.py b/saleor/core/management/commands/populatedb.pyindex 791d67b5f1..dceb2650ca 100644--- a/saleor/core/management/commands/populatedb.py+++ b/saleor/core/management/commands/populatedb.py@@ -2,7 +2,8 @@ from django.core.management.base import BaseCommandfrom django.db import connectionfrom ....userprofile.models import User-from ...utils.random_data import create_items, create_orders, create_users+from ...utils.random_data import (+ create_items, create_orders, create_users, create_shipping_methods)class Command(BaseCommand):@@ -37,6 +38,8 @@ class Command(BaseCommand):def handle(self, *args, **options):self.make_database_faster()create_images = not options['withoutimages']+ for msg in create_shipping_methods():+ self.stdout.write(msg)for msg in create_items(self.placeholders_dir, 40, create_images):self.stdout.write(msg)for msg in create_users(20):diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.pyindex c21b809c8e..7c04fc2df6 100644--- a/saleor/core/utils/random_data.py+++ b/saleor/core/utils/random_data.py@@ -10,20 +10,28 @@ from faker import Factoryfrom faker.providers import BaseProviderfrom prices import Price+from ...shipping.models import ShippingMethod, ShippingMethodCountryfrom ...order.models import DeliveryGroup, Order, OrderedItem, Payment-from ...product.models import (Category, Product, ProductImage, ProductVariant,- Stock)+from ...product.models import Category, Product, ProductImage, ProductVariant, Stockfrom ...userprofile.models import Address, Userfake = Factory.create()STOCK_LOCATION = 'default'+DELIVERY_REGIONS = [ShippingMethodCountry.ANY_COUNTRY, 'US', 'PL', 'DE', 'GB']-class FakePriceProvider(BaseProvider):++class SaleorProvider(BaseProvider):def price(self):return Price(fake.pydecimal(2, 2, positive=True),currency=settings.DEFAULT_CURRENCY)-fake.add_provider(FakePriceProvider)++ def delivery_region(self):+ return random.choice(DELIVERY_REGIONS)++ def shipping_method(self):+ return random.choice(ShippingMethod.objects.all())+fake.add_provider(SaleorProvider)def get_email(first_name, last_name):@@ -151,10 +159,17 @@ def create_payment(delivery_group):def create_delivery_group(order):+ region = order.shipping_address.country+ if region not in DELIVERY_REGIONS:+ region = ShippingMethodCountry.ANY_COUNTRY+ shipping_method = fake.shipping_method()+ shipping_country = shipping_method.price_per_country.get_or_create(+ country_code=region, defaults={'price': fake.price()})[0]delivery_group = DeliveryGroup.objects.create(status=random.choice(['new', 'shipped']),order=order,- shipping_price=fake.price())+ shipping_method_name=str(shipping_country),+ shipping_price=shipping_country.get_total())return delivery_group@@ -191,9 +206,7 @@ def create_fake_order():'shipping_address': address,'anonymous_user_email': get_email(address.first_name, address.last_name)}- order = Order.objects.create(- shipping_method='dummy',- **user_data)+ order = Order.objects.create(**user_data)order.change_status('payment-pending')delivery_group = create_delivery_group(order)@@ -221,3 +234,12 @@ def create_orders(how_many=10):for dummy in range(how_many):order = create_fake_order()yield 'Order: %s' % (order,)+++def create_shipping_methods():+ shipping_method = ShippingMethod.objects.create(name='UPC')+ shipping_method.price_per_country.create(price=fake.price())+ yield 'Shipping method #%d' % shipping_method.id+ shipping_method = ShippingMethod.objects.create(name='DHL')+ shipping_method.price_per_country.create(price=fake.price())+ yield 'Shipping method #%d' % shipping_method.id