From e7e3eb36c00883a4057655fc47ca8f09df71b70a Mon Sep 17 00:00:00 2001 From: Leigh Morresi <275001+dgtlmoon@users.noreply.github.com> Date: Tue, 2 Feb 2021 12:14:23 +0100 Subject: [PATCH] Refactor slightly confusing difference build function --- backend/backend.py | 33 +++++++++++++++++++-------------- backend/templates/diff.html | 4 ++-- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/backend/backend.py b/backend/backend.py index 06876c47..cee3fedd 100644 --- a/backend/backend.py +++ b/backend/backend.py @@ -185,30 +185,35 @@ def diff_history_page(uuid): watch = datastore.data['watching'][uuid] dates = list(watch['history'].keys()) + # Convert to int, sort and back to str again dates = [int(i) for i in dates] dates.sort(reverse=True) dates = [str(i) for i in dates] - - left_file_contents = right_file_contents = "" - l_file = watch['history'][str(dates[1])] - with open(l_file, 'r') as f: - left_file_contents = f.read() + newest_file = watch['history'][dates[0]] + with open(newest_file, 'r') as f: + newest_version_file_contents = f.read() previous_version = request.args.get('previous_version') + try: - r_file = watch['history'][str(previous_version)] + previous_file = watch['history'][previous_version] except KeyError: - # Not present, use a default value - r_file = watch['history'][dates[0]] + # Not present, use a default value, the second one in the sorted list. + previous_file = watch['history'][dates[1]] - with open(r_file, 'r') as f: - right_file_contents = f.read() + with open(previous_file, 'r') as f: + previous_version_file_contents = f.read() + + output = render_template("diff.html", watch_a=watch, + messages=messages, + newest=newest_version_file_contents, + previous=previous_version_file_contents, + extra_stylesheets=extra_stylesheets, + versions=dates[1:], + current_previous_version=str(previous_version), + current_diff_url=watch['url']) - #print (dates, l_file, r_file) - output = render_template("diff.html", watch_a=watch, messages=messages, left=left_file_contents, - right=right_file_contents, extra_stylesheets=extra_stylesheets, versions=dates[:-1], - current_previous_version=str(previous_version), current_diff_url=watch['url']) return output @app.route("/favicon.ico", methods=['GET']) diff --git a/backend/templates/diff.html b/backend/templates/diff.html index 044d5e25..633ce4be 100644 --- a/backend/templates/diff.html +++ b/backend/templates/diff.html @@ -38,8 +38,8 @@