Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Apply changes after review
Maciej Korycinskief3017991409
diff --git a/docs/customization/extensions.rst b/docs/customization/extensions.rstindex 99f1318a82..de95631960 100644--- a/docs/customization/extensions.rst+++ b/docs/customization/extensions.rst@@ -59,17 +59,37 @@ To activate the plugin, add it to the ``PLUGINS`` list in your Django settings:PLUGINS = ["saleor.extensions.plugins.custom.CustomPlugin", ]-Configuring Plugin-^^^^^^^^^^^^^^^^^^+Configuring Plugins+^^^^^^^^^^^^^^^^^^^Saleor allows you to change a configuration of the given plugin over API.-Plugin owner needs to overwrite a method to create a structure of default configuration ``_get_default_configuration``.+Plugin owner needs to overwrite a method to create a structure of default configuration ``_get_default_configuration``+and ``CONFIG_STRUCTURE`` .It requires an expected structure as in the followed example:.. code-block:: pythonfrom django.utils.translation import pgettext_lazy+from saleor.extensions import ConfigurationTypeField++ CONFIG_STRUCTURE = {+ "Username or account": {+ "type": ConfigurationTypeField.STRING,+ "help_text": pgettext_lazy(+ "Plugin help text", "Provide user or account details"+ ),+ "label": pgettext_lazy("Plugin label", "Username or account"),+ },+ "Password or license": {+ "type": ConfigurationTypeField.STRING,+ "help_text": pgettext_lazy(+ "Plugin help text", "Provide password or license details"+ ),+ "label": pgettext_lazy("Plugin label", "Password or license"),+ }+ }+@classmethoddef _get_default_configuration(cls):defaults = {@@ -80,25 +100,19 @@ It requires an expected structure as in the followed example:{"name": "Username or account","value": "",- "type": ConfigurationTypeField.STRING,- "help_text": "Provide user or account details",- "label": pgettext_lazy("Plugin label", "Username or account"),},{"name": "Password or license","value": "",- "type": ConfigurationTypeField.STRING,- "help_text": "Provide password or license details",- "label": pgettext_lazy("Plugin label", "Password or license"),},]}return defaults-``ExtensionManager`` will use these data to create default configuration in DB which will be served by API.+``ExtensionManager`` will use this data to create default configuration in DB which will be served by API.-By using graphQL queries - ``pluginConfigurations`` and ``pluginConfiguration`` user will be able to list all enabled plugins.+By using GraphQL queries - ``pluginConfigurations`` and ``pluginConfiguration`` user will be able to list all enabled plugins.Mutation ``pluginConfigurationUpdate`` will allow the user to active/disable and update configuration fields like ``API keys``for a given plugin.@@ -114,9 +128,9 @@ API serves response with the given fields:+------------------+-----------------------------------------------------------------------------+| active | Indicate if the plugin is activated or not |+------------------+-----------------------------------------------------------------------------+-| description | Quick description of the plugin |+| description | Description of the plugin |+------------------+-----------------------------------------------------------------------------+-| configuration | It stores all configuration fields as a list that can be changed by a user |+| configuration | It stores all configuration fields as a list that can be changed by a user |+------------------+-----------------------------------------------------------------------------+Configuration fields:@@ -124,21 +138,21 @@ Configuration fields:+----------+-----------------------------------------------------------------+| Name | Description |+==========+=================================================================+-| name | name of the field |+| name | Name of the field |+----------+-----------------------------------------------------------------+-| value | current value of the field |+| value | Current value of the field |+----------+-----------------------------------------------------------------+-| type | type of the field. Saleor supports - ``String`` and ``Boolean`` |+| type | Type of the field. Saleor supports - ``String`` and ``Boolean`` |+----------+-----------------------------------------------------------------+-| helpText | description of the field |+| helpText | Description of the field |+----------+-----------------------------------------------------------------+-| label | label for the field |+| label | Label for the field |+----------+-----------------------------------------------------------------+ExtensionsManager------------------ExtensionsManager is located in ``saleor.extensions.base_plugin``.+``ExtensionsManager`` is located in ``saleor.extensions.base_plugin``.It is a class responsible for handling all declared plugins and serving a response from them.It serves a default response in case of a non-declared plugin. There is a possibility to overwrite an ExtensionsManagerclass by implementing it on its own. Saleor will discover the manager class by taking the declared path from@@ -148,7 +162,7 @@ Each Django request object has its own manager included as the ``extensions`` fiBasePlugin-----------BasePlugin is located in ``saleor.extensions.base_plugin``. It is an abstract class for storing all methods+``BasePlugin`` is located in ``saleor.extensions.base_plugin``. It is an abstract class for storing all methodsavailable for any plugin. All methods take the ``previous_value`` parameter. This contains a valuecalculated by the previous plugin in the queue. If the plugin is first in line, it will use the default value calculated bythe manager.diff --git a/saleor/extensions/__init__.py b/saleor/extensions/__init__.pyindex d01bc94286..2ff4b876ae 100644--- a/saleor/extensions/__init__.py+++ b/saleor/extensions/__init__.py@@ -25,14 +25,9 @@ class ConfigurationTypeField:STRING = "String"BOOLEAN = "Boolean"CHOICES = [- (- STRING,- pgettext_lazy("Configuration field is a String type", "Field is a String"),- ),+ (STRING, pgettext_lazy("Type of the configuration field", "Field is a String")),(BOOLEAN,- pgettext_lazy(- "Configuration field is a Boolean type", "Field is a Boolean"- ),+ pgettext_lazy("Type of the configuration field", "Field is a Boolean"),),]diff --git a/saleor/extensions/base_plugin.py b/saleor/extensions/base_plugin.pyindex 348ac0d841..8f18b74951 100644--- a/saleor/extensions/base_plugin.py+++ b/saleor/extensions/base_plugin.py@@ -24,9 +24,10 @@ class BasePlugin:"""PLUGIN_NAME = ""+ CONFIG_STRUCTURE = Nonedef __init__(self, *args, **kwargs):- self._cached_config_from_db = None+ self._cached_config = Noneself.active = Nonedef __str__(self):@@ -34,10 +35,10 @@ class BasePlugin:def _initialize_plugin_configuration(self):plugin_config_qs = PluginConfiguration.objects.filter(name=self.PLUGIN_NAME)- plugin_config = self._cached_config_from_db or plugin_config_qs.first()+ plugin_config = self._cached_config or plugin_config_qs.first()if plugin_config:- self._cached_config_from_db = plugin_config+ self._cached_config = plugin_configself.active = plugin_config.activedef calculate_checkout_total(@@ -163,10 +164,23 @@ class BasePlugin:defaults = Nonereturn defaults+ @classmethod+ def _append_config_structure(cls, configuration):+ config_structure = getattr(cls, "CONFIG_STRUCTURE", {})+ for coniguration_field in configuration:++ structure_to_add = config_structure.get(coniguration_field.get("name"))+ if structure_to_add:+ coniguration_field.update(structure_to_add)+ return config_structure+@classmethoddef get_plugin_configuration(cls, queryset: QuerySet) -> "PluginConfiguration":defaults = cls._get_default_configuration()configuration = queryset.get_or_create(name=cls.PLUGIN_NAME, defaults=defaults)[0]+ if configuration.configuration:+ # Let's add a translated descriptions and labels+ cls._append_config_structure(configuration.configuration)return configurationdiff --git a/saleor/extensions/plugins/avatax/plugin.py b/saleor/extensions/plugins/avatax/plugin.pyindex 0e530efc67..41baccc763 100644--- a/saleor/extensions/plugins/avatax/plugin.py+++ b/saleor/extensions/plugins/avatax/plugin.py@@ -36,6 +36,50 @@ logger = logging.getLogger(__name__)class AvataxPlugin(BasePlugin):PLUGIN_NAME = "Avalara"+ CONFIG_STRUCTURE = {+ "Username or account": {+ "type": ConfigurationTypeField.STRING,+ "help_text": pgettext_lazy(+ "Plugin help text", "Provide user or account details"+ ),+ "label": pgettext_lazy("Plugin label", "Username or account"),+ },+ "Password or license": {+ "type": ConfigurationTypeField.STRING,+ "help_text": pgettext_lazy(+ "Plugin help text", "Provide password or license details"+ ),+ "label": pgettext_lazy("Plugin label", "Password or license"),+ },+ "Use sandbox": {+ "type": ConfigurationTypeField.BOOLEAN,+ "help_text": pgettext_lazy(+ "Plugin help text",+ "Determines if Saleor should use Avatax sandbox API.",+ ),+ "label": pgettext_lazy("Plugin label", "Use sandbox"),+ },+ "Company name": {+ "type": ConfigurationTypeField.STRING,+ "help_text": pgettext_lazy(+ "Plugin help text",+ "Avalara needs to receive company code. Some more "+ "complicated systems can use more than one company "+ "code, in that case, this variable should be changed "+ "based on data from Avalara's admin panel",+ ),+ "label": pgettext_lazy("Plugin label", "Company name"),+ },+ "Autocommit": {+ "type": ConfigurationTypeField.BOOLEAN,+ "help_text": pgettext_lazy(+ "Plugin help text",+ "Determines, if all transactions sent to Avalara "+ "should be committed by default.",+ ),+ "label": pgettext_lazy("Plugin label", "Autocommit"),+ },+ }def __init__(self, *args, **kwargs):super().__init__(*args, **kwargs)@@ -44,8 +88,8 @@ class AvataxPlugin(BasePlugin):def _initialize_plugin_configuration(self):super()._initialize_plugin_configuration()- if self._cached_config_from_db and self._cached_config_from_db.configuration:- configuration = self._cached_config_from_db.configuration+ if self._cached_config and self._cached_config.configuration:+ configuration = self._cached_config.configuration# Convert to dict to easier take config elementsconfiguration = {item["name"]: item["value"] for item in configuration}@@ -378,45 +422,11 @@ class AvataxPlugin(BasePlugin):"description": "","active": False,"configuration": [- {- "name": "Username or account",- "value": "",- "type": ConfigurationTypeField.STRING,- "help_text": "Provide user or account details",- "label": pgettext_lazy("Plugin label", "Username or account"),- },- {- "name": "Password or license",- "value": "",- "type": ConfigurationTypeField.STRING,- "help_text": "Provide password or license details",- "label": pgettext_lazy("Plugin label", "Password or license"),- },- {- "name": "Use sandbox",- "value": True,- "type": ConfigurationTypeField.BOOLEAN,- "help_text": "Determines if Saleor should use Avatax sandbox API.",- "label": pgettext_lazy("Plugin label", "Use sandbox"),- },- {- "name": "Company name",- "value": "DEFAULT",- "type": ConfigurationTypeField.STRING,- "help_text": "Avalara needs to receive company code. Some more "- "complicated systems can use more than one company "- "code, in that case, this variable should be changed "- "based on data from Avalara's admin panel",- "label": pgettext_lazy("Plugin label", "Company name"),- },- {- "name": "Autocommit",- "value": False,- "type": ConfigurationTypeField.BOOLEAN,- "help_text": "Determines, if all transactions sent to Avalara "- "should be committed by default.",- "label": pgettext_lazy("Plugin label", "Autocommit"),- },+ {"name": "Username or account", "value": ""},+ {"name": "Password or license", "value": ""},+ {"name": "Use sandbox", "value": True},+ {"name": "Company name", "value": "DEFAULT"},+ {"name": "Autocommit", "value": False},],}return defaultsdiff --git a/saleor/extensions/plugins/vatlayer/plugin.py b/saleor/extensions/plugins/vatlayer/plugin.pyindex 2be7e4784d..39abdae6a5 100644--- a/saleor/extensions/plugins/vatlayer/plugin.py+++ b/saleor/extensions/plugins/vatlayer/plugin.py@@ -37,7 +37,7 @@ class VatlayerPlugin(BasePlugin):def _initialize_plugin_configuration(self):super()._initialize_plugin_configuration()- if not self._cached_config_from_db:+ if not self._cached_config:# This should be removed after we drop an Vatl's settings from Django# settings fileself.active = bool(settings.VATLAYER_ACCESS_KEY)