Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fixed display of tax rate (forms & details) + update of product tax rate
NyanKiyoshi115d67ac2748
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex f4b13cd69b..6d9322bc84 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -85,6 +85,7 @@ All notable, unreleased changes to this project will be documented in this file.- Added product variant bulk create mutation - #4749 by @fowczarek- availablePaymentGateways extended with configuration data in GraphQL schema - #4774 by @salwator- Add metadata to Order model - #4513 by @szewczykmira+- Fixed display of the products tax rate in the details page of dashboard 1.0, users can now update the tax rate of products in dashboard 1.0. The tax fields will no longer be shown if no tax support is enabled. - #4780 by @NyanKiyoshi## 2.8.0diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.pyindex 3826fa3057..d3cf20891f 100644--- a/saleor/dashboard/product/forms.py+++ b/saleor/dashboard/product/forms.py@@ -42,6 +42,7 @@ from ..seo.fields import SeoDescriptionField, SeoTitleFieldfrom ..seo.utils import prepare_seo_descriptionfrom ..widgets import RichTextEditorWidgetfrom . import ProductBulkAction+from .utils import get_product_tax_ratefrom .widgets import ImagePreviewWidget@@ -300,12 +301,9 @@ class ProductForm(MoneyModelForm, AttributesMixin):def __init__(self, *args, **kwargs):super().__init__(*args, **kwargs)- manager = get_extensions_manager()+ self.manager = manager = get_extensions_manager()product_type = self.instance.product_type- product_tax_rate = manager.get_tax_code_from_object_meta(self.instance).code- self.initial["tax_rate"] = (- product_tax_rate or manager.get_tax_code_from_object_meta(product_type).code- )+ self.initial["tax_rate"] = get_product_tax_rate(self.instance, manager)self.available_attributes = product_type.product_attributes.prefetch_related("values").product_attributes_sorted()@@ -351,17 +349,21 @@ class ProductForm(MoneyModelForm, AttributesMixin):)return seo_description- @transaction.atomicdef save(self, commit=True):assert commit is True, "Commit is required to build the M2M structure"- super().save()+ with transaction.atomic():+ super().save()- self.save_attributes()- self.instance.collections.clear()+ self.save_attributes()+ self.instance.collections.clear()++ for collection in self.cleaned_data["collections"]:+ self.instance.collections.add(collection)- for collection in self.cleaned_data["collections"]:- self.instance.collections.add(collection)+ tax_rate = self.cleaned_data.get("tax_rate")+ if tax_rate:+ self.manager.assign_tax_code_to_object_meta(self.instance, tax_rate)update_product_minimal_variant_price_task.delay(self.instance.pk)return self.instancediff --git a/saleor/dashboard/product/utils.py b/saleor/dashboard/product/utils.pynew file mode 100644index 0000000000..ec94118c1d--- /dev/null+++ b/saleor/dashboard/product/utils.py@@ -0,0 +1,11 @@+from ...extensions.manager import ExtensionsManager, get_extensions_manager+from ...product.models import Product+++def get_product_tax_rate(product: Product, manager: ExtensionsManager = None) -> str:+ manager = manager or get_extensions_manager()+ tax_rate = manager.get_tax_code_from_object_meta(product).code+ tax_rate = (+ tax_rate or manager.get_tax_code_from_object_meta(product.product_type).code+ )+ return tax_ratediff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.pyindex 536951f089..d41055bbf5 100644--- a/saleor/dashboard/product/views.py+++ b/saleor/dashboard/product/views.py@@ -23,6 +23,7 @@ from ...product.utils.costs import get_margin_for_variant, get_product_costs_datfrom ..views import staff_member_requiredfrom . import formsfrom .filters import AttributeFilter, ProductFilter, ProductTypeFilter+from .utils import get_product_tax_rate@staff_member_required@@ -69,6 +70,7 @@ def product_details(request, pk):only_variant = variants.first() if no_variants else Nonectx = {"product": product,+ "tax_rate_code": get_product_tax_rate(product),"sale_price": sale_price,"discounted_price": discounted_price,"variants": variants,diff --git a/templates/dashboard/product/detail.html b/templates/dashboard/product/detail.htmlindex 24546ac79b..b907570fcf 100644--- a/templates/dashboard/product/detail.html+++ b/templates/dashboard/product/detail.html@@ -153,7 +153,7 @@</h5><h6>{% if product.charge_taxes %}- {% blocktrans trimmed with tax_rate=product.tax_rate context "Product pricing card header subtitle" %}+ {% blocktrans trimmed with tax_rate=tax_rate_code context "Product pricing card header subtitle" %}Taxes are charged ({{ tax_rate }} tax rate){% endblocktrans %}{% else %}diff --git a/templates/dashboard/product/form.html b/templates/dashboard/product/form.htmlindex e1c326dd3c..45c499ed68 100644--- a/templates/dashboard/product/form.html+++ b/templates/dashboard/product/form.html@@ -87,7 +87,9 @@{{ product_form.price|materializecss }}</div><div class="row">- {{ product_form.tax_rate|materializecss }}+ {% if product_form.tax_rate.field.choices %}+ {{ product_form.tax_rate|materializecss }}+ {% endif %}{{ product_form.charge_taxes|materializecss }}</div>{% if not product.product_type.has_variants %}diff --git a/templates/dashboard/product/product_type/form.html b/templates/dashboard/product/product_type/form.htmlindex 3d361f50fa..637fb89f90 100644--- a/templates/dashboard/product/product_type/form.html+++ b/templates/dashboard/product/product_type/form.html@@ -90,14 +90,16 @@</div></div></div>- <div class="row">- <div class="col s12">- <span class="card-title">{% trans "Taxes" %}</span>- </div>- <div class="col s12 m8">- {{ form.tax_rate|materializecss }}+ {% if form.tax_rate.field.choices %}+ <div class="row">+ <div class="col s12">+ <span class="card-title">{% trans "Taxes" %}</span>+ </div>+ <div class="col s12 m8">+ {{ form.tax_rate|materializecss }}+ </div></div>- </div>+ {% endif %}<div class="row"><div class="col s12"><span class="card-title">{% trans "Shipping" %}</span>diff --git a/tests/dashboard/test_product.py b/tests/dashboard/test_product.pyindex 6e0e27f5de..f508ae83d7 100644--- a/tests/dashboard/test_product.py+++ b/tests/dashboard/test_product.py@@ -1,4 +1,5 @@import json+from unittest import mockfrom unittest.mock import MagicMock, Mockfrom django.forms import HiddenInput@@ -6,8 +7,10 @@ from django.forms.models import model_to_dictfrom django.urls import reversefrom prices import Money, MoneyRange, TaxedMoney, TaxedMoneyRange+from saleor.core.taxes import TaxTypefrom saleor.dashboard.product import ProductBulkActionfrom saleor.dashboard.product.forms import ProductForm, ProductVariantForm+from saleor.extensions.manager import get_extensions_managerfrom saleor.product.forms import VariantChoiceFieldfrom saleor.product.models import (Attribute,@@ -235,7 +238,10 @@ def test_view_product_create(admin_client, product_type, category):assert Product.objects.count() == 1-def test_view_product_edit(admin_client, product):+ "saleor.extensions.manager.ExtensionsManager.assign_tax_code_to_object_meta"+)+def test_view_product_edit(mocked_set_tax_rate_code, admin_client, product):url = reverse("dashboard:product-update", kwargs={"pk": product.pk})data = {"name": "Product second name",@@ -255,6 +261,56 @@ def test_view_product_edit(admin_client, product):"dashboard:product-details", kwargs={"pk": product.pk})assert product.name == "Product second name"+ assert (+ mocked_set_tax_rate_code.call_count == 0+ ), "The tax code shouldn't have been changed"+++def test_update_product_tax_rate(admin_client, product):+ """Ensure tax rates are correctly listed in the form and the form is properly+ updating the tax rate.+ """+ tax_rate_code = "dummy"++ url = reverse("dashboard:product-update", kwargs={"pk": product.pk})+ data = {+ "name": "Product second name",+ "description": "Product description.",+ "price_0": 10,+ "price_1": product.price.currency,+ "category": product.category.pk,+ "variant-sku": "123",+ "variant-quantity": 10,+ "tax_rate": tax_rate_code,+ }++ manager = get_extensions_manager()++ with mock.patch("saleor.extensions.manager.ExtensionsManager") as mocked_manager:+ mocked_manager = mocked_manager()+ mocked_manager.assign_tax_code_to_object_meta.side_effect = (+ manager.assign_tax_code_to_object_meta+ )+ mocked_manager.get_tax_rate_type_choices.return_value = [+ TaxType(code=tax_rate_code, description="")+ ]++ # Ensure the available tax rate codes are properly returned+ response = admin_client.get(url)+ assert response.status_code == 200+ form = response.context["form"]+ tax_rate_field = form.fields["tax_rate"]+ assert tax_rate_field.choices == [("dummy", "")]++ # Ensure we are able to set a given tax rate code to a product+ response = admin_client.post(url, data)+ assert response.status_code == 302+ assert get_redirect_location(response) == reverse(+ "dashboard:product-details", kwargs={"pk": product.pk}+ )+ mocked_manager.assign_tax_code_to_object_meta.assert_called_once_with(+ mock.ANY, "dummy"+ )def test_view_product_delete(db, admin_client, product):