Update Gdrive integration (Error Handling on Callback and wording)

This commit is contained in:
Ozzie Isaacs 2020-07-21 18:01:38 +02:00
parent 93a0217d5f
commit 66acd1821d
20 changed files with 36 additions and 29 deletions

View File

@ -34,18 +34,17 @@ from flask import Blueprint, flash, request, redirect, url_for, abort
from flask_babel import gettext as _ from flask_babel import gettext as _
from flask_login import login_required from flask_login import login_required
try:
from googleapiclient.errors import HttpError
except ImportError:
pass
from . import logger, gdriveutils, config, ub, calibre_db from . import logger, gdriveutils, config, ub, calibre_db
from .web import admin_required from .web import admin_required
gdrive = Blueprint('gdrive', __name__) gdrive = Blueprint('gdrive', __name__)
log = logger.create() log = logger.create()
try:
from googleapiclient.errors import HttpError
except ImportError as err:
log.debug(("Cannot import googleapiclient, using gdrive will not work: %s", err))
current_milli_time = lambda: int(round(time() * 1000)) current_milli_time = lambda: int(round(time() * 1000))
gdrive_watch_callback_token = 'target=calibreweb-watch_files' gdrive_watch_callback_token = 'target=calibreweb-watch_files'
@ -73,7 +72,7 @@ def google_drive_callback():
credentials = gdriveutils.Gauth.Instance().auth.flow.step2_exchange(auth_code) credentials = gdriveutils.Gauth.Instance().auth.flow.step2_exchange(auth_code)
with open(gdriveutils.CREDENTIALS, 'w') as f: with open(gdriveutils.CREDENTIALS, 'w') as f:
f.write(credentials.to_json()) f.write(credentials.to_json())
except ValueError as error: except (ValueError, AttributeError) as error:
log.error(error) log.error(error)
return redirect(url_for('admin.configuration')) return redirect(url_for('admin.configuration'))
@ -94,7 +93,7 @@ def watch_gdrive():
try: try:
result = gdriveutils.watchChange(gdriveutils.Gdrive.Instance().drive, notification_id, result = gdriveutils.watchChange(gdriveutils.Gdrive.Instance().drive, notification_id,
'web_hook', address, gdrive_watch_callback_token, current_milli_time() + 604800*1000) 'web_hook', address, gdrive_watch_callback_token, current_milli_time() + 604800*1000)
config.config_google_drive_watch_changes_response = json.dumps(result) # config.config_google_drive_watch_changes_response = json.dumps(result)
# after save(), config_google_drive_watch_changes_response will be a json object, not string # after save(), config_google_drive_watch_changes_response will be a json object, not string
config.save() config.save()
except HttpError as e: except HttpError as e:
@ -118,7 +117,7 @@ def revoke_watch_gdrive():
last_watch_response['resourceId']) last_watch_response['resourceId'])
except HttpError: except HttpError:
pass pass
config.config_google_drive_watch_changes_response = None config.config_google_drive_watch_changes_response = {}
config.save() config.save()
return redirect(url_for('admin.configuration')) return redirect(url_for('admin.configuration'))

View File

@ -36,7 +36,9 @@ try:
from apiclient import errors from apiclient import errors
from httplib2 import ServerNotFoundError from httplib2 import ServerNotFoundError
gdrive_support = True gdrive_support = True
except ImportError: importError = None
except ImportError as err:
importError = err
gdrive_support = False gdrive_support = False
from . import logger, cli, config from . import logger, cli, config
@ -52,6 +54,8 @@ if gdrive_support:
logger.get('googleapiclient.discovery_cache').setLevel(logger.logging.ERROR) logger.get('googleapiclient.discovery_cache').setLevel(logger.logging.ERROR)
if not logger.is_debug_enabled(): if not logger.is_debug_enabled():
logger.get('googleapiclient.discovery').setLevel(logger.logging.ERROR) logger.get('googleapiclient.discovery').setLevel(logger.logging.ERROR)
else:
log.debug("Cannot import pydrive,httplib2, using gdrive will not work: %s", importError)
class Singleton: class Singleton:
@ -99,7 +103,11 @@ class Singleton:
@Singleton @Singleton
class Gauth: class Gauth:
def __init__(self): def __init__(self):
self.auth = GoogleAuth(settings_file=SETTINGS_YAML) try:
self.auth = GoogleAuth(settings_file=SETTINGS_YAML)
except NameError as error:
log.error(error)
self.auth = None
@Singleton @Singleton

View File

@ -41,7 +41,7 @@
</div> </div>
{% else %} {% else %}
{% if show_authenticate_google_drive and g.user.is_authenticated and not config.config_use_google_drive %} {% if show_authenticate_google_drive and g.user.is_authenticated and not config.config_use_google_drive %}
<div >{{_('Please hit submit to continue with setup')}}</div> <div >{{_('Please hit save to continue with setup')}}</div>
{% endif %} {% endif %}
{% if not g.user.is_authenticated %} {% if not g.user.is_authenticated %}
<div >{{_('Please finish Google Drive setup after login')}}</div> <div >{{_('Please finish Google Drive setup after login')}}</div>

View File

@ -1556,7 +1556,7 @@ msgid "Authenticate Google Drive"
msgstr "Ověřit Google Drive" msgstr "Ověřit Google Drive"
#: cps/templates/config_edit.html:44 #: cps/templates/config_edit.html:44
msgid "Please hit submit to continue with setup" msgid "Please hit save to continue with setup"
msgstr "Klikněte na odeslat pro pokračování v nastavení" msgstr "Klikněte na odeslat pro pokračování v nastavení"
#: cps/templates/config_edit.html:47 #: cps/templates/config_edit.html:47

View File

@ -1557,7 +1557,7 @@ msgid "Authenticate Google Drive"
msgstr "Google Drive authentifizieren" msgstr "Google Drive authentifizieren"
#: cps/templates/config_edit.html:44 #: cps/templates/config_edit.html:44
msgid "Please hit submit to continue with setup" msgid "Please hit save to continue with setup"
msgstr "Bitte auf Abschicken drücken, um mit dem Setup fortzufahren" msgstr "Bitte auf Abschicken drücken, um mit dem Setup fortzufahren"
#: cps/templates/config_edit.html:47 #: cps/templates/config_edit.html:47

View File

@ -1560,7 +1560,7 @@ msgid "Authenticate Google Drive"
msgstr "Autentificar Google Drive" msgstr "Autentificar Google Drive"
#: cps/templates/config_edit.html:44 #: cps/templates/config_edit.html:44
msgid "Please hit submit to continue with setup" msgid "Please hit save to continue with setup"
msgstr "Por favor, pulsa enviar para continuar con la configuración" msgstr "Por favor, pulsa enviar para continuar con la configuración"
#: cps/templates/config_edit.html:47 #: cps/templates/config_edit.html:47

View File

@ -1557,7 +1557,7 @@ msgid "Authenticate Google Drive"
msgstr "Autentikoi Google Drive" msgstr "Autentikoi Google Drive"
#: cps/templates/config_edit.html:44 #: cps/templates/config_edit.html:44
msgid "Please hit submit to continue with setup" msgid "Please hit save to continue with setup"
msgstr "Ole hyvä ja paina lähetä jatkaaksesi asennusta" msgstr "Ole hyvä ja paina lähetä jatkaaksesi asennusta"
#: cps/templates/config_edit.html:47 #: cps/templates/config_edit.html:47

View File

@ -1571,7 +1571,7 @@ msgid "Authenticate Google Drive"
msgstr "Authentification Google Drive" msgstr "Authentification Google Drive"
#: cps/templates/config_edit.html:44 #: cps/templates/config_edit.html:44
msgid "Please hit submit to continue with setup" msgid "Please hit save to continue with setup"
msgstr "Veuillez cliquer sur soumettre pour continuer linitialisation" msgstr "Veuillez cliquer sur soumettre pour continuer linitialisation"
#: cps/templates/config_edit.html:47 #: cps/templates/config_edit.html:47

View File

@ -1557,7 +1557,7 @@ msgid "Authenticate Google Drive"
msgstr "Google Drive hitelesítés" msgstr "Google Drive hitelesítés"
#: cps/templates/config_edit.html:44 #: cps/templates/config_edit.html:44
msgid "Please hit submit to continue with setup" msgid "Please hit save to continue with setup"
msgstr "A beállítás folytatásához kattints a Küldés gombra" msgstr "A beállítás folytatásához kattints a Küldés gombra"
#: cps/templates/config_edit.html:47 #: cps/templates/config_edit.html:47

View File

@ -1556,7 +1556,7 @@ msgid "Authenticate Google Drive"
msgstr "Autenticazione Google Drive" msgstr "Autenticazione Google Drive"
#: cps/templates/config_edit.html:44 #: cps/templates/config_edit.html:44
msgid "Please hit submit to continue with setup" msgid "Please hit save to continue with setup"
msgstr "Per favore premi invio per proseguire con la configurazione" msgstr "Per favore premi invio per proseguire con la configurazione"
#: cps/templates/config_edit.html:47 #: cps/templates/config_edit.html:47

View File

@ -1557,7 +1557,7 @@ msgid "Authenticate Google Drive"
msgstr "Googleドライブを認証" msgstr "Googleドライブを認証"
#: cps/templates/config_edit.html:44 #: cps/templates/config_edit.html:44
msgid "Please hit submit to continue with setup" msgid "Please hit save to continue with setup"
msgstr "決定を押して設定を続けてください" msgstr "決定を押して設定を続けてください"
#: cps/templates/config_edit.html:47 #: cps/templates/config_edit.html:47

View File

@ -1558,7 +1558,7 @@ msgid "Authenticate Google Drive"
msgstr "វាយបញ្ចូលគណនី Google Drive" msgstr "វាយបញ្ចូលគណនី Google Drive"
#: cps/templates/config_edit.html:44 #: cps/templates/config_edit.html:44
msgid "Please hit submit to continue with setup" msgid "Please hit save to continue with setup"
msgstr "" msgstr ""
#: cps/templates/config_edit.html:47 #: cps/templates/config_edit.html:47

View File

@ -1558,7 +1558,7 @@ msgid "Authenticate Google Drive"
msgstr "Google Drive goedkeuren" msgstr "Google Drive goedkeuren"
#: cps/templates/config_edit.html:44 #: cps/templates/config_edit.html:44
msgid "Please hit submit to continue with setup" msgid "Please hit save to continue with setup"
msgstr "Druk op 'Opslaan' om door te gaan met instellen" msgstr "Druk op 'Opslaan' om door te gaan met instellen"
#: cps/templates/config_edit.html:47 #: cps/templates/config_edit.html:47

View File

@ -1570,7 +1570,7 @@ msgid "Authenticate Google Drive"
msgstr "Uwierzytelnij Dysk Google" msgstr "Uwierzytelnij Dysk Google"
#: cps/templates/config_edit.html:44 #: cps/templates/config_edit.html:44
msgid "Please hit submit to continue with setup" msgid "Please hit save to continue with setup"
msgstr "Kliknij przycisk, aby kontynuować instalację" msgstr "Kliknij przycisk, aby kontynuować instalację"
#: cps/templates/config_edit.html:47 #: cps/templates/config_edit.html:47

View File

@ -1558,7 +1558,7 @@ msgid "Authenticate Google Drive"
msgstr "Аутентификация Google Drive" msgstr "Аутентификация Google Drive"
#: cps/templates/config_edit.html:44 #: cps/templates/config_edit.html:44
msgid "Please hit submit to continue with setup" msgid "Please hit save to continue with setup"
msgstr "Пожалуйста, нажмите «Отправить», чтобы продолжить настройку" msgstr "Пожалуйста, нажмите «Отправить», чтобы продолжить настройку"
#: cps/templates/config_edit.html:47 #: cps/templates/config_edit.html:47

View File

@ -1557,7 +1557,7 @@ msgid "Authenticate Google Drive"
msgstr "Autentisera Google Drive" msgstr "Autentisera Google Drive"
#: cps/templates/config_edit.html:44 #: cps/templates/config_edit.html:44
msgid "Please hit submit to continue with setup" msgid "Please hit save to continue with setup"
msgstr "Klicka på skicka för att fortsätta med installationen" msgstr "Klicka på skicka för att fortsätta med installationen"
#: cps/templates/config_edit.html:47 #: cps/templates/config_edit.html:47

View File

@ -1557,7 +1557,7 @@ msgid "Authenticate Google Drive"
msgstr "Google Drive Doğrula" msgstr "Google Drive Doğrula"
#: cps/templates/config_edit.html:44 #: cps/templates/config_edit.html:44
msgid "Please hit submit to continue with setup" msgid "Please hit save to continue with setup"
msgstr "Kuruluma devam etmek için Gönder'e tıklayın" msgstr "Kuruluma devam etmek için Gönder'e tıklayın"
#: cps/templates/config_edit.html:47 #: cps/templates/config_edit.html:47

View File

@ -1556,7 +1556,7 @@ msgid "Authenticate Google Drive"
msgstr "Автентифікація Google Drive" msgstr "Автентифікація Google Drive"
#: cps/templates/config_edit.html:44 #: cps/templates/config_edit.html:44
msgid "Please hit submit to continue with setup" msgid "Please hit save to continue with setup"
msgstr "" msgstr ""
#: cps/templates/config_edit.html:47 #: cps/templates/config_edit.html:47

View File

@ -1557,7 +1557,7 @@ msgid "Authenticate Google Drive"
msgstr "认证 Google Drive" msgstr "认证 Google Drive"
#: cps/templates/config_edit.html:44 #: cps/templates/config_edit.html:44
msgid "Please hit submit to continue with setup" msgid "Please hit save to continue with setup"
msgstr "请点击提交以继续设置" msgstr "请点击提交以继续设置"
#: cps/templates/config_edit.html:47 #: cps/templates/config_edit.html:47

View File

@ -1556,7 +1556,7 @@ msgid "Authenticate Google Drive"
msgstr "" msgstr ""
#: cps/templates/config_edit.html:44 #: cps/templates/config_edit.html:44
msgid "Please hit submit to continue with setup" msgid "Please hit save to continue with setup"
msgstr "" msgstr ""
#: cps/templates/config_edit.html:47 #: cps/templates/config_edit.html:47