From d2617322c6b1214e261cf013102c41ed45885f41 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Tue, 27 Oct 2020 19:39:11 +0100 Subject: [PATCH] Negate a condition - removes two levels of indentation - makes it clear that if the wrong tokens are provided nothing will happen - remove a useless nested function --- cps/gdrive.py | 62 +++++++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/cps/gdrive.py b/cps/gdrive.py index 74a45061..584c90cf 100644 --- a/cps/gdrive.py +++ b/cps/gdrive.py @@ -123,38 +123,36 @@ def revoke_watch_gdrive(): @gdrive.route("/gdrive/watch/callback", methods=['GET', 'POST']) def on_received_watch_confirmation(): + if request.headers.get('X-Goog-Channel-Token') != gdrive_watch_callback_token \ + or request.headers.get('X-Goog-Resource-State') != 'change' \ + or not request.data: + return redirect(url_for('admin.configuration')) + log.debug('%r', request.headers) - if request.headers.get('X-Goog-Channel-Token') == gdrive_watch_callback_token \ - and request.headers.get('X-Goog-Resource-State') == 'change' \ - and request.data: + log.debug('%r', request.data) + log.info('Change received from gdrive') - data = request.data - - def updateMetaData(): - log.info('Change received from gdrive') - log.debug('%r', data) - try: - j = json.loads(data) - log.info('Getting change details') - response = gdriveutils.getChangeById(gdriveutils.Gdrive.Instance().drive, j['id']) - log.debug('%r', response) - if response: - if sys.version_info < (3, 0): - dbpath = os.path.join(config.config_calibre_dir, "metadata.db") - else: - dbpath = os.path.join(config.config_calibre_dir, "metadata.db").encode() - if not response['deleted'] and response['file']['title'] == 'metadata.db' \ - and response['file']['md5Checksum'] != hashlib.md5(dbpath): - tmpDir = tempfile.gettempdir() - log.info('Database file updated') - copyfile(dbpath, os.path.join(tmpDir, "metadata.db_" + str(current_milli_time()))) - log.info('Backing up existing and downloading updated metadata.db') - gdriveutils.downloadFile(None, "metadata.db", os.path.join(tmpDir, "tmp_metadata.db")) - log.info('Setting up new DB') - # prevent error on windows, as os.rename does on exisiting files - move(os.path.join(tmpDir, "tmp_metadata.db"), dbpath) - calibre_db.reconnect_db(config, ub.app_DB_path) - except Exception as e: - log.exception(e) - updateMetaData() + try: + j = json.loads(request.data) + log.info('Getting change details') + response = gdriveutils.getChangeById(gdriveutils.Gdrive.Instance().drive, j['id']) + log.debug('%r', response) + if response: + if sys.version_info < (3, 0): + dbpath = os.path.join(config.config_calibre_dir, "metadata.db") + else: + dbpath = os.path.join(config.config_calibre_dir, "metadata.db").encode() + if not response['deleted'] and response['file']['title'] == 'metadata.db' \ + and response['file']['md5Checksum'] != hashlib.md5(dbpath): + tmpDir = tempfile.gettempdir() + log.info('Database file updated') + copyfile(dbpath, os.path.join(tmpDir, "metadata.db_" + str(current_milli_time()))) + log.info('Backing up existing and downloading updated metadata.db') + gdriveutils.downloadFile(None, "metadata.db", os.path.join(tmpDir, "tmp_metadata.db")) + log.info('Setting up new DB') + # prevent error on windows, as os.rename does on exisiting files + move(os.path.join(tmpDir, "tmp_metadata.db"), dbpath) + calibre_db.reconnect_db(config, ub.app_DB_path) + except Exception as e: + log.exception(e) return ''