From f477d48c7c715defdb565c24df525b710e70e200 Mon Sep 17 00:00:00 2001 From: Virgil Grigoras Date: Sun, 9 Sep 2018 11:46:00 +0200 Subject: [PATCH 01/11] More robust handling while checking for new updates --- cps/static/js/main.js | 6 ++++ cps/templates/admin.html | 1 + cps/web.py | 61 +++++++++++++++++++++++++++++++--------- 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/cps/static/js/main.js b/cps/static/js/main.js index 524abdb0..3fdd42ae 100644 --- a/cps/static/js/main.js +++ b/cps/static/js/main.js @@ -104,6 +104,7 @@ $(function() { var $this = $(this); var buttonText = $this.html(); $this.html("..."); + $("#update_error").addClass("hidden") $.ajax({ dataType: "json", url: window.location.pathname + "/../../get_update_status", @@ -116,6 +117,11 @@ $(function() { .removeClass("hidden") .find("span").html(data.commit); } + if (data.error.length != 0) { + $("#update_error") + .removeClass("hidden") + .find("span").html(data.error); + } } }); }); diff --git a/cps/templates/admin.html b/cps/templates/admin.html index 81ff05c5..78773cac 100644 --- a/cps/templates/admin.html +++ b/cps/templates/admin.html @@ -93,6 +93,7 @@

{{_('Administration')}}

{{_('Current commit timestamp')}}: {{commit}}
+

{{_('Reconnect to Calibre DB')}}
{{_('Restart Calibre-Web')}}
diff --git a/cps/web.py b/cps/web.py index 4ba2c35b..05fd8835 100644 --- a/cps/web.py +++ b/cps/web.py @@ -1082,23 +1082,58 @@ def get_matching_tags(): @app.route("/get_update_status", methods=['GET']) @login_required_if_no_ano def get_update_status(): - status = {} + status = { + 'status': False + } + repository_url = 'https://api.github.com/repos/janeczku/calibre-web' tz = datetime.timedelta(seconds=time.timezone if (time.localtime().tm_isdst == 0) else time.altzone) + if request.method == "GET": # should be automatically replaced by git with current commit hash - commit_id = '$Format:%H$' - # ToDo: Handle server not reachable -> ValueError: - commit = requests.get('https://api.github.com/repos/janeczku/calibre-web/git/refs/heads/master').json() - if "object" in commit and commit['object']['sha'] != commit_id: - status['status'] = True - commitdate = requests.get('https://api.github.com/repos/janeczku/calibre-web/git/commits/'+commit['object']['sha']).json() - if "committer" in commitdate: - form_date=datetime.datetime.strptime(commitdate['committer']['date'],"%Y-%m-%dT%H:%M:%SZ") - tz - status['commit'] = format_datetime(form_date, format='short', locale=get_locale()) + current_commit_id = '$Format:%H$' + + try: + r = requests.get(repository_url + '/git/refs/heads/master') + r.raise_for_status() + commit = r.json() + except requests.exceptions.HTTPError as ex: + status['error'] = _(u'HTTP Error') + ' ' + str(ex) + except requests.exceptions.ConnectionError: + status['error'] = _(u'Connection error') + except requests.exceptions.Timeout: + status['error'] = _(u'Timeout while establishing connection') + except requests.exceptions.RequestException: + status['error'] = _(u'General error') + + if 'error' in status: + return json.dumps(status) + + if 'object' in commit and commit['object']['sha'] != current_commit_id: + # a new update is available + try: + r = requests.get(repository_url + '/git/commits/' + commit['object']['sha']) + r.raise_for_status() + update_data = r.json() + except requests.exceptions.HTTPError as ex: + status['error'] = _(u'HTTP Error') + ' ' + str(ex) + except requests.exceptions.ConnectionError: + status['error'] = _(u'Connection error') + except requests.exceptions.Timeout: + status['error'] = _(u'Timeout while establishing connection') + except requests.exceptions.RequestException: + status['error'] = _(u'General error') + + if 'error' in status: + return json.dumps(status) + + if 'committer' in update_data: + status['status'] = True + new_commit_date = datetime.datetime.strptime( + update_data['committer']['date'], '%Y-%m-%dT%H:%M:%SZ') - tz + status['commit'] = format_datetime(new_commit_date, format='short', locale=get_locale()) else: - status['commit'] = u'Unknown' - else: - status['status'] = False + status['error'] = _(u'Could not fetch update information') + return json.dumps(status) From 6e2dbb7cd6a817a52ffa763434858b80f0e92d1e Mon Sep 17 00:00:00 2001 From: Virgil Grigoras Date: Sun, 9 Sep 2018 13:00:46 +0200 Subject: [PATCH 02/11] move current commit hash to its own file --- cps/helper.py | 8 ++++++++ cps/web.py | 17 +++++++++++++---- version | 1 + 3 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 version diff --git a/cps/helper.py b/cps/helper.py index d13496bc..1fcbbc9c 100755 --- a/cps/helper.py +++ b/cps/helper.py @@ -547,3 +547,11 @@ def check_unrar(unrarLocation): error=True return (error, version) +def is_sha1(sha1): + if len(sha1) != 40: + return False + try: + temp = int(sha1, 16) + except ValueError: + return False + return True diff --git a/cps/web.py b/cps/web.py index 05fd8835..07791daf 100644 --- a/cps/web.py +++ b/cps/web.py @@ -1,5 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +from builtins import OSError + try: from googleapiclient.errors import HttpError except ImportError: @@ -1083,14 +1085,21 @@ def get_matching_tags(): @login_required_if_no_ano def get_update_status(): status = { - 'status': False + 'status': False, + 'current_commit_hash': '' } repository_url = 'https://api.github.com/repos/janeczku/calibre-web' tz = datetime.timedelta(seconds=time.timezone if (time.localtime().tm_isdst == 0) else time.altzone) if request.method == "GET": - # should be automatically replaced by git with current commit hash - current_commit_id = '$Format:%H$' + # get current commit hash from file + try: + with open('version', 'r') as f: + data = f.read().replace('\n', '') + if helper.is_sha1(data): + status['current_commit_hash'] = data + except FileNotFoundError: + pass try: r = requests.get(repository_url + '/git/refs/heads/master') @@ -1108,7 +1117,7 @@ def get_update_status(): if 'error' in status: return json.dumps(status) - if 'object' in commit and commit['object']['sha'] != current_commit_id: + if 'object' in commit and commit['object']['sha'] != status['current_commit_hash']: # a new update is available try: r = requests.get(repository_url + '/git/commits/' + commit['object']['sha']) diff --git a/version b/version new file mode 100644 index 00000000..313ee7cd --- /dev/null +++ b/version @@ -0,0 +1 @@ +$Format:%H$ \ No newline at end of file From 354eac1512af6f663fca565a2770fb16f60965ed Mon Sep 17 00:00:00 2001 From: Virgil Grigoras Date: Sun, 9 Sep 2018 13:51:28 +0200 Subject: [PATCH 03/11] fix html errors and formating --- cps/templates/admin.html | 208 +++++++++++++++++++++------------------ 1 file changed, 113 insertions(+), 95 deletions(-) diff --git a/cps/templates/admin.html b/cps/templates/admin.html index 78773cac..2be9028e 100644 --- a/cps/templates/admin.html +++ b/cps/templates/admin.html @@ -1,105 +1,124 @@ {% extends "layout.html" %} {% block body %} -
-

{{_('User list')}}

- - - - - - - - - - - - {% for user in content %} - {% if not user.role_anonymous() or config.config_anonbrowse %} - - - - - - - - - - - {% endif %} - {% endfor %} -
{{_('Nickname')}}{{_('E-mail')}}{{_('Kindle')}}{{_('DLS')}}
{{user.nickname}}{{user.email}}{{user.kindle_mail}}{{user.downloads.count()}}
- -

{{_('SMTP e-mail server settings')}}

- - - - - - - - - - - - - - -
{{_('SMTP hostname')}}{{_('SMTP port')}}{{_('SSL')}}{{_('SMTP login')}}
{{email.mail_server}}{{email.mail_port}}{% if email.mail_use_ssl %}{% else %}{% endif %}{{email.mail_login}}
- -
-

{{_('Configuration')}}

-
-
-
{{_('Calibre DB dir')}}
-
{{config.config_calibre_dir}}
-
-
-
{{_('Log level')}}
-
{{config.get_Log_Level()}}
-
-
-
{{_('Port')}}
-
{{config.config_port}}
+
+
+
+

{{_('User list')}}

+ + + + + + + + + + + + {% for user in content %} + {% if not user.role_anonymous() or config.config_anonbrowse %} + + + + + + + + + + + {% endif %} + {% endfor %} +
{{_('Nickname')}}{{_('E-mail')}}{{_('Kindle')}}{{_('DLS')}}
{{user.nickname}}{{user.email}}{{user.kindle_mail}}{{user.downloads.count()}}
+
-
-
-
{{_('Books per page')}}
-
{{config.config_books_per_page}}
-
-
-
{{_('Uploading')}}
-
{% if config.config_uploading %}{% else %}{% endif %}
-
-
-
{{_('Anonymous browsing')}}
-
{% if config.config_anonbrowse %}{% else %}{% endif %}
-
-
-
{{_('Public registration')}}
-
{% if config.config_public_reg %}{% else %}{% endif %}
+ +
+
+

{{_('SMTP e-mail server settings')}}

+ + + + + + + + + + + + + + + +
{{_('SMTP hostname')}}{{_('SMTP port')}}{{_('SSL')}}{{_('SMTP login')}}
{{email.mail_server}}{{email.mail_port}}{% if email.mail_use_ssl %}{% else %}{% endif %}{{email.mail_login}}
+
-
-
{{_('Remote login')}}
-
{% if config.config_remote_login %}{% else %}{% endif %}
+
+ +
+
+

{{_('Configuration')}}

+
+
+
{{_('Calibre DB dir')}}
+
{{config.config_calibre_dir}}
+
+
+
{{_('Log level')}}
+
{{config.get_Log_Level()}}
+
+
+
{{_('Port')}}
+
{{config.config_port}}
+
+
+
+
+
{{_('Books per page')}}
+
{{config.config_books_per_page}}
+
+
+
{{_('Uploading')}}
+
{% if config.config_uploading %}{% else %}{% endif %}
+
+
+
{{_('Anonymous browsing')}}
+
{% if config.config_anonbrowse %}{% else %}{% endif %}
+
+
+
{{_('Public registration')}}
+
{% if config.config_public_reg %}{% else %}{% endif %}
+
+
+
{{_('Remote login')}}
+
{% if config.config_remote_login %}{% else %}{% endif %}
+
+
+
-
-
-

- - + +
+
+

{{_('Administration')}}

+
{{_('Current commit timestamp')}}: {{commit}}
+ + +

+
{{_('Reconnect to Calibre DB')}}
+
{{_('Restart Calibre-Web')}}
+
{{_('Stop Calibre-Web')}}
+
{{_('Check for update')}}
+
-

{{_('Administration')}}

-
{{_('Current commit timestamp')}}: {{commit}}
- - -

-
{{_('Reconnect to Calibre DB')}}
-
{{_('Restart Calibre-Web')}}
-
{{_('Stop Calibre-Web')}}
-
{{_('Check for update')}}
- +
-