Skip to content

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

Add ability to generate orders and payments

Adam Bogdał638acd62f564
diff --git a/saleor/product/management/commands/populatedb.py b/saleor/product/management/commands/populatedb.py
index a5c273c38d..2db57018be 100644
--- a/saleor/product/management/commands/populatedb.py
+++ b/saleor/product/management/commands/populatedb.py
@@ -1,8 +1,6 @@
-from os.path import exists
+from django.core.management.base import BaseCommand
-from django.core.management.base import BaseCommand, CommandError
-
-from utils.create_random_data import create_items, create_users
+from utils.create_random_data import create_items, create_users, create_orders
from saleor.userprofile.models import User
@@ -24,6 +22,8 @@ class Command(BaseCommand):
self.stdout.write(msg)
for msg in create_users(10):
self.stdout.write(msg)
+ for msg in create_orders(20):
+ self.stdout.write(msg)
if options['createsuperuser']:
user = User.objects.create_superuser(
diff --git a/utils/create_random_data.py b/utils/create_random_data.py
index 6fbd52ec92..bbafaa6671 100644
--- a/utils/create_random_data.py
+++ b/utils/create_random_data.py
@@ -4,8 +4,11 @@ import random
import unicodedata
from faker import Factory
+from django.conf import settings
from django.core.files import File
+from saleor.order import Order
+from saleor.order.models import OrderedItem, DeliveryGroup, Payment
from saleor.product.models import (Product, ProductVariant, ProductImage, Stock)
from saleor.product.models import Category
from saleor.userprofile.models import User, Address
@@ -14,6 +17,13 @@ fake = Factory.create()
STOCK_LOCATION = 'default'
+def get_email(first_name, last_name):
+ _first = unicodedata.normalize('NFD', first_name).encode('ascii', 'ignore')
+ _last = unicodedata.normalize('NFD', last_name).encode('ascii', 'ignore')
+ return '%s.%[email protected]' % (
+ _first.lower().decode('utf-8'), _last.lower().decode('utf-8'))
+
+
def get_or_create_category(name, **kwargs):
defaults = {
'description': fake.text()
@@ -102,10 +112,7 @@ def create_fake_user():
first_name = fake.first_name()
last_name = fake.last_name()
- _first = unicodedata.normalize('NFD', first_name).encode('ascii', 'ignore')
- _last = unicodedata.normalize('NFD', last_name).encode('ascii', 'ignore')
-
- email = u'%s.%[email protected]' % (_first.lower(), _last.lower())
+ email = get_email(first_name, last_name)
user = User.objects.create_user(email=email, password='password')
@@ -125,7 +132,100 @@ def create_fake_user():
return user
+def create_payment(delivery_group):
+ order = delivery_group.order
+ status = random.choice(['waiting', 'preauth', 'confirmed'])
+ payment = Payment.objects.create(
+ order=order,
+ status=status,
+ variant='dummy',
+ transaction_id=str(fake.random_int(1, 100000)),
+ currency=settings.DEFAULT_CURRENCY,
+ total=order.get_total().gross,
+ delivery=delivery_group.shipping_price.gross,
+ customer_ip_address=fake.ipv4(),
+ billing_first_name=order.billing_address.first_name,
+ billing_last_name=order.billing_address.last_name,
+ billing_address_1=order.billing_address.street_address_1,
+ billing_city=order.billing_address.city,
+ billing_postcode=order.billing_address.postal_code,
+ billing_country_code=order.billing_address.country)
+ if status == 'confirmed':
+ payment.captured_amount = payment.total
+ payment.save()
+ return payment
+
+
+def create_delivery_group(order):
+ delivery_group = DeliveryGroup.objects.create(
+ status=random.choice(['new', 'shipped']),
+ order=order,
+ shipping_price=fake.pyfloat(1, 2, positive=True))
+ return delivery_group
+
+
+def create_order_line(delivery_group):
+ product = Product.objects.all().order_by('?')[0]
+ variant = product.variants.all()[0]
+ OrderedItem.objects.create(
+ delivery_group=delivery_group,
+ product=product,
+ product_name=product.name,
+ product_sku=variant.sku,
+ quantity=random.randrange(1, 5),
+ unit_price_net=product.price.net,
+ unit_price_gross=product.price.gross)
+
+
+def create_order_lines(delivery_group, how_many=10):
+ for i in range(how_many):
+ create_order_line(delivery_group)
+
+
+def create_fake_order():
+ user = random.choice([None, User.objects.filter(
+ is_superuser=False).order_by('?')[0]])
+ if user:
+ user_data = {
+ 'user': user,
+ 'billing_address': user.default_billing_address,
+ 'shipping_address': user.default_shipping_address}
+ else:
+ address = Address.objects.create(
+ first_name=fake.first_name(),
+ last_name=fake.last_name(),
+ street_address_1=fake.street_address(),
+ city=fake.city(),
+ postal_code=fake.postcode(),
+ country=fake.country_code())
+ user_data = {
+ 'billing_address': address,
+ 'shipping_address': address,
+ 'anonymous_user_email': get_email(
+ address.first_name, address.last_name)}
+ order = Order.objects.create(
+ shipping_method='dummy',
+ **user_data)
+ order.change_status('payment-pending')
+
+ delivery_group = create_delivery_group(order)
+ create_order_lines(delivery_group, random.randrange(1, 5))
+
+ payment = create_payment(delivery_group)
+ if payment.status == 'confirmed':
+ order.change_status('fully-paid')
+ if random.choice([True, False]):
+ order.change_status('shipped')
+ return order
+
+
def create_users(how_many=10):
for i in range(how_many):
user = create_fake_user()
yield "User - %s" % user.email
+
+
+def create_orders(how_many=10):
+ for i in range(how_many):
+ order = create_fake_order()
+ yield "Order - %s" % order