Skip to content

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

Update plugin config when new config item added

Jakub Draganek1c71f33eba15
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3539d8519a..fe6896111f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -106,6 +106,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Refactored the backend side of `checkoutCreate` to improve performances and prevent side effects over the user's checkout if the checkout creation was to fail. - #4367 by @NyanKiyoshi
- Refactored the logic of cleaning the checkout shipping method over the API, so users do not lose the shipping method when updating their checkout. If the shipping method becomes invalid, it will be replaced by the cheapest available. - #4367 by @NyanKiyoshi & @szewczykmira
- Refactored process of getting available shipping methods to make it easier to understand and prevent human-made errors. - #4367 by @NyanKiyoshi
+- Moved 3D secure option to Braintree plugin configuration and update config structure mechanism - #4751 by @salwator
## 2.7.0
diff --git a/saleor/extensions/base_plugin.py b/saleor/extensions/base_plugin.py
index b28f397756..142279ea47 100644
--- a/saleor/extensions/base_plugin.py
+++ b/saleor/extensions/base_plugin.py
@@ -1,3 +1,4 @@
+from copy import copy
from decimal import Decimal
from typing import TYPE_CHECKING, Any, List, Union
@@ -221,19 +222,38 @@ class BasePlugin:
@classmethod
def _append_config_structure(cls, configuration):
config_structure = getattr(cls, "CONFIG_STRUCTURE") or {}
- for coniguration_field in configuration:
+ for configuration_field in configuration:
- structure_to_add = config_structure.get(coniguration_field.get("name"))
+ structure_to_add = config_structure.get(configuration_field.get("name"))
if structure_to_add:
- coniguration_field.update(structure_to_add)
+ configuration_field.update(structure_to_add)
return config_structure
+ @classmethod
+ def _update_configuration_structure(cls, configuration):
+ config_structure = getattr(cls, "CONFIG_STRUCTURE") or {}
+ desired_config_keys = set(config_structure.keys())
+
+ config = configuration.configuration or []
+ configured_keys = set(d["name"] for d in config)
+ missing_keys = desired_config_keys - configured_keys
+
+ if not missing_keys:
+ return
+
+ default_config = cls._get_default_configuration()["configuration"]
+ update_values = [copy(k) for k in default_config if k["name"] in missing_keys]
+ config.extend(update_values)
+
+ configuration.save(update_fields=["configuration"])
+
@classmethod
def get_plugin_configuration(cls, queryset: QuerySet) -> "PluginConfiguration":
defaults = cls._get_default_configuration()
configuration = queryset.get_or_create(name=cls.PLUGIN_NAME, defaults=defaults)[
0
]
+ cls._update_configuration_structure(configuration)
if configuration.configuration:
# Let's add a translated descriptions and labels
cls._append_config_structure(configuration.configuration)
diff --git a/saleor/payment/interface.py b/saleor/payment/interface.py
index 0f49e6f0fb..a22c871e7c 100644
--- a/saleor/payment/interface.py
+++ b/saleor/payment/interface.py
@@ -91,6 +91,7 @@ class GatewayConfig:
# a unified structure
connection_params: Dict[str, Any]
store_customer: bool = False
+ require_3d_secure: bool = False
@dataclass
diff --git a/tests/extensions/test_manager.py b/tests/extensions/test_manager.py
index ef234e1551..81e6e4be58 100644
--- a/tests/extensions/test_manager.py
+++ b/tests/extensions/test_manager.py
@@ -7,6 +7,8 @@ from prices import Money, TaxedMoney
from saleor.core.payments import Gateway
from saleor.core.taxes import TaxType
+
+from saleor.extensions import ConfigurationTypeField
from saleor.extensions.base_plugin import BasePlugin
from saleor.extensions.manager import ExtensionsManager, get_extensions_manager
from saleor.extensions.models import PluginConfiguration
@@ -14,6 +16,9 @@ from saleor.extensions.models import PluginConfiguration
class SamplePlugin(BasePlugin):
PLUGIN_NAME = "Sample Plugin"
+ CONFIG_STRUCTURE = {
+ "Test": {"type": ConfigurationTypeField.BOOLEAN, "help_text": "", "label": ""}
+ }
def calculate_checkout_total(self, checkout, discounts, previous_value):
total = Money("1.0", currency=checkout.get_total().currency)
@@ -70,7 +75,7 @@ class SamplePlugin(BasePlugin):
"name": "Sample Plugin",
"description": "",
"active": True,
- "configuration": None,
+ "configuration": [{"name": "Test", "value": True}],
}
return defaults
@@ -294,6 +299,37 @@ def test_manager_save_plugin_configuration():
assert not configuration.active
+def test_plugin_updates_configuration_shape():
+ new_config = {"name": "Foo", "value": "bar"}
+
+ @classmethod
+ def new_default_configuration(cls):
+ defaults = {
+ "name": "Sample Plugin",
+ "description": "",
+ "active": True,
+ "configuration": [{"name": "Test", "value": True}, new_config],
+ }
+ return defaults
+
+ new_config_structure = {
+ "type": ConfigurationTypeField.STRING,
+ "help_text": "foo",
+ "label": "foo",
+ }
+ plugins = ["tests.extensions.test_manager.SamplePlugin"]
+ manager = ExtensionsManager(plugins=plugins)
+ manager.get_plugin_configuration(plugin_name="Sample Plugin")
+
+ SamplePlugin._get_default_configuration = new_default_configuration
+ SamplePlugin.CONFIG_STRUCTURE["Foo"] = new_config_structure
+
+ configuration = manager.get_plugin_configuration(plugin_name="Sample Plugin")
+ configuration.refresh_from_db()
+ assert len(configuration.configuration) == 2
+ assert configuration.configuration[1] == new_config
+
+
class ActivePaymentGateway(BasePlugin):
PLUGIN_NAME = "braintree"