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. # 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) 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 # Collect all elements that shoud be send to the client when the websocket
# connection is established # connection is established
output = [] output = []
@ -74,8 +77,6 @@ def ws_add_site(message):
# Send all data. If there is no data, then only accept the connection # Send all data. If there is no data, then only accept the connection
if output: if output:
send_or_wait(message.reply_channel.send, {'text': json.dumps(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 @channel_session_user

View File

@ -481,6 +481,9 @@ class Collection:
cache.set(self.get_cache_key(), ids) cache.set(self.get_cache_key(), ids)
_models_to_collection_string = {}
def get_model_from_collection_string(collection_string): def get_model_from_collection_string(collection_string):
""" """
Returns a model class which belongs to the argument 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(): for model in app_config.get_models():
yield model yield model
for model in model_generator(): # On the first run, generate the dict. It can not change at runtime.
try: if not _models_to_collection_string:
model_collection_string = model.get_collection_string() for model in model_generator():
except AttributeError: try:
# Skip models which do not have the method get_collection_string. get_collection_string = model.get_collection_string
pass except AttributeError:
else: # Skip models which do not have the method get_collection_string.
if model_collection_string == collection_string: pass
# The model was found. else:
break _models_to_collection_string[get_collection_string()] = model
else:
# No model was found in all apps. return _models_to_collection_string[collection_string]
raise ValueError('Invalid message. A valid collection_string is missing.')
return model
def get_single_element_cache_key(collection_string, id): def get_single_element_cache_key(collection_string, id):

View File

@ -58,7 +58,7 @@ class TestGetModelFromCollectionString(TestCase):
self.assertEqual(projector_model, Projector) self.assertEqual(projector_model, Projector)
def test_unknown_app(self): def test_unknown_app(self):
with self.assertRaises(ValueError): with self.assertRaises(KeyError):
collection.get_model_from_collection_string('invalid/model') collection.get_model_from_collection_string('invalid/model')