Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Extract voucher usage increase/decrease methods to Voucher manager
Artur Smęt6d3182a7d24c
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.pyindex 8ac02a61e5..f8c3dd8b90 100644--- a/saleor/checkout/core.py+++ b/saleor/checkout/core.py@@ -237,8 +237,7 @@ class Checkout(object):group.add_items_from_partition(partition)if voucher is not None:- voucher.used += 1- voucher.save(update_fields=['used'])+ Voucher.objects.increase_usage(voucher)return orderdiff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.pyindex 70b399b99f..7d00f50c6b 100644--- a/saleor/dashboard/order/forms.py+++ b/saleor/dashboard/order/forms.py@@ -11,6 +11,7 @@ from payments.models import PAYMENT_STATUS_CHOICESfrom satchless.item import InsufficientStockfrom ...cart.forms import QuantityField+from ...discount.models import Voucherfrom ...order import Statusfrom ...order.models import DeliveryGroup, Order, OrderedItem, OrderNotefrom ...product.models import ProductVariant, Stock@@ -265,8 +266,7 @@ class RemoveVoucherForm(forms.Form):self.order.discount_amount = 0self.order.discount_name = ''voucher = self.order.voucher- voucher.used -= 1- voucher.save(update_fields=['used'])+ Voucher.objects.decrease_usage(voucher)self.order.voucher = NoneOrder.objects.recalculate_order(self.order)diff --git a/saleor/discount/models.py b/saleor/discount/models.pyindex 3b7ee9ae88..7bf7ec29d8 100644--- a/saleor/discount/models.py+++ b/saleor/discount/models.py@@ -4,6 +4,7 @@ from decimal import Decimalfrom django.conf import settingsfrom django.db import models+from django.db.models import Ffrom django.utils.translation import pgettext, pgettext_lazyfrom django.utils.encoding import python_2_unicode_compatible, smart_textfrom django_countries import countries@@ -32,6 +33,14 @@ class VoucherQueryset(models.QuerySet):queryset = queryset.filter(start_date__lte=today)return queryset+ def increase_usage(self, voucher):+ voucher.used = F('used') + 1+ voucher.save(update_fields=['used'])++ def decrease_usage(self, voucher):+ voucher.used = F('used') - 1+ voucher.save(update_fields=['used'])+@python_2_unicode_compatibleclass Voucher(models.Model):