From 00341f8a50d095dd463e99db23a5d2238aa3dc68 Mon Sep 17 00:00:00 2001 From: Adarnof Date: Tue, 25 Oct 2016 21:59:58 +0000 Subject: [PATCH] Delay new Discourse user group sync to allow request to complete. Ensures Discourse creates user model to avoid 404s when calling API. --- services/managers/discourse_manager.py | 6 ++++-- services/views.py | 7 ++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/services/managers/discourse_manager.py b/services/managers/discourse_manager.py index 060e345e..830fbaf9 100644 --- a/services/managers/discourse_manager.py +++ b/services/managers/discourse_manager.py @@ -156,7 +156,9 @@ class DiscourseManager: } silent = kwargs.pop('silent', False) if args: - endpoint['path'] = endpoint['path'] % args + endpoint['parsed_url'] = endpoint['path'] % args + else: + endpoint['parsed_url'] = endpoint['path'] data = {} for arg in endpoint['args']['required']: data[arg] = kwargs[arg] @@ -166,7 +168,7 @@ class DiscourseManager: for arg in kwargs: if arg not in endpoint['args']['required'] and arg not in endpoint['args']['optional'] and not silent: logger.warn("Received unrecognized kwarg %s for endpoint %s" % (arg, endpoint)) - r = endpoint['method'](settings.DISCOURSE_URL + endpoint['path'], params=params, json=data) + r = endpoint['method'](settings.DISCOURSE_URL + endpoint['parsed_url'], params=params, json=data) try: if 'errors' in r.json() and not silent: logger.error("Discourse execution failed.\nEndpoint: %s\nErrors: %s" % (endpoint, r.json()['errors'])) diff --git a/services/views.py b/services/views.py index 79af39d7..adb1ab89 100755 --- a/services/views.py +++ b/services/views.py @@ -1199,9 +1199,10 @@ def discourse_sso(request): ## Record activation and queue group sync - auth.discourse_enabled = True - auth.save() - update_discourse_groups.delay(request.user.pk) + if not auth.discourse_enabled: + auth.discourse_enabled = True + auth.save() + update_discourse_groups.apply_async(args=[request.user.pk], countdown=30) # wait 30s for new user creation on Discourse ## Redirect back to Discourse