Fix for adding images whilst editing, for where the t directory does not exist and at the root directory of expoweb

This commit is contained in:
Martin Green
2022-06-26 11:20:14 +01:00
parent 5fbe0b31c2
commit 8f0ea8ed82

View File

@@ -20,22 +20,34 @@ MAX_IMAGE_HEIGHT = 800
THUMBNAIL_WIDTH = 200 THUMBNAIL_WIDTH = 200
THUMBNAIL_HEIGHT = 200 THUMBNAIL_HEIGHT = 200
def get_dir(path):
"From a path sent from urls.py, determine the directory."
if "/" in path:
return path.rsplit('/', 1)[0]
else:
return ""
def image_selector(request, path): def image_selector(request, path):
'''Returns available images''' '''Returns available images'''
directory = path.rsplit('/', 1)[0] directory = get_dir(path)
thumbnailspath = Path(settings.EXPOWEB) / directory / "t" thumbnailspath = Path(settings.EXPOWEB) / directory / "t"
thumbnails = [] thumbnails = []
for f in thumbnailspath.iterdir(): if thumbnailspath.is_dir():
if f.is_file(): for f in thumbnailspath.iterdir():
thumbnail_url = reverse('expopage', args=["%s/t/%s" % (directory, f.name)]) if f.is_file():
name_base = f.name.rsplit('.', 1)[0] if directory:
page_path_base = Path(settings.EXPOWEB) / directory / "l" base = f"{directory}/"
if ((page_path_base / ("%s.htm" % name_base)).is_file()): else:
page_url = reverse('expopage', args=["%s/l/%s.htm" % (directory, name_base)]) base = ""
else: thumbnail_url = reverse('expopage', args=["%st/%s" % (base, f.name)])
page_url = reverse('expopage', args=["%s/l/%s.html" % (directory, name_base)]) name_base = f.name.rsplit('.', 1)[0]
page_path_base = Path(settings.EXPOWEB) / directory / "l"
if ((page_path_base / ("%s.htm" % name_base)).is_file()):
page_url = reverse('expopage', args=["%sl/%s.htm" % (base, name_base)])
else:
page_url = reverse('expopage', args=["%s/l/%s.html" % (base, name_base)])
thumbnails.append({"thumbnail_url": thumbnail_url, "page_url": page_url}) thumbnails.append({"thumbnail_url": thumbnail_url, "page_url": page_url})
return render(request, 'image_selector.html', {'thumbnails': thumbnails}) return render(request, 'image_selector.html', {'thumbnails': thumbnails})
@@ -43,7 +55,7 @@ def image_selector(request, path):
@ensure_csrf_cookie @ensure_csrf_cookie
def new_image_form(request, path): def new_image_form(request, path):
'''Manages a form to upload new images''' '''Manages a form to upload new images'''
directory = path.rsplit('/', 1)[0] directory = get_dir(path)
if request.method == 'POST': if request.method == 'POST':
form = NewWebImageForm(request.POST, request.FILES, directory = directory) form = NewWebImageForm(request.POST, request.FILES, directory = directory)
if form.is_valid(): if form.is_valid():
@@ -69,6 +81,10 @@ def new_image_form(request, path):
'filepath': f'/{image_rel_path}' 'filepath': f'/{image_rel_path}'
}) })
image_path, thumb_path, desc_path = form.get_full_paths() image_path, thumb_path, desc_path = form.get_full_paths()
# Create directories if required
for full_path in image_path, thumb_path, desc_path:
print(full_path, full_path.parent)
full_path.parent.mkdir(parents=False, exist_ok=True)
try: try:
change_message = form.cleaned_data["change_message"] change_message = form.cleaned_data["change_message"]
version_control.write_and_commit([(desc_path, image_page, "utf-8"), version_control.write_and_commit([(desc_path, image_page, "utf-8"),