Skip to content

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

Add deprecated type on voucher type

fowczarekc4f2c25bb161
diff --git a/saleor/dashboard/discount/forms.py b/saleor/dashboard/discount/forms.py
index 11fef4bd15..8084f0ad9c 100644
--- a/saleor/dashboard/discount/forms.py
+++ b/saleor/dashboard/discount/forms.py
@@ -128,7 +128,7 @@ class ShippingVoucherForm(forms.ModelForm):
fields = ["countries", "min_amount_spent"]
-class ValueVoucherForm(forms.ModelForm):
+class EntireOrderVoucherForm(forms.ModelForm):
min_amount_spent = MinAmountSpent
class Meta:
@@ -164,12 +164,16 @@ class SpecificProductVoucherForm(CommonVoucherForm):
queryset=Product.objects.all(),
fetch_data_url=reverse_lazy("dashboard:ajax-products"),
required=True,
- label=pgettext_lazy("Product", "Products"),
+ label=pgettext_lazy(
+ "Products that can be discounted with the voucher", "Products"
+ ),
)
categories = TreeNodeMultipleChoiceField(
queryset=Category.objects.all(),
required=True,
- label=pgettext_lazy("Categories", "Categories"),
+ label=pgettext_lazy(
+ "Categories that can be discounted with the voucher", "Categories"
+ ),
)
class Meta:
@@ -181,7 +185,11 @@ class SpecificProductVoucherForm(CommonVoucherForm):
"apply_once_per_order",
"min_amount_spent",
]
- labels = {"collections": pgettext_lazy("Collections", "Collections")}
+ labels = {
+ "collections": pgettext_lazy(
+ "Collections that can be discounted with the voucher", "Collections"
+ )
+ }
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
diff --git a/saleor/dashboard/discount/views.py b/saleor/dashboard/discount/views.py
index 2ddc653200..a763693a47 100644
--- a/saleor/dashboard/discount/views.py
+++ b/saleor/dashboard/discount/views.py
@@ -22,7 +22,7 @@ def get_voucher_type_forms(voucher, data):
VoucherType.SHIPPING: forms.ShippingVoucherForm(
data or None, instance=voucher, prefix=VoucherType.SHIPPING
),
- VoucherType.ENTIRE_ORDER: forms.ValueVoucherForm(
+ VoucherType.ENTIRE_ORDER: forms.EntireOrderVoucherForm(
data or None, instance=voucher, prefix=VoucherType.ENTIRE_ORDER
),
VoucherType.SPECIFIC_PRODUCT: forms.SpecificProductVoucherForm(
diff --git a/saleor/discount/__init__.py b/saleor/discount/__init__.py
index 8dc87dc67f..4eb1941514 100644
--- a/saleor/discount/__init__.py
+++ b/saleor/discount/__init__.py
@@ -20,6 +20,7 @@ class VoucherType:
COLLECTION = "collection"
CATEGORY = "category"
SHIPPING = "shipping"
+ VALUE = "value"
ENTIRE_ORDER = "entire_order"
SPECIFIC_PRODUCT = "specific_product"
diff --git a/saleor/discount/migrations/0014_auto_20190628_0154.py b/saleor/discount/migrations/0014_auto_20190701_0402.py
similarity index 72%
rename from saleor/discount/migrations/0014_auto_20190628_0154.py
rename to saleor/discount/migrations/0014_auto_20190701_0402.py
index d83461197e..8fdb0c782f 100644
--- a/saleor/discount/migrations/0014_auto_20190628_0154.py
+++ b/saleor/discount/migrations/0014_auto_20190701_0402.py
@@ -1,13 +1,21 @@
-# Generated by Django 2.2.2 on 2019-06-28 06:54
+# Generated by Django 2.2.2 on 2019-07-01 09:02
from django.db import migrations, models
+from saleor.discount import VoucherType
+
+
+def replace_value_vocucher_type(apps, schema_editor):
+ cls = apps.get_model("discount", "Voucher")
+ cls.objects.filter(type="value").update(type=VoucherType.ENTIRE_ORDER)
+
class Migration(migrations.Migration):
dependencies = [("discount", "0013_auto_20190618_0733")]
operations = [
+ migrations.RunPython(replace_value_vocucher_type),
migrations.AlterField(
model_name="voucher",
name="type",
@@ -26,5 +34,5 @@ class Migration(migrations.Migration):
default="entire_order",
max_length=20,
),
- )
+ ),
]
diff --git a/saleor/graphql/discount/enums.py b/saleor/graphql/discount/enums.py
index a4a23db8eb..e6c6808ade 100644
--- a/saleor/graphql/discount/enums.py
+++ b/saleor/graphql/discount/enums.py
@@ -13,6 +13,7 @@ class VoucherTypeEnum(graphene.Enum):
COLLECTION = VoucherType.COLLECTION
CATEGORY = VoucherType.CATEGORY
SHIPPING = VoucherType.SHIPPING
+ VALUE = VoucherType.VALUE
ENTIRE_ORDER = VoucherType.ENTIRE_ORDER
SPECIFIC_PRODUCT = VoucherType.SPECIFIC_PRODUCT
diff --git a/saleor/graphql/discount/mutations.py b/saleor/graphql/discount/mutations.py
index f2cdc7bad9..c6cef7238a 100644
--- a/saleor/graphql/discount/mutations.py
+++ b/saleor/graphql/discount/mutations.py
@@ -67,9 +67,10 @@ class BaseDiscountCatalogueMutation(BaseMutation):
class VoucherInput(graphene.InputObjectType):
type = VoucherTypeEnum(
description=(
- "Voucher type: product, category shipping or entire order. "
- "Deprecated: PRODUCT, COLLECTION, CATEGORY are deprecated, "
- "use SPECIFIC_PRODUCT instead."
+ "Voucher type: PRODUCT, CATEGORY SHIPPING or ENTIRE_ORDER. "
+ "Deprecated fields: "
+ "PRODUCT, COLLECTION, CATEGORY use SPECIFIC_PRODUCT instead. "
+ "VALUE use ENTIRE_ORDER instead."
)
)
name = graphene.String(description="Voucher name.")
@@ -127,6 +128,9 @@ class VoucherCreate(ModelMutation):
data["code"] = generate_promo_code()
elif not is_available_promo_code(code):
raise PromoCodeAlreadyExists()
+ voucher_type = data.get("type", None)
+ if voucher_type == VoucherTypeEnum.VALUE:
+ data["type"] = VoucherTypeEnum.ENTIRE_ORDER.value
cleaned_input = super().clean_input(info, instance, data)
return cleaned_input
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 8bd224e4c2..35438646a9 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -3216,6 +3216,7 @@ enum VoucherTypeEnum {
COLLECTION
CATEGORY
SHIPPING
+ VALUE
ENTIRE_ORDER
SPECIFIC_PRODUCT
}
diff --git a/tests/api/test_discount.py b/tests/api/test_discount.py
index fa138dd7db..7e2744d16d 100644
--- a/tests/api/test_discount.py
+++ b/tests/api/test_discount.py
@@ -222,6 +222,32 @@ def test_create_voucher_with_empty_code(staff_api_client, permission_manage_disc
assert data["code"] != ""
+def test_create_voucher_with_deprecated_type(
+ staff_api_client, permission_manage_discounts
+):
+ start_date = timezone.now() - timedelta(days=365)
+ end_date = timezone.now() + timedelta(days=365)
+ variables = {
+ "name": "test voucher",
+ "type": VoucherTypeEnum.VALUE.name,
+ "code": "",
+ "discountValueType": DiscountValueTypeEnum.FIXED.name,
+ "discountValue": 10.12,
+ "minAmountSpent": 1.12,
+ "startDate": start_date.isoformat(),
+ "endDate": end_date.isoformat(),
+ }
+
+ response = staff_api_client.post_graphql(
+ CREATE_VOUCHER_MUTATION, variables, permissions=[permission_manage_discounts]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["voucherCreate"]["voucher"]
+ assert data["name"] == variables["name"]
+ assert data["code"] != ""
+ assert data["type"] == VoucherTypeEnum.ENTIRE_ORDER.name
+
+
def test_create_voucher_with_existing_gift_card_code(
staff_api_client, gift_card, permission_manage_discounts
):