Repository: saleor/saleor

Issue:
Have `populatedb` create random orders and payments
When learning how a store works having example data in place helps a lot. The same is true when working on design of pages that depend on certain data being in place.



Discussion:

Resolving patch:
diff --git a/saleor/product/management/commands/populatedb.py b/saleor/product/management/commands/populatedb.py
--- 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 @@ def handle(self, *args, **options):
             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
--- a/utils/create_random_data.py
+++ b/utils/create_random_data.py
@@ -4,8 +4,11 @@
 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 @@
 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.%s@example.com' % (
+        _first.lower().decode('utf-8'), _last.lower().decode('utf-8'))
+
+
 def get_or_create_category(name, **kwargs):
     defaults = {
         'description': fake.text()
@@ -98,24 +108,22 @@ def create_items(placeholder_dir, how_many=10):
         yield "Product - %s %s Variants" % (product, product.variants.count())
 
 
-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.%s@example.com' % (_first.lower(), _last.lower())
-
-    user = User.objects.create_user(email=email, password='password')
-
+def create_address():
     address = Address.objects.create(
-        first_name=first_name,
-        last_name=last_name,
+        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())
+    return address
+
+
+def create_fake_user():
+    address = create_address()
+    email = get_email(address.first_name, address.last_name)
+
+    user = User.objects.create_user(email=email, password='password')
 
     user.addresses.add(address)
     user.default_billing_address = address
@@ -125,7 +133,94 @@ 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 = create_address()
+        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


Test patch: