Skip to content

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

Add voucher generator for populatedb

Krzysztof Wolski78935a74e0db
diff --git a/saleor/core/management/commands/populatedb.py b/saleor/core/management/commands/populatedb.py
index 88dc08c47d..c0ecfdd86e 100644
--- a/saleor/core/management/commands/populatedb.py
+++ b/saleor/core/management/commands/populatedb.py
@@ -4,7 +4,7 @@ from django.db import connection
from ...utils.random_data import (
create_orders, create_users, create_shipping_methods,
- create_products_by_schema, create_product_sales)
+ create_products_by_schema, create_product_sales, create_vouchers)
from ...utils import create_superuser
@@ -55,6 +55,8 @@ class Command(BaseCommand):
stdout=self.stdout)
for msg in create_product_sales(5):
self.stdout.write(msg)
+ for msg in create_vouchers():
+ self.stdout.write(msg)
for msg in create_users(20):
self.stdout.write(msg)
for msg in create_orders(20):
diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.py
index c26c7f8b8c..3842c4393b 100644
--- a/saleor/core/utils/random_data.py
+++ b/saleor/core/utils/random_data.py
@@ -13,7 +13,7 @@ from faker import Factory
from faker.providers import BaseProvider
from prices import Price
-from saleor.discount.models import Sale
+from ...discount.models import Sale, Voucher
from ...order.models import DeliveryGroup, Order, OrderedItem, Payment
from ...product.models import (AttributeChoiceValue, Category, Product,
ProductAttribute, ProductClass, ProductImage,
@@ -453,3 +453,28 @@ def create_shipping_methods():
shipping_method = ShippingMethod.objects.create(name='DHL')
shipping_method.price_per_country.create(price=fake.price())
yield 'Shipping method #%d' % shipping_method.id
+
+
+def create_vouchers():
+ voucher, created = Voucher.objects.get_or_create(
+ code='FREESHIPPING', defaults={
+ 'type': Voucher.SHIPPING_TYPE,
+ 'name': 'Free shipping',
+ 'discount_value_type': Voucher.DISCOUNT_VALUE_PERCENTAGE,
+ 'discount_value': 100})
+ if created:
+ yield 'Voucher #%d' % voucher.id
+ else:
+ yield 'Shipping voucher already exists'
+
+ voucher, created = Voucher.objects.get_or_create(
+ code='DISCOUNT', defaults={
+ 'type': Voucher.VALUE_TYPE,
+ 'name': 'Big order discount',
+ 'discount_value_type': Voucher.DISCOUNT_VALUE_FIXED,
+ 'discount_value': 25,
+ 'limit': 200})
+ if created:
+ yield 'Voucher #%d' % voucher.id
+ else:
+ yield 'Value voucher already exists'
diff --git a/tests/test_core.py b/tests/test_core.py
index cf7f7a64bb..714ace1e91 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -11,8 +11,9 @@ from saleor.core.utils.random_data import (create_address, create_attribute,
create_users,
create_orders,
create_products_by_schema,
- create_product_sales)
-from saleor.discount.models import Sale
+ create_product_sales,
+ create_vouchers)
+from saleor.discount.models import Sale, Voucher
from saleor.order.models import Order
from saleor.product.models import Product
from saleor.shipping.models import ShippingMethod
@@ -143,3 +144,10 @@ def test_create_product_sales(db):
for _ in create_product_sales(how_many):
pass
assert Sale.objects.all().count() == 5
+
+
+def test_create_vouchers(db):
+ assert Voucher.objects.all().count() == 0
+ for _ in create_vouchers():
+ pass
+ assert Voucher.objects.all().count() == 2