Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add caching on the plugin level for Vatlayer
Maciej Korycinskiff01b40105d8
diff --git a/saleor/core/extensions/plugins/vatlayer/__init__.py b/saleor/core/extensions/plugins/vatlayer/__init__.pyindex 03b6ac994e..985709bbcf 100644--- a/saleor/core/extensions/plugins/vatlayer/__init__.py+++ b/saleor/core/extensions/plugins/vatlayer/__init__.py@@ -1,6 +1,4 @@-from django.conf import settingsfrom django.utils.translation import pgettext_lazy-from django_countries.fields import Countryfrom django_prices_vatlayer.utils import get_tax_for_rate, get_tax_rates_for_countryfrom prices import Money, MoneyRange, TaxedMoney, TaxedMoneyRange@@ -126,16 +124,6 @@ def get_taxes_for_country(country):return taxes-def get_taxes_for_address(address):- """Return proper taxes for address or default country."""- if address is not None:- country = address.country- else:- country = Country(settings.DEFAULT_COUNTRY)-- return get_taxes_for_country(country)--def get_tax_rate_by_name(rate_name, taxes=None):"""Return value of tax rate for current taxes."""if not taxes or not rate_name:diff --git a/saleor/core/extensions/plugins/vatlayer/plugin.py b/saleor/core/extensions/plugins/vatlayer/plugin.pyindex 4eda7261aa..821143a8e1 100644--- a/saleor/core/extensions/plugins/vatlayer/plugin.py+++ b/saleor/core/extensions/plugins/vatlayer/plugin.py@@ -12,7 +12,6 @@ from . import (TaxRateType,apply_tax_to_price,get_taxed_shipping_price,- get_taxes_for_address,get_taxes_for_country,)@@ -30,6 +29,7 @@ class VatlayerPlugin(BasePlugin):def __init__(self, *args, **kwargs):super().__init__(*args, **kwargs)self._enabled = bool(settings.VATLAYER_ACCESS_KEY)+ self._cached_taxes = {}def _skip_plugin(self, previous_value: Union[TaxedMoney, TaxedMoneyRange]) -> bool:if not self._enabled:@@ -87,6 +87,18 @@ class VatlayerPlugin(BasePlugin):)return lines_total+ def _get_taxes_for_country(self, country: Country):+ """Tries to fetch cached taxes on the plugin level. If the plugin doesn't have+ cached taxes for a given country it will fetch it from cache or db"""+ if not country:+ country = Country(settings.DEFAULT_COUNTRY)+ country_code = country.code+ if country_code in self._cached_taxes:+ return self._cached_taxes[country_code]+ taxes = get_taxes_for_country(country)+ self._cached_taxes[country_code] = taxes+ return taxes+def calculate_checkout_shipping(self,checkout: "Checkout",@@ -98,7 +110,9 @@ class VatlayerPlugin(BasePlugin):return previous_valueaddress = checkout.shipping_address or checkout.billing_address- taxes = get_taxes_for_address(address)+ taxes = None+ if address:+ taxes = self._get_taxes_for_country(address.country)if not checkout.shipping_method:return previous_value@@ -111,7 +125,9 @@ class VatlayerPlugin(BasePlugin):return previous_valueaddress = order.shipping_address or order.billing_address- taxes = get_taxes_for_address(address)+ taxes = None+ if address:+ taxes = self._get_taxes_for_country(address.country)if not order.shipping_method:return previous_valuereturn get_taxed_shipping_price(order.shipping_method.price, taxes)@@ -177,9 +193,7 @@ class VatlayerPlugin(BasePlugin):if self._skip_plugin(previous_value):return previous_value- taxes = None- if country:- taxes = get_taxes_for_country(country)+ taxes = self._get_taxes_for_country(country)return get_taxed_shipping_price(prices, taxes)def apply_taxes_to_shipping(@@ -188,7 +202,7 @@ class VatlayerPlugin(BasePlugin):if self._skip_plugin(previous_value):return previous_value- taxes = get_taxes_for_country(shipping_address.country)+ taxes = self._get_taxes_for_country(shipping_address.country)return get_taxed_shipping_price(price, taxes)def apply_taxes_to_product(@@ -202,14 +216,10 @@ class VatlayerPlugin(BasePlugin):if self._skip_plugin(previous_value):return previous_value- taxes = kwargs.get("taxes")- if country and not taxes:- # FIXME After we introduce plugin architecture, taxes will be cached on the- # plugin level and there will be no need to pass it from view functions.- # This is only temporary approach to limit redis and db hits- taxes = get_taxes_for_country(country)- if not product.charge_taxes:- taxes = None+ taxes = None+ if country and product.charge_taxes:+ taxes = self._get_taxes_for_country(country)+product_tax_rate = self.__get_tax_code_from_object_meta(product).codetax_rate = (product_tax_ratediff --git a/tests/extensions/plugins/test_vatlayer.py b/tests/extensions/plugins/test_vatlayer.pyindex cdfab75296..b06faf9fa5 100644--- a/tests/extensions/plugins/test_vatlayer.py+++ b/tests/extensions/plugins/test_vatlayer.py@@ -1,4 +1,5 @@from unittest import mock+from unittest.mock import Mockfrom urllib.parse import urlparseimport pytest@@ -13,9 +14,9 @@ from saleor.core.extensions.plugins.vatlayer import (apply_tax_to_price,get_tax_rate_by_name,get_taxed_shipping_price,- get_taxes_for_address,get_taxes_for_country,)+from saleor.core.extensions.plugins.vatlayer.plugin import VatlayerPluginfrom saleor.dashboard.taxes.filters import get_country_choices_for_vat@@ -256,26 +257,6 @@ def test_get_country_choices_for_vat(vatlayer):assert choices == expected_choices-def test_get_taxes_for_address(address, vatlayer, compare_taxes):- taxes = get_taxes_for_address(address)- compare_taxes(taxes, vatlayer)---def test_get_taxes_for_address_fallback_default(settings, vatlayer, compare_taxes):- settings.DEFAULT_COUNTRY = "PL"- taxes = get_taxes_for_address(None)- compare_taxes(taxes, vatlayer)---def test_get_taxes_for_address_other_country(address, vatlayer, compare_taxes):- address.country = "DE"- address.save()- tax_rates = get_taxes_for_country(Country("DE"))-- taxes = get_taxes_for_address(address)- compare_taxes(taxes, tax_rates)--def test_get_taxes_for_country(vatlayer, compare_taxes):taxes = get_taxes_for_country(Country("PL"))compare_taxes(taxes, vatlayer)@@ -362,3 +343,19 @@ def test_apply_tax_to_price_no_taxes_return_taxed_money_range():def test_apply_tax_to_price_no_taxes_raise_typeerror_for_invalid_type():with pytest.raises(TypeError):assert apply_tax_to_price(None, "standard", 100)+++def test_vatlayer_plugin_caches_taxes(vatlayer, monkeypatch, product, address):++ mocked_taxes = Mock(wraps=get_taxes_for_country)+ monkeypatch.setattr(+ "saleor.core.extensions.plugins.vatlayer.plugin.get_taxes_for_country",+ mocked_taxes,+ )+ plugin = VatlayerPlugin()+ price = product.variants.first().get_price()+ price = TaxedMoney(price, price)+ address.country = Country("de")+ plugin.apply_taxes_to_product(product, price, address.country, price)+ plugin.apply_taxes_to_shipping(price, address, price)+ assert mocked_taxes.call_count == 1