From 74c1cc63dc33f19a34fc5ce8c2c27b7f33a731a5 Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Tue, 25 Apr 2017 14:56:49 +0200 Subject: [PATCH] Open websocket connections immediately Also made a performance boost to get_model_from_collection_string() --- openslides/utils/autoupdate.py | 5 +++-- openslides/utils/collection.py | 29 +++++++++++++++-------------- tests/unit/utils/test_collection.py | 2 +- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/openslides/utils/autoupdate.py b/openslides/utils/autoupdate.py index 4218fb2ce..cd27f37d5 100644 --- a/openslides/utils/autoupdate.py +++ b/openslides/utils/autoupdate.py @@ -56,6 +56,9 @@ def ws_add_site(message): # Saves the reply channel to the user. Uses 0 for anonymous users. websocket_user_cache.add(message.user.id or 0, message.reply_channel.name) + # Open the websocket connection. + send_or_wait(message.reply_channel.send, {'accept': True}) + # Collect all elements that shoud be send to the client when the websocket # connection is established output = [] @@ -74,8 +77,6 @@ def ws_add_site(message): # Send all data. If there is no data, then only accept the connection if output: send_or_wait(message.reply_channel.send, {'text': json.dumps(output)}) - else: - send_or_wait(message.reply_channel.send, {'accept': True}) @channel_session_user diff --git a/openslides/utils/collection.py b/openslides/utils/collection.py index a4ca92449..661007d00 100644 --- a/openslides/utils/collection.py +++ b/openslides/utils/collection.py @@ -481,6 +481,9 @@ class Collection: cache.set(self.get_cache_key(), ids) +_models_to_collection_string = {} + + def get_model_from_collection_string(collection_string): """ Returns a model class which belongs to the argument collection_string. @@ -493,20 +496,18 @@ def get_model_from_collection_string(collection_string): for model in app_config.get_models(): yield model - for model in model_generator(): - try: - model_collection_string = model.get_collection_string() - except AttributeError: - # Skip models which do not have the method get_collection_string. - pass - else: - if model_collection_string == collection_string: - # The model was found. - break - else: - # No model was found in all apps. - raise ValueError('Invalid message. A valid collection_string is missing.') - return model + # On the first run, generate the dict. It can not change at runtime. + if not _models_to_collection_string: + for model in model_generator(): + try: + get_collection_string = model.get_collection_string + except AttributeError: + # Skip models which do not have the method get_collection_string. + pass + else: + _models_to_collection_string[get_collection_string()] = model + + return _models_to_collection_string[collection_string] def get_single_element_cache_key(collection_string, id): diff --git a/tests/unit/utils/test_collection.py b/tests/unit/utils/test_collection.py index 9bb292e9b..065c7683b 100644 --- a/tests/unit/utils/test_collection.py +++ b/tests/unit/utils/test_collection.py @@ -58,7 +58,7 @@ class TestGetModelFromCollectionString(TestCase): self.assertEqual(projector_model, Projector) def test_unknown_app(self): - with self.assertRaises(ValueError): + with self.assertRaises(KeyError): collection.get_model_from_collection_string('invalid/model')