From 301946deeba7150f64c58f3bb2da2368cb8294b2 Mon Sep 17 00:00:00 2001 From: William Gearty Date: Tue, 25 Jul 2023 15:37:41 -0400 Subject: [PATCH] Move backup theme images to their own folder to avoid clobbering --- .gitignore | 1 + esp/esp/themes/views.py | 20 +++++++++++--------- esp/templates/themes/logos.html | 5 +++++ 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index e9476cee5a..0b85a05ed1 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ esp/public/media/email_images/* esp/public/media/uploaded/* esp/public/media/styles esp/public/media/images +esp/public/media/default_images/backups esp/public/media/default_images/histograms esp/public/media/default_images/theme esp/public/media/default_images/favicon.ico diff --git a/esp/esp/themes/views.py b/esp/esp/themes/views.py index b57514d7da..0fee31d72a 100755 --- a/esp/esp/themes/views.py +++ b/esp/esp/themes/views.py @@ -97,6 +97,8 @@ def selector(request, keep_files=None): def logos(request): context = {} tc = ThemeController() + if not os.path.exists(settings.MEDIA_ROOT + 'images/backups'): + os.makedirs(settings.MEDIA_ROOT + 'images/backups') if request.POST: if 'new_logo' in request.FILES: @@ -104,7 +106,7 @@ def logos(request): with open(settings.MEDIA_ROOT + 'images/theme/logo.png', 'wb+') as destination: for chunk in f.chunks(): destination.write(chunk) - with open(settings.MEDIA_ROOT + 'images/theme/logo.' + datetime.now().strftime("%Y%m%d-%H%M%S") + '.png', 'wb+') as destination: + with open(settings.MEDIA_ROOT + 'images/backups/logo.' + datetime.now().strftime("%Y%m%d-%H%M%S") + '.png', 'wb+') as destination: for chunk in f.chunks(): destination.write(chunk) elif 'new_header' in request.FILES: @@ -112,7 +114,7 @@ def logos(request): with open(settings.MEDIA_ROOT + 'images/theme/header.png', 'wb+') as destination: for chunk in f.chunks(): destination.write(chunk) - with open(settings.MEDIA_ROOT + 'images/theme/header.' + datetime.now().strftime("%Y%m%d-%H%M%S") + '.png', 'wb+') as destination: + with open(settings.MEDIA_ROOT + 'images/backups/header.' + datetime.now().strftime("%Y%m%d-%H%M%S") + '.png', 'wb+') as destination: for chunk in f.chunks(): destination.write(chunk) elif 'new_favicon' in request.FILES: @@ -120,19 +122,19 @@ def logos(request): with open(settings.MEDIA_ROOT + 'images/favicon.ico', 'wb+') as destination: for chunk in f.chunks(): destination.write(chunk) - with open(settings.MEDIA_ROOT + 'images/favicon.' + datetime.now().strftime("%Y%m%d-%H%M%S") + '.ico', 'wb+') as destination: + with open(settings.MEDIA_ROOT + 'images/backups/favicon.' + datetime.now().strftime("%Y%m%d-%H%M%S") + '.ico', 'wb+') as destination: for chunk in f.chunks(): destination.write(chunk) elif 'logo_select' in request.POST: - shutil.copyfile(settings.MEDIA_ROOT + 'images/theme/' + request.POST['logo_select'], settings.MEDIA_ROOT + 'images/theme/logo.png') + shutil.copyfile(settings.MEDIA_ROOT + 'images/backups/' + request.POST['logo_select'], settings.MEDIA_ROOT + 'images/theme/logo.png') elif 'header_select' in request.POST: - shutil.copyfile(settings.MEDIA_ROOT + 'images/theme/' + request.POST['header_select'], settings.MEDIA_ROOT + 'images/theme/header.png') + shutil.copyfile(settings.MEDIA_ROOT + 'images/backups/' + request.POST['header_select'], settings.MEDIA_ROOT + 'images/theme/header.png') elif 'favicon_select' in request.POST: - shutil.copyfile(settings.MEDIA_ROOT + 'images/' + request.POST['favicon_select'], settings.MEDIA_ROOT + 'images/favicon.ico') + shutil.copyfile(settings.MEDIA_ROOT + 'images/backups/' + request.POST['favicon_select'], settings.MEDIA_ROOT + 'images/favicon.ico') - context['logo_files'] = [(path.split('public')[1], path.split('images/theme/')[1]) for path in tc.list_filenames(settings.MEDIA_ROOT + 'images/theme', "logo\..*\.png")] - context['header_files'] = [(path.split('public')[1], path.split('images/theme/')[1]) for path in tc.list_filenames(settings.MEDIA_ROOT + 'images/theme', "header\..*\.png")] - context['favicon_files'] = [(path.split('public')[1], path.split('images/')[1]) for path in tc.list_filenames(settings.MEDIA_ROOT + 'images', "favicon\..*\.ico")] + context['logo_files'] = [(path.split('public')[1], path.split('images/backups/')[1]) for path in tc.list_filenames(settings.MEDIA_ROOT + 'images/backups', "logo\..*\.png")] + context['header_files'] = [(path.split('public')[1], path.split('images/backups/')[1]) for path in tc.list_filenames(settings.MEDIA_ROOT + 'images/backups', "header\..*\.png")] + context['favicon_files'] = [(path.split('public')[1], path.split('images/backups/')[1]) for path in tc.list_filenames(settings.MEDIA_ROOT + 'images/backups', "favicon\..*\.ico")] context['has_header'] = os.path.exists(settings.MEDIA_ROOT + 'images/theme/header.png') return render_to_response('themes/logos.html', request, context) diff --git a/esp/templates/themes/logos.html b/esp/templates/themes/logos.html index 66c8f00ec5..fe32a71959 100644 --- a/esp/templates/themes/logos.html +++ b/esp/templates/themes/logos.html @@ -48,6 +48,11 @@

Logo Editor

+
+ + You may need to hard refresh the page (Shift + Refresh) after changing any of these theme images for the changes to take effect locally. +
+