diff --git a/CHANGELOG b/CHANGELOG index 920e4611e..de619f577 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -103,9 +103,10 @@ Core: - Added caching for the index views [#3419, #3424]. - Added projector prioritization [#3425]. - Use native twisted mode for daphne [#3487]. -- Save language selection to session storage [#3543] +- Saved language selection to session storage [#3543]. - Set default of projector resolution to 1220x915 [#2549]. - Preparations for the SAML plugin; Fixed caching of main views [#3535]. +- Removed unnecessary OPTIONS request in config [#3541]. Mediafiles: - Fixed reloading of PDF on page change [#3274]. diff --git a/openslides/core/apps.py b/openslides/core/apps.py index 8f3c79f7e..5cc7e8141 100644 --- a/openslides/core/apps.py +++ b/openslides/core/apps.py @@ -1,3 +1,7 @@ +from collections import OrderedDict +from operator import attrgetter +from typing import Any, List # noqa + from django.apps import AppConfig from django.conf import settings from django.db.models.signals import post_migrate @@ -68,6 +72,8 @@ class CoreAppConfig(AppConfig): yield Collection(self.get_model(model).get_collection_string()) def get_angular_constants(self): + from .config import config + # Client settings client_settings_keys = [ 'MOTIONS_ALLOW_AMENDMENTS_OF_AMENDMENTS' @@ -83,7 +89,30 @@ class CoreAppConfig(AppConfig): client_settings = { 'name': 'OpenSlidesSettings', 'value': client_settings_dict} - return [client_settings] + + # Config variables + config_groups = [] # type: List[Any] # TODO: Replace Any by correct type + for config_variable in sorted(config.config_variables.values(), key=attrgetter('weight')): + if config_variable.is_hidden(): + # Skip hidden config variables. Do not even check groups and subgroups. + continue + if not config_groups or config_groups[-1]['name'] != config_variable.group: + # Add new group. + config_groups.append(OrderedDict( + name=config_variable.group, + subgroups=[])) + if not config_groups[-1]['subgroups'] or config_groups[-1]['subgroups'][-1]['name'] != config_variable.subgroup: + # Add new subgroup. + config_groups[-1]['subgroups'].append(OrderedDict( + name=config_variable.subgroup, + items=[])) + # Add the config variable to the current group and subgroup. + config_groups[-1]['subgroups'][-1]['items'].append(config_variable.data) + config_variables = { + 'name': 'OpenSlidesConfigVariables', + 'value': config_groups} + + return [client_settings, config_variables] def call_save_default_values(**kwargs): diff --git a/openslides/core/config.py b/openslides/core/config.py index fc0869e9d..3d29ebf6e 100644 --- a/openslides/core/config.py +++ b/openslides/core/config.py @@ -281,7 +281,7 @@ class ConfigVariable: @property def data(self) -> ConfigVariableDict: """ - Property with all data for OPTIONS requests. + Property with all data for AngularJS variable on startup. """ return ConfigVariableDict( key=self.name, diff --git a/openslides/core/static/js/core/base.js b/openslides/core/static/js/core/base.js index b2ce650b8..b9046c86d 100644 --- a/openslides/core/static/js/core/base.js +++ b/openslides/core/static/js/core/base.js @@ -878,13 +878,6 @@ angular.module('OpenSlidesApp.core', [ return DS.defineResource({ name: 'core/config', idAttribute: 'key', - configOptions: configOptions, - getConfigOptions: function () { - if (!this.configOptions) { - this.configOptions = $http({ 'method': 'OPTIONS', 'url': '/rest/core/config/' }); - } - return this.configOptions; - }, translate: function (value) { return gettextCatalog.getString(value); } diff --git a/openslides/core/static/js/core/site.js b/openslides/core/static/js/core/site.js index 40c29fdae..ba3dbbb06 100644 --- a/openslides/core/static/js/core/site.js +++ b/openslides/core/static/js/core/site.js @@ -360,11 +360,6 @@ angular.module('OpenSlidesApp.core.site', [ .state('config', { url: '/config', controller: 'ConfigCtrl', - resolve: { - configOptions: function(Config) { - return Config.getConfigOptions(); - } - }, data: { title: gettext('Settings'), basePerm: 'core.can_manage_config', @@ -1119,14 +1114,14 @@ angular.module('OpenSlidesApp.core.site', [ '$timeout', 'MajorityMethodChoices', 'Config', - 'configOptions', + 'OpenSlidesConfigVariables', 'gettextCatalog', 'DateTimePickerTranslation', 'Editor', - function($scope, $timeout, MajorityMethodChoices, Config, configOptions, + function($scope, $timeout, MajorityMethodChoices, Config, OpenSlidesConfigVariables, gettextCatalog, DateTimePickerTranslation, Editor) { Config.bindAll({}, $scope, 'configs'); - $scope.configGroups = configOptions.data.config_groups; + $scope.configGroups = OpenSlidesConfigVariables; $scope.dateTimePickerTranslatedButtons = DateTimePickerTranslation.getButtons(); $scope.ckeditorOptions = Editor.getOptions(); diff --git a/openslides/core/views.py b/openslides/core/views.py index 7b5cd5a3c..b22d961a8 100644 --- a/openslides/core/views.py +++ b/openslides/core/views.py @@ -1,7 +1,5 @@ import json import uuid -from collections import OrderedDict -from operator import attrgetter from textwrap import dedent from typing import Any, Dict, List, cast # noqa @@ -29,7 +27,6 @@ from ..utils.plugins import ( from ..utils.rest_api import ( ModelViewSet, Response, - SimpleMetadata, ValidationError, detail_route, list_route, @@ -581,36 +578,6 @@ class TagViewSet(ModelViewSet): return result -class ConfigMetadata(SimpleMetadata): - """ - Custom metadata class to add config info to responses on OPTIONS requests. - """ - def determine_metadata(self, request, view): - # Build tree. - config_groups = [] # type: List[Any] # TODO: Replace Any by correct type - for config_variable in sorted(config.config_variables.values(), key=attrgetter('weight')): - if config_variable.is_hidden(): - # Skip hidden config variables. Do not even check groups and subgroups. - continue - if not config_groups or config_groups[-1]['name'] != config_variable.group: - # Add new group. - config_groups.append(OrderedDict( - name=config_variable.group, - subgroups=[])) - if not config_groups[-1]['subgroups'] or config_groups[-1]['subgroups'][-1]['name'] != config_variable.subgroup: - # Add new subgroup. - config_groups[-1]['subgroups'].append(OrderedDict( - name=config_variable.subgroup, - items=[])) - # Add the config variable to the current group and subgroup. - config_groups[-1]['subgroups'][-1]['items'].append(config_variable.data) - - # Add tree to metadata. - metadata = super().determine_metadata(request, view) - metadata['config_groups'] = config_groups - return metadata - - class ConfigViewSet(ModelViewSet): """ API endpoint for the config. @@ -620,7 +587,6 @@ class ConfigViewSet(ModelViewSet): """ access_permissions = ConfigAccessPermissions() queryset = ConfigStore.objects.all() - metadata_class = ConfigMetadata def check_view_permissions(self): """ diff --git a/tests/integration/core/test_views.py b/tests/integration/core/test_views.py index 940b095be..9482b50e8 100644 --- a/tests/integration/core/test_views.py +++ b/tests/integration/core/test_views.py @@ -197,14 +197,6 @@ class ConfigViewSet(TestCase): self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response.data, {'detail': 'Invalid input. Config value is missing.'}) - def test_metadata_with_hidden(self): - self.client.login(username='admin', password='admin') - response = self.client.options(reverse('config-list')) - filter_obj = filter( - lambda item: item['key'] == 'test_var_pud2zah2teeNaiP7IoNa', - response.data['config_groups'][0]['subgroups'][0]['items']) - self.assertEqual(len(list(filter_obj)), 0) - def validator_for_testing(value): """ diff --git a/tests/unit/config/test_api.py b/tests/unit/config/test_api.py index 400683f18..3d57f9975 100644 --- a/tests/unit/config/test_api.py +++ b/tests/unit/config/test_api.py @@ -26,8 +26,8 @@ class TestConfigVariable(TestCase): class TestConfigHandler(TestCase): - def test_get_not_found(self): - config.key_to_id = 'has to be set or there is a db query' + @patch('openslides.core.config.ConfigHandler.save_default_values') + def test_get_not_found(self, mock_save_default_values): self.assertRaises( ConfigNotFound, config.__getitem__, diff --git a/tests/unit/core/test_views.py b/tests/unit/core/test_views.py index 492318c28..5b14362e9 100644 --- a/tests/unit/core/test_views.py +++ b/tests/unit/core/test_views.py @@ -170,10 +170,11 @@ class WebclientJavaScriptView(TestCase): def setUp(self): self.request = MagicMock() + @patch('openslides.core.config.config') @patch('django.contrib.auth.models.Permission.objects.all') - def test_permissions_as_constant(self, mock_all): + def test_permissions_as_constant(self, mock_permissions_all, mock_config): self.view_instance = views.WebclientJavaScriptView() self.view_instance.request = self.request response = self.view_instance.get(realm='site') self.assertEqual(response.status_code, 200) - self.assertEqual(mock_all.call_count, 2) + self.assertEqual(mock_permissions_all.call_count, 2)