Skip to content

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

Move vatlayer plugin to dynamic config

Maciej Korycinskib26f9dd0bec6
diff --git a/saleor/extensions/plugins/vatlayer/plugin.py b/saleor/extensions/plugins/vatlayer/plugin.py
index 9df4964bd2..543acb5601 100644
--- a/saleor/extensions/plugins/vatlayer/plugin.py
+++ b/saleor/extensions/plugins/vatlayer/plugin.py
@@ -34,8 +34,19 @@ class VatlayerPlugin(BasePlugin):
self._enabled = bool(settings.VATLAYER_ACCESS_KEY)
self._cached_taxes = {}
+ def _initialize_plugin_configuration(self):
+ super()._initialize_plugin_configuration()
+
+ if not self._cached_config_from_db:
+ # This should be removed after we drop an Vatl's settings from Django
+ # settings file
+ self.active = bool(settings.VATLAYER_ACCESS_KEY)
+
def _skip_plugin(self, previous_value: Union[TaxedMoney, TaxedMoneyRange]) -> bool:
- if not self._enabled:
+ if not self.active:
+ return True
+
+ if not settings.VATLAYER_ACCESS_KEY:
return True
# The previous plugin already calculated taxes so we can skip our logic
@@ -56,6 +67,8 @@ class VatlayerPlugin(BasePlugin):
previous_value: TaxedMoney,
) -> TaxedMoney:
+ self._initialize_plugin_configuration()
+
if self._skip_plugin(previous_value):
return previous_value
@@ -75,6 +88,8 @@ class VatlayerPlugin(BasePlugin):
previous_value: TaxedMoney,
) -> TaxedMoney:
"""Calculate subtotal gross for checkout."""
+ self._initialize_plugin_configuration()
+
if self._skip_plugin(previous_value):
return previous_value
@@ -112,6 +127,8 @@ class VatlayerPlugin(BasePlugin):
previous_value: TaxedMoney,
) -> TaxedMoney:
"""Calculate shipping gross for checkout."""
+ self._initialize_plugin_configuration()
+
if self._skip_plugin(previous_value):
return previous_value
@@ -127,6 +144,8 @@ class VatlayerPlugin(BasePlugin):
def calculate_order_shipping(
self, order: "Order", previous_value: TaxedMoney
) -> TaxedMoney:
+ self._initialize_plugin_configuration()
+
if self._skip_plugin(previous_value):
return previous_value
@@ -144,6 +163,8 @@ class VatlayerPlugin(BasePlugin):
discounts: List["DiscountInfo"],
previous_value: TaxedMoney,
) -> TaxedMoney:
+ self._initialize_plugin_configuration()
+
if self._skip_plugin(previous_value):
return previous_value
@@ -160,6 +181,8 @@ class VatlayerPlugin(BasePlugin):
def calculate_order_line_unit(
self, order_line: "OrderLine", previous_value: TaxedMoney
) -> TaxedMoney:
+ self._initialize_plugin_configuration()
+
if self._skip_plugin(previous_value):
return previous_value
@@ -173,7 +196,9 @@ class VatlayerPlugin(BasePlugin):
def get_tax_rate_type_choices(
self, previous_value: List["TaxType"]
) -> List["TaxType"]:
- if not self._enabled:
+ self._initialize_plugin_configuration()
+
+ if not self.active:
return previous_value
rate_types = get_tax_rate_types() + [DEFAULT_TAX_RATE_NAME]
@@ -184,18 +209,24 @@ class VatlayerPlugin(BasePlugin):
return sorted(choices, key=lambda x: x.code)
def show_taxes_on_storefront(self, previous_value: bool) -> bool:
- if not self._enabled:
+ self._initialize_plugin_configuration()
+
+ if not self.active:
return previous_value
return True
def taxes_are_enabled(self, previous_value: bool) -> bool:
- if not self._enabled:
+ self._initialize_plugin_configuration()
+
+ if not self.active:
return previous_value
return True
def apply_taxes_to_shipping_price_range(
self, prices: MoneyRange, country: Country, previous_value: TaxedMoneyRange
) -> TaxedMoneyRange:
+ self._initialize_plugin_configuration()
+
if self._skip_plugin(previous_value):
return previous_value
@@ -205,6 +236,8 @@ class VatlayerPlugin(BasePlugin):
def apply_taxes_to_shipping(
self, price: Money, shipping_address: "Address", previous_value: TaxedMoney
) -> TaxedMoney:
+ self._initialize_plugin_configuration()
+
if self._skip_plugin(previous_value):
return previous_value
@@ -218,6 +251,8 @@ class VatlayerPlugin(BasePlugin):
country: Country,
previous_value: TaxedMoney,
) -> TaxedMoney:
+ self._initialize_plugin_configuration()
+
if self._skip_plugin(previous_value):
return previous_value
return self.__apply_taxes_to_product(product, price, country)
@@ -239,7 +274,9 @@ class VatlayerPlugin(BasePlugin):
def assign_tax_code_to_object_meta(
self, obj: Union["Product", "ProductType"], tax_code: str, previous_value: Any
):
- if not self._enabled:
+ self._initialize_plugin_configuration()
+
+ if not self.active:
return previous_value
if tax_code not in dict(TaxRateType.CHOICES):
@@ -259,7 +296,9 @@ class VatlayerPlugin(BasePlugin):
def get_tax_code_from_object_meta(
self, obj: Union["Product", "ProductType"], previous_value: "TaxType"
) -> "TaxType":
- if not self._enabled:
+ self._initialize_plugin_configuration()
+
+ if not self.active:
return previous_value
return self.__get_tax_code_from_object_meta(obj)
@@ -273,7 +312,9 @@ class VatlayerPlugin(BasePlugin):
self, obj: Union["Product", "ProductType"], country: Country, previous_value
) -> Decimal:
"""Return tax rate percentage value for given tax rate type in the country."""
- if not self._enabled:
+ self._initialize_plugin_configuration()
+
+ if not self.active:
return previous_value
taxes = self._get_taxes_for_country(country)
if not taxes: