Open websocket connections immediately

Also made a performance boost to get_model_from_collection_string()
This commit is contained in:
Oskar Hahn 2017-04-25 14:56:49 +02:00 committed by Emanuel Schütze
parent e1a95588e7
commit 74c1cc63dc
3 changed files with 19 additions and 17 deletions

View File

@ -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

View File

@ -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
# 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:
model_collection_string = model.get_collection_string()
get_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
_models_to_collection_string[get_collection_string()] = model
return _models_to_collection_string[collection_string]
def get_single_element_cache_key(collection_string, id):

View File

@ -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')