Skip to content

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

Applied changes from review

Mikail KOCAK7ce128ecf5b8
diff --git a/saleor/dashboard/product/forms.py b/saleor/dashboard/product/forms.py
index d3cf20891f..57bc62389f 100644
--- a/saleor/dashboard/product/forms.py
+++ b/saleor/dashboard/product/forms.py
@@ -253,7 +253,15 @@ class AttributesMixin:
class ProductForm(MoneyModelForm, AttributesMixin):
tax_rate = forms.ChoiceField(
- required=False, label=pgettext_lazy("Product tax rate type", "Tax rate")
+ required=False,
+ label=pgettext_lazy("Product tax rate type", "Tax rate"),
+ help_text=pgettext_lazy(
+ "Help text for the tax rate field over the product update/create form",
+ (
+ "Make sure you have enabled a VAT provider and fetched the rates if "
+ "needed by the plugin."
+ ),
+ ),
)
category = TreeNodeChoiceField(
queryset=Category.objects.all(), label=pgettext_lazy("Category", "Category")
@@ -301,9 +309,9 @@ class ProductForm(MoneyModelForm, AttributesMixin):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
- self.manager = manager = get_extensions_manager()
+ self.manager = get_extensions_manager()
product_type = self.instance.product_type
- self.initial["tax_rate"] = get_product_tax_rate(self.instance, manager)
+ self.initial["tax_rate"] = get_product_tax_rate(self.instance, self.manager)
self.available_attributes = product_type.product_attributes.prefetch_related(
"values"
).product_attributes_sorted()
@@ -320,9 +328,15 @@ class ProductForm(MoneyModelForm, AttributesMixin):
self.fields["seo_title"] = SeoTitleField(
extra_attrs={"data-bind": self["name"].auto_id}
)
- self.fields["tax_rate"].choices = [
- (tax.code, tax.description) for tax in manager.get_tax_rate_type_choices()
+ tax_rate_field = self.fields["tax_rate"]
+ tax_rate_field.choices = [
+ (tax.code, tax.description)
+ for tax in self.manager.get_tax_rate_type_choices()
]
+
+ if not tax_rate_field.choices:
+ tax_rate_field.disabled = True
+
if include_taxes_in_prices():
self.fields["price"].label = pgettext_lazy(
"Currency gross amount", "Gross price"
diff --git a/templates/dashboard/product/detail.html b/templates/dashboard/product/detail.html
index b907570fcf..87a5147c33 100644
--- a/templates/dashboard/product/detail.html
+++ b/templates/dashboard/product/detail.html
@@ -152,7 +152,7 @@
{% trans "Pricing" context "Product pricing card header" %}
</h5>
<h6>
- {% if product.charge_taxes %}
+ {% if product.charge_taxes and tax_rate_code %}
{% blocktrans trimmed with tax_rate=tax_rate_code context "Product pricing card header subtitle" %}
Taxes are charged ({{ tax_rate }} tax rate)
{% endblocktrans %}
diff --git a/templates/dashboard/product/form.html b/templates/dashboard/product/form.html
index 45c499ed68..e1c326dd3c 100644
--- a/templates/dashboard/product/form.html
+++ b/templates/dashboard/product/form.html
@@ -87,9 +87,7 @@
{{ product_form.price|materializecss }}
</div>
<div class="row">
- {% if product_form.tax_rate.field.choices %}
- {{ product_form.tax_rate|materializecss }}
- {% endif %}
+ {{ product_form.tax_rate|materializecss }}
{{ 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.html
index 637fb89f90..7a978db10d 100644
--- a/templates/dashboard/product/product_type/form.html
+++ b/templates/dashboard/product/product_type/form.html
@@ -90,16 +90,18 @@
</div>
</div>
</div>
- {% 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 }}
+ {% if form.tax_rate.field.choices %}
+ {{ form.tax_rate|materializecss }}
+ {% else %}
+ {% trans "Tax settings are disabled. Make sure you have enabled a VAT provider and fetched the rates if required by the plugin." context "Note message in product type form when no rates were found" %}
+ {% endif %}
</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.py
index f508ae83d7..96a305c201 100644
--- a/tests/dashboard/test_product.py
+++ b/tests/dashboard/test_product.py
@@ -2,6 +2,7 @@ import json
from unittest import mock
from unittest.mock import MagicMock, Mock
+import pytest
from django.forms import HiddenInput
from django.forms.models import model_to_dict
from django.urls import reverse
@@ -266,11 +267,28 @@ def test_view_product_edit(mocked_set_tax_rate_code, admin_client, product):
), "The tax code shouldn't have been changed"
-def test_update_product_tax_rate(admin_client, product):
+def mocked_tax_types():
+ tax_rate_code = "dummy"
+ manager = get_extensions_manager()
+
+ types = [TaxType(code=tax_rate_code, description="")]
+
+ 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 = types
+ yield mocked_manager, types
+
+
+def test_update_product_tax_rate(admin_client, product, mocked_tax_types):
"""Ensure tax rates are correctly listed in the form and the form is properly
updating the tax rate.
"""
- tax_rate_code = "dummy"
+ mocked_manager, tax_types = mocked_tax_types
+ tax_rate_code = tax_types[0].code
url = reverse("dashboard:product-update", kwargs={"pk": product.pk})
data = {
@@ -284,33 +302,30 @@ def test_update_product_tax_rate(admin_client, product):
"tax_rate": tax_rate_code,
}
- manager = get_extensions_manager()
+ # 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, tax_rate_code
+ )
- 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_product_update_resolves_tax_types(admin_client, product, mocked_tax_types):
+ """Ensure tax rates are correctly resolved in the product form."""
+ mocked_manager, tax_types = mocked_tax_types
+ tax_rate_code = tax_types[0].code
+
+ url = reverse("dashboard:product-update", kwargs={"pk": product.pk})
+
+ # 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 == [(tax_rate_code, "")]
def test_view_product_delete(db, admin_client, product):