From 99d59cd0e76369a281bd33e5fee848e35e571a22 Mon Sep 17 00:00:00 2001 From: CasVT Date: Sun, 10 Mar 2024 11:53:12 +0100 Subject: [PATCH] Fixed manifest updating for smaller url prefix When the value of the URL prefix would be changed, if the resulting json would be smaller than the current json, it would create invalid json. Now we wipe the contents before writing to avoid this. --- backend/settings.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/backend/settings.py b/backend/settings.py index ef97dc9..e616bb5 100644 --- a/backend/settings.py +++ b/backend/settings.py @@ -5,7 +5,7 @@ Getting and setting settings """ import logging -from json import dumps, loads +from json import dump, load from typing import Any from backend.custom_exceptions import InvalidKeyValue, KeyNotFound @@ -67,7 +67,10 @@ def _format_setting(key: str, value): if not isinstance(value, str): raise InvalidKeyValue(key, value) - if value: + if value == '/': + value = '' + + elif value: value = '/' + value.strip('/') elif key == 'log_level' and not value in (logging.INFO, logging.DEBUG): @@ -171,12 +174,16 @@ def update_manifest(url_base: str) -> None: Args: url_base (str): The url base to use in the file. """ - with open(folder_path('frontend', 'static', 'json', 'pwa_manifest.json'), 'r+') as f: - manifest = loads(f.read()) + filename = folder_path('frontend', 'static', 'json', 'pwa_manifest.json') + + with open(filename, 'r') as f: + manifest = load(f) manifest['start_url'] = url_base + '/' manifest['icons'][0]['src'] = f'{url_base}/static/img/favicon.svg' - f.seek(0) - f.write(dumps(manifest, indent=4)) + + with open(filename, 'w') as f: + dump(manifest, f, indent=4) + return def backup_hosting_settings() -> None: @@ -228,5 +235,7 @@ def restore_hosting_settings() -> None: "UPDATE config SET value = ? WHERE key = ?", ((v, k) for k, v in hosting_settings.items()) ) - + + update_manifest(hosting_settings['url_prefix']) + return