Skip to content

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

Extract voucher usage increase/decrease methods to Voucher manager

Artur Smęt6d3182a7d24c
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.py
index 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 order
diff --git a/saleor/dashboard/order/forms.py b/saleor/dashboard/order/forms.py
index 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_CHOICES
from satchless.item import InsufficientStock
from ...cart.forms import QuantityField
+from ...discount.models import Voucher
from ...order import Status
from ...order.models import DeliveryGroup, Order, OrderedItem, OrderNote
from ...product.models import ProductVariant, Stock
@@ -265,8 +266,7 @@ class RemoveVoucherForm(forms.Form):
self.order.discount_amount = 0
self.order.discount_name = ''
voucher = self.order.voucher
- voucher.used -= 1
- voucher.save(update_fields=['used'])
+ Voucher.objects.decrease_usage(voucher)
self.order.voucher = None
Order.objects.recalculate_order(self.order)
diff --git a/saleor/discount/models.py b/saleor/discount/models.py
index 3b7ee9ae88..7bf7ec29d8 100644
--- a/saleor/discount/models.py
+++ b/saleor/discount/models.py
@@ -4,6 +4,7 @@ from decimal import Decimal
from django.conf import settings
from django.db import models
+from django.db.models import F
from django.utils.translation import pgettext, pgettext_lazy
from django.utils.encoding import python_2_unicode_compatible, smart_text
from 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_compatible
class Voucher(models.Model):