Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add Collections to the Sale
Pacu27b591139524f
diff --git a/saleor/core/middleware.py b/saleor/core/middleware.pyindex 6257cefdc5..02d5c2d588 100644--- a/saleor/core/middleware.py+++ b/saleor/core/middleware.py@@ -33,7 +33,8 @@ def google_analytics(get_response):def discounts(get_response):"""Assign active discounts to `request.discounts`."""def middleware(request):- discounts = Sale.objects.prefetch_related('products', 'categories')+ discounts = Sale.objects.prefetch_related(+ 'products', 'categories', 'collections')request.discounts = discountsreturn get_response(request)diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.pyindex 7f38b78d1a..d23754125f 100644--- a/saleor/core/utils/random_data.py+++ b/saleor/core/utils/random_data.py@@ -492,7 +492,8 @@ def create_users(how_many=10):def create_orders(how_many=10):taxes = get_taxes_for_country(Country(settings.DEFAULT_COUNTRY))- discounts = Sale.objects.prefetch_related('products', 'categories')+ discounts = Sale.objects.prefetch_related(+ 'products', 'categories', 'collections')for dummy in range(how_many):order = create_fake_order(discounts, taxes)yield 'Order: %s' % (order,)diff --git a/saleor/dashboard/discount/forms.py b/saleor/dashboard/discount/forms.pyindex 349c73abef..f9021b368e 100644--- a/saleor/dashboard/discount/forms.py+++ b/saleor/dashboard/discount/forms.py@@ -38,7 +38,10 @@ class SaleForm(forms.ModelForm):'Discounted products'),'categories': pgettext_lazy('Discounted categories',- 'Discounted categories')}+ 'Discounted categories'),+ 'collections': pgettext_lazy(+ 'Discounted collections',+ 'Discounted collections')}def __init__(self, *args, **kwargs):super().__init__(*args, **kwargs)@@ -55,11 +58,12 @@ class SaleForm(forms.ModelForm):'Sale cannot exceed 100%'))products = cleaned_data.get('products')categories = cleaned_data.get('categories')- if not products and not categories:+ collections = cleaned_data.get('collections')+ if not any([products, categories, collections]):raise forms.ValidationError(pgettext_lazy('Sale (discount) error',- 'A single sale must point to at least one product and/or '- 'category.'))+ 'A single sale must point to at least one product, collection'+ 'and/or category.'))return cleaned_datadiff --git a/saleor/discount/models.py b/saleor/discount/models.pyindex 0ac06440fa..5d11507f6b 100644--- a/saleor/discount/models.py+++ b/saleor/discount/models.py@@ -146,6 +146,7 @@ class Sale(models.Model):default=0)products = models.ManyToManyField('product.Product', blank=True)categories = models.ManyToManyField('product.Category', blank=True)+ collections = models.ManyToManyField('product.Collection', blank=True)class Meta:app_label = 'discount'diff --git a/saleor/graphql/discount/mutations.py b/saleor/graphql/discount/mutations.pyindex 48e40ec100..cafa0599ba 100644--- a/saleor/graphql/discount/mutations.py+++ b/saleor/graphql/discount/mutations.py@@ -94,6 +94,8 @@ class SaleInput(graphene.InputObjectType):graphene.ID, description='Products related to the discount.')categories = graphene.List(graphene.ID, description='Categories related to the discount.')+ collections = graphene.List(+ graphene.ID, description='Collections related to the discount.')class SaleCreate(ModelMutation):diff --git a/templates/dashboard/discount/sale/form.html b/templates/dashboard/discount/sale/form.htmlindex 8451758270..c936f18d2c 100644--- a/templates/dashboard/discount/sale/form.html+++ b/templates/dashboard/discount/sale/form.html@@ -95,6 +95,9 @@<div class="row">{{ form.categories|materializecss }}</div>+ <div class="row">+ {{ form.collections|materializecss }}+ </div></div><div class="card-action right-align">diff --git a/tests/conftest.py b/tests/conftest.pyindex d7629debbc..ef8503481f 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -577,9 +577,10 @@ def payment_input(order_with_lines):@pytest.fixture()-def sale(db, default_category):+def sale(db, default_category, collection):sale = Sale.objects.create(name="Sale", value=5)sale.categories.add(default_category)+ sale.collections.add(collection)return sale