Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add filters for inactive giftcards
fowczarek8524d67e425e
diff --git a/saleor/giftcard/models.py b/saleor/giftcard/models.pyindex 9486649e91..1a373dd9f5 100644--- a/saleor/giftcard/models.py+++ b/saleor/giftcard/models.py@@ -2,10 +2,20 @@ from datetime import datefrom django.conf import settingsfrom django.db import models+from django.db.models import Qfrom django.utils.translation import pgettext_lazyfrom django_prices.models import MoneyField+class GiftCardQueryset(models.QuerySet):+ def active(self, date):+ return self.filter(+ Q(expiration_date__isnull=True) | Q(expiration_date__gte=date),+ start_date__lte=date,+ is_active=True,+ )++class GiftCard(models.Model):code = models.CharField(max_length=16, unique=True, db_index=True)buyer = models.ForeignKey(@@ -34,6 +44,8 @@ class GiftCard(models.Model):decimal_places=settings.DEFAULT_DECIMAL_PLACES,)+ objects = GiftCardQueryset.as_manager()+class Meta:permissions = ((diff --git a/saleor/giftcard/utils.py b/saleor/giftcard/utils.pyindex be9aebe478..ebf53b3dbe 100644--- a/saleor/giftcard/utils.py+++ b/saleor/giftcard/utils.py@@ -1,3 +1,5 @@+from datetime import date+from django.core.exceptions import ValidationErrorfrom .models import GiftCard@@ -5,7 +7,7 @@ from .models import GiftCarddef add_gift_card_code_to_checkout(checkout, promo_code):try:- gift_card = GiftCard.objects.get(code=promo_code)+ gift_card = GiftCard.objects.active(date=date.today()).get(code=promo_code)except GiftCard.DoesNotExist:raise ValidationError({"promo_code": "Gift card with given code is invalid."})add_gift_card_to_checkout(checkout, gift_card)