diff --git a/openslides/core/config.py b/openslides/core/config.py index be055244b..5b836cce1 100644 --- a/openslides/core/config.py +++ b/openslides/core/config.py @@ -60,7 +60,11 @@ class ConfigHandler: # Validate datatype and run validators. expected_type = INPUT_TYPE_MAPPING[config_variable.input_type] - if not isinstance(value, expected_type): + + # Try to convert value into the expected datatype + try: + value = expected_type(value) + except ValueError: raise ConfigError(_('Wrong datatype. Expected %s, got %s.') % (expected_type, type(value))) if config_variable.input_type == 'choice' and value not in map(lambda choice: choice['value'], config_variable.choices): raise ConfigError(_('Invalid input. Choice does not match.')) diff --git a/openslides/core/static/js/core.js b/openslides/core/static/js/core.js index 547656cfb..39e8c0362 100644 --- a/openslides/core/static/js/core.js +++ b/openslides/core/static/js/core.js @@ -250,7 +250,7 @@ angular.module('OpenSlidesApp.core.site', ['OpenSlidesApp.core']) controller: 'ConfigCtrl', resolve: { configOption: function($http) { - return $http({ 'method': 'OPTIONS', 'url': '/rest/config/config/' }); + return $http({ 'method': 'OPTIONS', 'url': '/rest/core/config/' }); } } }) @@ -351,7 +351,7 @@ angular.module('OpenSlidesApp.core.site', ['OpenSlidesApp.core']) string: 'text', integer: 'number', boolean: 'checkbox', - choice: 'radio', + choice: 'choice', }[type]; } @@ -363,8 +363,11 @@ angular.module('OpenSlidesApp.core.site', ['OpenSlidesApp.core']) var field = $parse(iAttrs.field)($scope); var config = Config.get(field.key); $scope.type = getHtmlType(field.input_type); + if ($scope.type == 'choice') { + $scope.choices = field.choices; + } $scope.label = field.label; - $scope.id = 'field-' + field.id; + $scope.key = 'field-' + field.key; $scope.value = config.value; $scope.help_text = field.help_text; } @@ -433,12 +436,7 @@ angular.module('OpenSlidesApp.core.site', ['OpenSlidesApp.core']) $scope.configGroups = configOption.data.config_groups; // save changed config value - $scope.save = function(key, value, type) { - // TODO: find a better way to check the type without using of - // the extra parameter 'type' from template - if (type == 'number') { - value = parseInt(value); - } + $scope.save = function(key, value) { Config.get(key).value = value; Config.save(key); } diff --git a/openslides/core/static/templates/config-form-field.html b/openslides/core/static/templates/config-form-field.html index c5348732b..c49fdc249 100644 --- a/openslides/core/static/templates/config-form-field.html +++ b/openslides/core/static/templates/config-form-field.html @@ -2,10 +2,15 @@
- + + + - diff --git a/openslides/core/static/templates/config.html b/openslides/core/static/templates/config.html index 8548f6057..150ff9e9e 100644 --- a/openslides/core/static/templates/config.html +++ b/openslides/core/static/templates/config.html @@ -11,7 +11,7 @@ {{ group.name }} -
+
@@ -21,7 +21,8 @@
- + + diff --git a/tests/integration/core/test_views.py b/tests/integration/core/test_views.py index 53efa77f0..85edaf4dc 100644 --- a/tests/integration/core/test_views.py +++ b/tests/integration/core/test_views.py @@ -97,6 +97,18 @@ class ConfigViewSet(TestCase): self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.data, {'detail': "Wrong datatype. Expected , got ."}) + def test_update_wrong_datatype_that_can_be_converted(self): + """ + Try to send a string that can be converted to an integer to an integer + field. + """ + self.client = APIClient() + self.client.login(username='admin', password='admin') + response = self.client.put( + reverse('config-detail', args=['test_var_ohhii4iavoh5Phoh5ahg']), + {'value': '12345'}) + self.assertEqual(response.status_code, 200) + def test_update_good_choice(self): self.client = APIClient() self.client.login(username='admin', password='admin')