From 2ca7c596e3f0063a8efe07e4e2a735d56e5e70bc Mon Sep 17 00:00:00 2001 From: flan Date: Fri, 3 Nov 2017 01:46:38 +0100 Subject: [PATCH] Run msync handlers the same way as sync ones --- ankisyncd/sync_app.py | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/ankisyncd/sync_app.py b/ankisyncd/sync_app.py index 9e9f645..501a3a1 100644 --- a/ankisyncd/sync_app.py +++ b/ankisyncd/sync_app.py @@ -542,17 +542,7 @@ class SyncApp(object): if self.hook_pre_sync is not None: thread.execute(self.hook_pre_sync, [session]) - # Create a closure to run this operation inside of the thread allocated to this collection - def runFunc(col): - handler = session.get_handler_for_operation(url, col) - func = getattr(handler, url) - result = func(**data) - col.save() - return result - runFunc.func_name = url - - # Send to the thread to execute - result = thread.execute(runFunc) + result = self._execute_handler_method_in_thread(url, data, session) # If it's a complex data type, we convert it to JSON if type(result) not in (str, unicode): @@ -567,10 +557,7 @@ class SyncApp(object): if self.hook_post_sync is not None: thread.execute(self.hook_post_sync, [session]) - return Response( - status='200 OK', - content_type='application/json', - body=result) + return result elif url == 'upload': thread = session.get_thread() @@ -604,9 +591,15 @@ class SyncApp(object): if url == "begin": data['skey'] = session.skey - return self._execute_handler_method_in_thread(url, data, session) + result = self._execute_handler_method_in_thread(url, data, session) - return Response(status='200 OK', content_type='text/plain', body='Anki Sync Server') + # If it's a complex data type, we convert it to JSON + if type(result) not in (str, unicode): + result = json.dumps(result) + + return result + + return "Anki Sync Server" @staticmethod def _execute_handler_method_in_thread(method_name, keyword_args, session):