From 66acd1821d79da64ea14d13df48d4284fc31bb10 Mon Sep 17 00:00:00 2001
From: Ozzie Isaacs <ozzie.fernandez.isaccs@googlemail.com>
Date: Tue, 21 Jul 2020 18:01:38 +0200
Subject: [PATCH] Update Gdrive integration (Error Handling on Callback and
 wording)

---
 cps/gdrive.py                                   | 17 ++++++++---------
 cps/gdriveutils.py                              | 12 ++++++++++--
 cps/templates/config_edit.html                  |  2 +-
 cps/translations/cs/LC_MESSAGES/messages.po     |  2 +-
 cps/translations/de/LC_MESSAGES/messages.po     |  2 +-
 cps/translations/es/LC_MESSAGES/messages.po     |  2 +-
 cps/translations/fi/LC_MESSAGES/messages.po     |  2 +-
 cps/translations/fr/LC_MESSAGES/messages.po     |  2 +-
 cps/translations/hu/LC_MESSAGES/messages.po     |  2 +-
 cps/translations/it/LC_MESSAGES/messages.po     |  2 +-
 cps/translations/ja/LC_MESSAGES/messages.po     |  2 +-
 cps/translations/km/LC_MESSAGES/messages.po     |  2 +-
 cps/translations/nl/LC_MESSAGES/messages.po     |  2 +-
 cps/translations/pl/LC_MESSAGES/messages.po     |  2 +-
 cps/translations/ru/LC_MESSAGES/messages.po     |  2 +-
 cps/translations/sv/LC_MESSAGES/messages.po     |  2 +-
 cps/translations/tr/LC_MESSAGES/messages.po     |  2 +-
 cps/translations/uk/LC_MESSAGES/messages.po     |  2 +-
 .../zh_Hans_CN/LC_MESSAGES/messages.po          |  2 +-
 messages.pot                                    |  2 +-
 20 files changed, 36 insertions(+), 29 deletions(-)

diff --git a/cps/gdrive.py b/cps/gdrive.py
index aa3743d2..466097c9 100644
--- a/cps/gdrive.py
+++ b/cps/gdrive.py
@@ -34,18 +34,17 @@ from flask import Blueprint, flash, request, redirect, url_for, abort
 from flask_babel import gettext as _
 from flask_login import login_required
 
-try:
-    from googleapiclient.errors import HttpError
-except ImportError:
-    pass
-
 from . import logger, gdriveutils, config, ub, calibre_db
 from .web import admin_required
 
-
 gdrive = Blueprint('gdrive', __name__)
 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))
 
 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)
         with open(gdriveutils.CREDENTIALS, 'w') as f:
             f.write(credentials.to_json())
-    except ValueError as error:
+    except (ValueError, AttributeError) as error:
         log.error(error)
     return redirect(url_for('admin.configuration'))
 
@@ -94,7 +93,7 @@ def watch_gdrive():
         try:
             result = gdriveutils.watchChange(gdriveutils.Gdrive.Instance().drive, notification_id,
                                '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
             config.save()
         except HttpError as e:
@@ -118,7 +117,7 @@ def revoke_watch_gdrive():
                                     last_watch_response['resourceId'])
         except HttpError:
             pass
-        config.config_google_drive_watch_changes_response = None
+        config.config_google_drive_watch_changes_response = {}
         config.save()
     return redirect(url_for('admin.configuration'))
 
diff --git a/cps/gdriveutils.py b/cps/gdriveutils.py
index 9ea0479d..683605ff 100644
--- a/cps/gdriveutils.py
+++ b/cps/gdriveutils.py
@@ -36,7 +36,9 @@ try:
     from apiclient import errors
     from httplib2 import ServerNotFoundError
     gdrive_support = True
-except ImportError:
+    importError = None
+except ImportError as err:
+    importError = err
     gdrive_support = False
 
 from . import logger, cli, config
@@ -52,6 +54,8 @@ if gdrive_support:
     logger.get('googleapiclient.discovery_cache').setLevel(logger.logging.ERROR)
     if not logger.is_debug_enabled():
         logger.get('googleapiclient.discovery').setLevel(logger.logging.ERROR)
+else:
+    log.debug("Cannot import pydrive,httplib2, using gdrive will not work: %s", importError)
 
 
 class Singleton:
@@ -99,7 +103,11 @@ class Singleton:
 @Singleton
 class Gauth:
     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
diff --git a/cps/templates/config_edit.html b/cps/templates/config_edit.html
index 77a60c1b..6cb95764 100644
--- a/cps/templates/config_edit.html
+++ b/cps/templates/config_edit.html
@@ -41,7 +41,7 @@
           </div>
         {% else %}
             {% 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 %}
           {% if not g.user.is_authenticated %}
           <div >{{_('Please finish Google Drive setup after login')}}</div>
diff --git a/cps/translations/cs/LC_MESSAGES/messages.po b/cps/translations/cs/LC_MESSAGES/messages.po
index 6ef881ed..3dca9fe8 100644
--- a/cps/translations/cs/LC_MESSAGES/messages.po
+++ b/cps/translations/cs/LC_MESSAGES/messages.po
@@ -1556,7 +1556,7 @@ msgid "Authenticate Google Drive"
 msgstr "Ověřit Google Drive"
 
 #: 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í"
 
 #: cps/templates/config_edit.html:47
diff --git a/cps/translations/de/LC_MESSAGES/messages.po b/cps/translations/de/LC_MESSAGES/messages.po
index a1372adb..4d9725e5 100644
--- a/cps/translations/de/LC_MESSAGES/messages.po
+++ b/cps/translations/de/LC_MESSAGES/messages.po
@@ -1557,7 +1557,7 @@ msgid "Authenticate Google Drive"
 msgstr "Google Drive authentifizieren"
 
 #: 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"
 
 #: cps/templates/config_edit.html:47
diff --git a/cps/translations/es/LC_MESSAGES/messages.po b/cps/translations/es/LC_MESSAGES/messages.po
index 99bac86e..4bd142ca 100644
--- a/cps/translations/es/LC_MESSAGES/messages.po
+++ b/cps/translations/es/LC_MESSAGES/messages.po
@@ -1560,7 +1560,7 @@ msgid "Authenticate Google Drive"
 msgstr "Autentificar Google Drive"
 
 #: 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"
 
 #: cps/templates/config_edit.html:47
diff --git a/cps/translations/fi/LC_MESSAGES/messages.po b/cps/translations/fi/LC_MESSAGES/messages.po
index 09b133f1..26d644fb 100644
--- a/cps/translations/fi/LC_MESSAGES/messages.po
+++ b/cps/translations/fi/LC_MESSAGES/messages.po
@@ -1557,7 +1557,7 @@ msgid "Authenticate Google Drive"
 msgstr "Autentikoi Google Drive"
 
 #: 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"
 
 #: cps/templates/config_edit.html:47
diff --git a/cps/translations/fr/LC_MESSAGES/messages.po b/cps/translations/fr/LC_MESSAGES/messages.po
index 6860cc52..5ba89d85 100644
--- a/cps/translations/fr/LC_MESSAGES/messages.po
+++ b/cps/translations/fr/LC_MESSAGES/messages.po
@@ -1571,7 +1571,7 @@ msgid "Authenticate Google Drive"
 msgstr "Authentification Google Drive"
 
 #: 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 l’initialisation"
 
 #: cps/templates/config_edit.html:47
diff --git a/cps/translations/hu/LC_MESSAGES/messages.po b/cps/translations/hu/LC_MESSAGES/messages.po
index 0b0f71b4..4550161a 100644
--- a/cps/translations/hu/LC_MESSAGES/messages.po
+++ b/cps/translations/hu/LC_MESSAGES/messages.po
@@ -1557,7 +1557,7 @@ msgid "Authenticate Google Drive"
 msgstr "Google Drive hitelesítés"
 
 #: 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"
 
 #: cps/templates/config_edit.html:47
diff --git a/cps/translations/it/LC_MESSAGES/messages.po b/cps/translations/it/LC_MESSAGES/messages.po
index 8c544937..1e069ae6 100644
--- a/cps/translations/it/LC_MESSAGES/messages.po
+++ b/cps/translations/it/LC_MESSAGES/messages.po
@@ -1556,7 +1556,7 @@ msgid "Authenticate Google Drive"
 msgstr "Autenticazione Google Drive"
 
 #: 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"
 
 #: cps/templates/config_edit.html:47
diff --git a/cps/translations/ja/LC_MESSAGES/messages.po b/cps/translations/ja/LC_MESSAGES/messages.po
index 4ae537dc..01b8e37f 100644
--- a/cps/translations/ja/LC_MESSAGES/messages.po
+++ b/cps/translations/ja/LC_MESSAGES/messages.po
@@ -1557,7 +1557,7 @@ msgid "Authenticate Google Drive"
 msgstr "Googleドライブを認証"
 
 #: cps/templates/config_edit.html:44
-msgid "Please hit submit to continue with setup"
+msgid "Please hit save to continue with setup"
 msgstr "決定を押して設定を続けてください"
 
 #: cps/templates/config_edit.html:47
diff --git a/cps/translations/km/LC_MESSAGES/messages.po b/cps/translations/km/LC_MESSAGES/messages.po
index 10d2d6af..e8def12b 100644
--- a/cps/translations/km/LC_MESSAGES/messages.po
+++ b/cps/translations/km/LC_MESSAGES/messages.po
@@ -1558,7 +1558,7 @@ msgid "Authenticate Google Drive"
 msgstr "វាយបញ្ចូលគណនី Google Drive"
 
 #: cps/templates/config_edit.html:44
-msgid "Please hit submit to continue with setup"
+msgid "Please hit save to continue with setup"
 msgstr ""
 
 #: cps/templates/config_edit.html:47
diff --git a/cps/translations/nl/LC_MESSAGES/messages.po b/cps/translations/nl/LC_MESSAGES/messages.po
index 4ce11eb2..eda57375 100644
--- a/cps/translations/nl/LC_MESSAGES/messages.po
+++ b/cps/translations/nl/LC_MESSAGES/messages.po
@@ -1558,7 +1558,7 @@ msgid "Authenticate Google Drive"
 msgstr "Google Drive goedkeuren"
 
 #: 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"
 
 #: cps/templates/config_edit.html:47
diff --git a/cps/translations/pl/LC_MESSAGES/messages.po b/cps/translations/pl/LC_MESSAGES/messages.po
index e6dc6b45..8718b9b6 100644
--- a/cps/translations/pl/LC_MESSAGES/messages.po
+++ b/cps/translations/pl/LC_MESSAGES/messages.po
@@ -1570,7 +1570,7 @@ msgid "Authenticate Google Drive"
 msgstr "Uwierzytelnij Dysk Google"
 
 #: 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ę"
 
 #: cps/templates/config_edit.html:47
diff --git a/cps/translations/ru/LC_MESSAGES/messages.po b/cps/translations/ru/LC_MESSAGES/messages.po
index 8b265dab..403e0c94 100644
--- a/cps/translations/ru/LC_MESSAGES/messages.po
+++ b/cps/translations/ru/LC_MESSAGES/messages.po
@@ -1558,7 +1558,7 @@ msgid "Authenticate Google Drive"
 msgstr "Аутентификация Google Drive"
 
 #: cps/templates/config_edit.html:44
-msgid "Please hit submit to continue with setup"
+msgid "Please hit save to continue with setup"
 msgstr "Пожалуйста, нажмите «Отправить», чтобы продолжить настройку"
 
 #: cps/templates/config_edit.html:47
diff --git a/cps/translations/sv/LC_MESSAGES/messages.po b/cps/translations/sv/LC_MESSAGES/messages.po
index b41fd929..ceb839d4 100644
--- a/cps/translations/sv/LC_MESSAGES/messages.po
+++ b/cps/translations/sv/LC_MESSAGES/messages.po
@@ -1557,7 +1557,7 @@ msgid "Authenticate Google Drive"
 msgstr "Autentisera Google Drive"
 
 #: 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"
 
 #: cps/templates/config_edit.html:47
diff --git a/cps/translations/tr/LC_MESSAGES/messages.po b/cps/translations/tr/LC_MESSAGES/messages.po
index b6ee8906..4abd7bc2 100644
--- a/cps/translations/tr/LC_MESSAGES/messages.po
+++ b/cps/translations/tr/LC_MESSAGES/messages.po
@@ -1557,7 +1557,7 @@ msgid "Authenticate Google Drive"
 msgstr "Google Drive Doğrula"
 
 #: 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"
 
 #: cps/templates/config_edit.html:47
diff --git a/cps/translations/uk/LC_MESSAGES/messages.po b/cps/translations/uk/LC_MESSAGES/messages.po
index 41c448b2..d7dc9af3 100644
--- a/cps/translations/uk/LC_MESSAGES/messages.po
+++ b/cps/translations/uk/LC_MESSAGES/messages.po
@@ -1556,7 +1556,7 @@ msgid "Authenticate Google Drive"
 msgstr "Автентифікація Google Drive"
 
 #: cps/templates/config_edit.html:44
-msgid "Please hit submit to continue with setup"
+msgid "Please hit save to continue with setup"
 msgstr ""
 
 #: cps/templates/config_edit.html:47
diff --git a/cps/translations/zh_Hans_CN/LC_MESSAGES/messages.po b/cps/translations/zh_Hans_CN/LC_MESSAGES/messages.po
index 76b99287..759263c8 100644
--- a/cps/translations/zh_Hans_CN/LC_MESSAGES/messages.po
+++ b/cps/translations/zh_Hans_CN/LC_MESSAGES/messages.po
@@ -1557,7 +1557,7 @@ msgid "Authenticate Google Drive"
 msgstr "认证 Google Drive"
 
 #: cps/templates/config_edit.html:44
-msgid "Please hit submit to continue with setup"
+msgid "Please hit save to continue with setup"
 msgstr "请点击提交以继续设置"
 
 #: cps/templates/config_edit.html:47
diff --git a/messages.pot b/messages.pot
index 8b630b6c..078f1669 100644
--- a/messages.pot
+++ b/messages.pot
@@ -1556,7 +1556,7 @@ msgid "Authenticate Google Drive"
 msgstr ""
 
 #: cps/templates/config_edit.html:44
-msgid "Please hit submit to continue with setup"
+msgid "Please hit save to continue with setup"
 msgstr ""
 
 #: cps/templates/config_edit.html:47