Skip to content

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

Add fields to parse properly values

Mira Szewczykf6fa5719b500
diff --git a/saleor/dashboard/forms.py b/saleor/dashboard/forms.py
index 08ff609282..6fc3c2675f 100644
--- a/saleor/dashboard/forms.py
+++ b/saleor/dashboard/forms.py
@@ -198,3 +198,24 @@ class PermissionMultipleChoiceField(forms.ModelMultipleChoiceField):
def label_from_instance(self, obj):
return obj.name
+
+
+class ConfigField(forms.Field):
+ def __init__(self, *args, **kwargs):
+ structure = kwargs.pop("structure")
+ super().__init__(*args, **kwargs)
+ self.label = structure["name"]
+ self.name = structure["name"]
+ self.help_text = structure["help_text"]
+ self.initial = structure["value"]
+
+
+class ConfigBooleanField(forms.BooleanField, ConfigField):
+ def to_python(self, value):
+ return {"name": self.label, "value": super().to_python(value)}
+
+
+class ConfigCharField(forms.CharField, ConfigField):
+ def clean(self, value):
+ parsed_value = super().clean(value)
+ return {"name": self.label, "value": parsed_value}
diff --git a/saleor/dashboard/payment/forms.py b/saleor/dashboard/payment/forms.py
index 7a23697c11..519cbb2815 100644
--- a/saleor/dashboard/payment/forms.py
+++ b/saleor/dashboard/payment/forms.py
@@ -1,40 +1,38 @@
-from typing import Any, Dict
+from typing import Dict
from django import forms
from django.utils.text import slugify
from ...extensions import ConfigurationTypeField
from ...extensions.models import PluginConfiguration
+from ..forms import ConfigBooleanField, ConfigCharField
TYPE_TO_FIELD = {
- ConfigurationTypeField.STRING: forms.CharField,
- ConfigurationTypeField.BOOLEAN: forms.BooleanField,
+ ConfigurationTypeField.STRING: ConfigCharField,
+ ConfigurationTypeField.BOOLEAN: ConfigBooleanField,
}
class GatewayConfigurationForm(forms.Form):
- active = forms.BooleanField(required=False)
+ active = forms.BooleanField()
def __init__(self, plugin, *args, **kwargs):
self.plugin = plugin
super().__init__(*args, **kwargs)
- current_configuration = self._get_current_configuration()
+ self.current_configuration = self._get_current_configuration()
# add new fields specified for gateway
- self.fields.update(self._prepare_fields_for_given_config(current_configuration))
- self.fields["active"].initial = current_configuration.active
+ self.fields.update(
+ self._prepare_fields_for_given_config(self.current_configuration)
+ )
+ self.fields["active"].initial = self.current_configuration.active
def _get_current_configuration(self) -> PluginConfiguration:
qs = PluginConfiguration.objects.all()
return self.plugin.get_plugin_configuration(qs)
- def _create_field(self, structure: Dict[str, Any]) -> forms.Field:
- label = structure["label"]
- help_text = structure["help_text"]
+ def _create_field(self, structure: Dict[str, str]) -> forms.Field:
elem_type = structure["type"]
- current_value = structure["value"]
- return TYPE_TO_FIELD[elem_type](
- label=label, help_text=help_text, initial=current_value
- )
+ return TYPE_TO_FIELD[elem_type](structure=structure)
def _prepare_fields_for_given_config(
self, current_configuration: PluginConfiguration
@@ -43,8 +41,12 @@ class GatewayConfigurationForm(forms.Form):
structure = current_configuration.configuration
if structure is None:
raise Exception
-
for elem in structure:
slug = slugify(elem["name"])
parsed_fields[slug] = self._create_field(elem)
return parsed_fields
+
+ def clean(self):
+ cleaned_data = super().clean()
+
+ return cleaned_data
diff --git a/tests/dashboard/test_payment_form.py b/tests/dashboard/test_payment_form.py
index 77c13e7453..fda676a16d 100644
--- a/tests/dashboard/test_payment_form.py
+++ b/tests/dashboard/test_payment_form.py
@@ -1,6 +1,10 @@
import pytest
-from saleor.dashboard.payment.forms import GatewayConfigurationForm
+from saleor.dashboard.payment.forms import (
+ ConfigBooleanField,
+ ConfigCharField,
+ GatewayConfigurationForm,
+)
from saleor.extensions.models import PluginConfiguration
from saleor.payment.gateways.braintree.plugin import BraintreeGatewayPlugin
@@ -29,6 +33,11 @@ def plugin_configuration(db):
return plugin_configuration
+def gateway_config_form():
+ return GatewayConfigurationForm(BraintreeGatewayPlugin)
+
+
@pytest.fixture(autouse=True)
def enable_plugin(settings):
settings.PLUGINS = [
@@ -36,6 +45,57 @@ def enable_plugin(settings):
]
-def test_configuration_form_get_current_configuration(plugin_configuration):
- form = GatewayConfigurationForm(BraintreeGatewayPlugin)
- assert form._get_current_configuration() == plugin_configuration
+def config_boolean_structure():
+ return {
+ "name": "Require 3D secure",
+ "value": True,
+ "type": "Boolean",
+ "help_text": "Determines if Saleor should enforce 3D secure during payment.",
+ "label": "Require 3D secure",
+ }
+
+
+def config_char_field():
+ return {
+ "name": "Template path",
+ "value": "order/payment/braintree.html",
+ "type": "String",
+ "help_text": "Location of django payment template for gateway.",
+ "label": "Template path",
+ }
+
+
+def test_configuration_form_get_current_configuration(
+ plugin_configuration, gateway_config_form
+):
+ assert gateway_config_form._get_current_configuration() == plugin_configuration
+
+
+def test_configuration_form__create_field(gateway_config_form, config_char_field):
+ template_field = gateway_config_form._create_field(config_char_field)
+ assert isinstance(template_field, ConfigCharField)
+ assert template_field.label == config_char_field["name"]
+ assert template_field.initial == config_char_field["value"]
+
+
+def test_config_boolean_field_returned_value(config_boolean_structure):
+ boolean_field = ConfigBooleanField(structure=config_boolean_structure)
+ assert boolean_field.clean(True) == {
+ "name": config_boolean_structure["name"],
+ "value": True,
+ }
+ assert boolean_field.clean(False) == {
+ "name": config_boolean_structure["name"],
+ "value": False,
+ }
+
+
+def test_config_char_field_returned_value(config_char_field):
+ char_field = ConfigCharField(structure=config_char_field)
+ value = "1234466"
+ assert char_field.clean(value) == {
+ "name": config_char_field["name"],
+ "value": value,
+ }