diff --git a/openslides/utils/collection.py b/openslides/utils/collection.py index 3cd25aefd..0b4709507 100644 --- a/openslides/utils/collection.py +++ b/openslides/utils/collection.py @@ -38,7 +38,12 @@ class CollectionElement: self.information = information or {} if instance is not None: self.collection_string = instance.get_collection_string() - self.id = instance.pk + from openslides.core.config import config + if self.collection_string == config.get_collection_string(): + # For config objects we do not work with the pk but with the key. + self.id = instance.key + else: + self.id = instance.pk elif collection_string is not None and id is not None: self.collection_string = collection_string self.id = id @@ -144,8 +149,7 @@ class CollectionElement: # The config instance has to be get from the config element, because # some config values are not in the db. from openslides.core.config import config - if (self.collection_string == config.get_collection_string() and - isinstance(self.id, str)): + if self.collection_string == config.get_collection_string(): self.instance = {'key': self.id, 'value': config[self.id]} else: model = self.get_model() diff --git a/tests/unit/utils/test_collection.py b/tests/unit/utils/test_collection.py index 9886fdba2..9867e9ee6 100644 --- a/tests/unit/utils/test_collection.py +++ b/tests/unit/utils/test_collection.py @@ -225,6 +225,22 @@ class TestCollectionElement(TestCase): collection.CollectionElement.from_values('testmodule/model', 1), collection.CollectionElement.from_values('testmodule/other_model', 1)) + def test_config_cache_key(self): + """ + Test that collection elements for config values do always use the + config key as cache key. + """ + fake_config_instance = MagicMock() + fake_config_instance.get_collection_string.return_value = 'core/config' + fake_config_instance.key = 'test_config_key' + + self.assertEqual( + collection.CollectionElement.from_values('core/config', 'test_config_key').get_cache_key(), + 'core/config:test_config_key') + self.assertEqual( + collection.CollectionElement.from_instance(fake_config_instance).get_cache_key(), + 'core/config:test_config_key') + class TestCollection(TestCase): @patch('openslides.utils.collection.CollectionElement')