Skip to content

Commit

Permalink
Move backup theme images to their own folder to avoid clobbering
Browse files Browse the repository at this point in the history
  • Loading branch information
willgearty committed Jul 26, 2023
1 parent 768ad5f commit 301946d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 11 additions & 9 deletions esp/esp/themes/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,42 +97,44 @@ 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:
f = request.FILES['new_logo']
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:
f = request.FILES['new_header']
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:
f = request.FILES['new_favicon']
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)
Expand Down
5 changes: 5 additions & 0 deletions esp/templates/themes/logos.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@

<h1>Logo Editor</h1>

<div class="alert alert-warning">
<span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span>
You may need to hard refresh the page (Shift + Refresh) after changing any of these theme images for the changes to take effect locally.
</div>

<div class="row-fluid">
<div class="well">
<center>
Expand Down

0 comments on commit 301946d

Please sign in to comment.