Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Improve vouchers dashboard UX
Michał Ociepka45e5cef56545
diff --git a/saleor/dashboard/discount/forms.py b/saleor/dashboard/discount/forms.pyindex 429b442ea8..f06bd3a320 100644--- a/saleor/dashboard/discount/forms.py+++ b/saleor/dashboard/discount/forms.py@@ -1,6 +1,8 @@+import uuid+from django import formsfrom django.conf import settings-from django.utils.translation import pgettext_lazy+from django.utils.translation import pgettext_lazy, gettext as _from django_prices.forms import PriceFieldfrom ...discount.models import Sale, Voucher@@ -43,9 +45,17 @@ class VoucherForm(forms.ModelForm):instance = kwargs.get('instance')if instance and instance.type == Voucher.BASKET_TYPE and instance.limit is None:initial['type'] = VoucherForm.ALL_BASKETS+ if instance and instance.id is None and not initial.get('code'):+ initial['code'] = self._generate_codekwargs['initial'] = initialsuper(VoucherForm, self).__init__(*args, **kwargs)+ def _generate_code(self):+ while True:+ code = str(uuid.uuid4()).replace('-', '').upper()[:12]+ if not Voucher.objects.filter(code=code).exists():+ return code+def clean(self):cleaned_data = super(VoucherForm, self).clean()if cleaned_data['type'] == VoucherForm.ALL_BASKETS:@@ -87,7 +97,7 @@ class BasketVoucherForm(forms.ModelForm):limit = PriceField(min_value=0, required=True, currency = settings.DEFAULT_CURRENCY,- label=pgettext_lazy('voucher_form', 'Basket total equals or greater than'))+ label=pgettext_lazy('voucher_form', 'Basket total equal or greater than'))class Meta:model = Voucher@@ -102,30 +112,36 @@ class BasketVoucherForm(forms.ModelForm):class ProductVoucherForm(forms.ModelForm):- APPLY_TO_PRODUCT_CHOICES = (- (Voucher.APPLY_TO_ONE_PRODUCT,- pgettext_lazy('voucher_form', 'Apply only once')),- (Voucher.APPLY_TO_ALL_PRODUCTS,- pgettext_lazy('voucher_form', 'Apply to all matching products')))-- apply_to = forms.ChoiceField(choices=APPLY_TO_PRODUCT_CHOICES, required=False)+ apply_to = forms.ChoiceField(+ choices=Voucher.APPLY_TO_PRODUCT_CHOICES, required=False)class Meta:model = Voucherfields = ['product', 'apply_to']+ def __init__(self, *args, **kwargs):+ super(ProductVoucherForm, self).__init__(*args, **kwargs)+ self.fields['product'].required = True+def save(self, commit=True):self.instance.category = Noneself.instance.limit = Nonereturn super(ProductVoucherForm, self).save(commit)-class CategoryVoucherForm(ProductVoucherForm):+class CategoryVoucherForm(forms.ModelForm):++ apply_to = forms.ChoiceField(+ choices=Voucher.APPLY_TO_PRODUCT_CHOICES, required=False)class Meta:model = Voucherfields = ['category', 'apply_to']+ def __init__(self, *args, **kwargs):+ super(CategoryVoucherForm, self).__init__(*args, **kwargs)+ self.fields['category'].required = True+def save(self, commit=True):self.instance.limit = Noneself.instance.product = Nonediff --git a/saleor/dashboard/templates/dashboard/discount/voucher_form.html b/saleor/dashboard/templates/dashboard/discount/voucher_form.htmlindex 378def9fc3..2ecd6c4b8d 100644--- a/saleor/dashboard/templates/dashboard/discount/voucher_form.html+++ b/saleor/dashboard/templates/dashboard/discount/voucher_form.html@@ -21,9 +21,8 @@</a><ul id="voucher-delete" class="dropdown-content"><li>- <a class="modal-trigger-custom" href="#base-modal"- data-href="{% url 'dashboard:voucher-delete' pk=voucher.pk %}">- {% trans "Remove voucher" %}+ <a class="modal-trigger-custom" href="#base-modal" data-href="{% url 'dashboard:voucher-delete' pk=voucher.pk %}">+ {% trans "Remove voucher" %}</a></li></ul>diff --git a/saleor/dashboard/templates/dashboard/discount/voucher_list.html b/saleor/dashboard/templates/dashboard/discount/voucher_list.htmlindex 93b4e549fc..f22117b903 100644--- a/saleor/dashboard/templates/dashboard/discount/voucher_list.html+++ b/saleor/dashboard/templates/dashboard/discount/voucher_list.html@@ -35,8 +35,9 @@<th>{% trans "Discount" %}</th><th>{% trans "Start date" %}</th><th>{% trans "End date" %}</th>- <th>{% trans "Usage limit" %}</th><th>{% trans "Used" %}</th>+ <th>{% trans "Apply to" %}</th>+ <th>{% trans "Limit" %}</th></tr></thead><tbody>@@ -45,11 +46,25 @@<td><a href="{% url 'dashboard:voucher-update' pk=voucher.pk %}">{{ voucher }}</a></td>- <td>{{ voucher.discount_value }}{{ voucher.get_discount_value_type_display }}</td>+ <td>+ {{ voucher.discount_value }}{{ voucher.get_discount_value_type_display }}</td><td>{{ voucher.start_date }}</td><td>{{ voucher.end_date|default:"-" }}</td>- <td>{{ voucher.usage_limit|default:_("unlimited") }}</td>- <td>{{ voucher.used }}</td>+ <td>+ {% if voucher.usage_limit %}+ {{ voucher.used }}/{{ voucher.usage_limit }}+ {% else %}+ {{ voucher.used }}+ {% endif %}+ </td>+ <td>{{ voucher.get_apply_to_display|default:"-" }}</td>+ <td>+ {% if voucher.limit %}+ {% gross voucher.limit %}+ {% else %}+ -+ {% endif %}+ </td></tr>{% endfor %}</tbody>diff --git a/saleor/discount/models.py b/saleor/discount/models.pyindex 3dca69802e..cbed7eb764 100644--- a/saleor/discount/models.py+++ b/saleor/discount/models.py@@ -1,11 +1,15 @@from __future__ import unicode_literalsfrom datetime import date+from decimal import Decimalfrom django.conf import settingsfrom django.db import models-from django.utils.translation import pgettext_lazy+from django.utils.safestring import mark_safe+from django.utils.translation import pgettext_lazy, gettext as _from django.utils.encoding import python_2_unicode_compatible+from django_countries import countriesfrom django_prices.models import PriceField+from django_prices.templatetags.prices import grossfrom prices import FixedDiscount, percentage_discount, Price@@ -19,6 +23,12 @@ class Voucher(models.Model):APPLY_TO_ONE_PRODUCT = 'one'APPLY_TO_ALL_PRODUCTS = 'all'+ APPLY_TO_PRODUCT_CHOICES = (+ (APPLY_TO_ONE_PRODUCT,+ pgettext_lazy('voucher_form', 'Apply only once')),+ (APPLY_TO_ALL_PRODUCTS,+ pgettext_lazy('voucher_form', 'Apply to all matching products')))+DISCOUNT_VALUE_FIXED = 'fixed'DISCOUNT_VALUE_PERCENTAGE = 'percentage'@@ -38,8 +48,6 @@ class Voucher(models.Model):(BASKET_TYPE, pgettext_lazy('voucher_model', 'Baskets over')))- FIELDS_DEPENDED_ON_TYPE = ('product', 'category', 'apply_to', 'limit')-type = models.CharField(max_length=20, choices=TYPE_CHOICES)name = models.CharField(max_length=255, null=True, blank=True)code = models.CharField(max_length=12, unique=True, db_index=True)@@ -62,11 +70,38 @@ class Voucher(models.Model):limit = PriceField(max_digits=12, decimal_places=2, null=True,blank=True, currency=settings.DEFAULT_CURRENCY)+ @property+ def is_free(self):+ return (self.discount_value == Decimal(100)+ and self.discount_value_type == Voucher.DISCOUNT_VALUE_PERCENTAGE)def __str__(self):if self.name:return self.name- return self.type+ discount = '%s%s' % (+ self.discount_value, self.get_discount_value_type_display())+ if self.type == Voucher.SHIPPING_TYPE:+ if self.is_free:+ return _('Free shipping')+ else:+ return _('%(discount)s off shipping') % {'discount': discount}+ if self.type == Voucher.PRODUCT_TYPE:+ return _('%(discount)s off %(product)s') % {+ 'discount': discount, 'product': self.product}+ if self.type == Voucher.CATEGORY_TYPE:+ return _('%(discount)s off %(category)s') % {+ 'discount': discount, 'category': self.category}+ return _('%(discount)s off') % {'discount': discount}+++ def get_apply_to_display(self):+ if self.type == Voucher.SHIPPING_TYPE and self.apply_to:+ return countries.name(self.apply_to)+ if self.type == Voucher.SHIPPING_TYPE:+ return _('Any country')+ if self.apply_to and self.type in (Voucher.PRODUCT_TYPE, Voucher.CATEGORY_TYPE):+ choices = dict(self.APPLY_TO_PRODUCT_CHOICES)+ return choices[self.apply_to]@python_2_unicode_compatible