From 32dcc6482f66938132776f3749459a36bd714c0d Mon Sep 17 00:00:00 2001 From: Khanh Ngo Date: Thu, 23 Aug 2018 09:23:21 +0700 Subject: [PATCH] Fix db migration issue --- app/__init__.py | 6 +----- app/oauth.py | 1 - app/views.py | 23 +++++++++++++++++------ 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 37c25ae..b2425ce 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -37,8 +37,4 @@ if app.config.get('SAML_ENABLED') and app.config.get('SAML_ENCRYPT'): certutil.create_self_signed_cert() from app import models - -try: - from app import views -except: - logging.error("You have not initialized the DB yet or DB migration is running...") +from app import views diff --git a/app/oauth.py b/app/oauth.py index 9c01b8b..17f30a0 100644 --- a/app/oauth.py +++ b/app/oauth.py @@ -7,7 +7,6 @@ from app.models import Setting # TODO: # - Replace Flask-OAuthlib by authlib -# - Fix flask-migrate issue with calling Setting() class during downgrade / upgrade # - Fix github/google enabling (Currently need to reload the flask app) def github_oauth(): diff --git a/app/views.py b/app/views.py index bd11139..9d62c1e 100644 --- a/app/views.py +++ b/app/views.py @@ -28,9 +28,10 @@ if app.config['SAML_ENABLED']: from onelogin.saml2.auth import OneLogin_Saml2_Auth from onelogin.saml2.utils import OneLogin_Saml2_Utils +google = None +github = None logging = logger.getLogger(__name__) -google = google_oauth() -github = github_oauth() + # FILTERS app.jinja_env.filters['display_record_name'] = utils.display_record_name @@ -50,6 +51,14 @@ def inject_setting(): return dict(SETTING=setting) +@app.before_first_request +def register_modules(): + global google + global github + google = google_oauth() + github = github_oauth() + + # START USER AUTHENTICATION HANDLER @app.before_request def before_request(): @@ -143,7 +152,8 @@ def register(): @app.route('/google/login') def google_login(): - if not Setting().get('google_oauth_enabled'): + if not Setting().get('google_oauth_enabled') or google is None: + logging.error('Google OAuth is disabled or you have not yet reloaded the pda application after enabling.') return abort(400) else: return google.authorize(callback=url_for('google_authorized', _external=True)) @@ -151,7 +161,8 @@ def google_login(): @app.route('/github/login') def github_login(): - if not Setting().get('github_oauth_enabled'): + if not Setting().get('github_oauth_enabled') or github is None: + logging.error('Github OAuth is disabled or you have not yet reloaded the pda application after enabling.') return abort(400) else: return github.authorize(callback=url_for('github_authorized', _external=True)) @@ -1445,7 +1456,7 @@ def admin_setting_authentication(): Setting().set('google_token_params', request.form.get('google_token_params')) Setting().set('google_authorize_url', request.form.get('google_authorize_url')) Setting().set('google_base_url', request.form.get('google_base_url')) - result = {'status': True, 'msg': 'Saved successfully'} + result = {'status': True, 'msg': 'Saved successfully. Please reload PDA to take effect.'} elif conf_type == 'github': Setting().set('github_oauth_enabled', True if request.form.get('github_oauth_enabled') else False) Setting().set('github_oauth_key', request.form.get('github_oauth_key')) @@ -1454,7 +1465,7 @@ def admin_setting_authentication(): Setting().set('github_oauth_api_url', request.form.get('github_oauth_api_url')) Setting().set('github_oauth_token_url', request.form.get('github_oauth_token_url')) Setting().set('github_oauth_authorize_url', request.form.get('github_oauth_authorize_url')) - result = {'status': True, 'msg': 'Saved successfully'} + result = {'status': True, 'msg': 'Saved successfully. Please reload PDA to take effect.'} else: return abort(400)