diff --git a/core/views/editor_helpers.py b/core/views/editor_helpers.py index 319abb6..1fe30b0 100644 --- a/core/views/editor_helpers.py +++ b/core/views/editor_helpers.py @@ -20,22 +20,34 @@ MAX_IMAGE_HEIGHT = 800 THUMBNAIL_WIDTH = 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): '''Returns available images''' - directory = path.rsplit('/', 1)[0] + directory = get_dir(path) thumbnailspath = Path(settings.EXPOWEB) / directory / "t" thumbnails = [] - for f in thumbnailspath.iterdir(): - if f.is_file(): - thumbnail_url = reverse('expopage', args=["%s/t/%s" % (directory, f.name)]) - 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=["%s/l/%s.htm" % (directory, name_base)]) - else: - page_url = reverse('expopage', args=["%s/l/%s.html" % (directory, name_base)]) + if thumbnailspath.is_dir(): + for f in thumbnailspath.iterdir(): + if f.is_file(): + if directory: + base = f"{directory}/" + else: + base = "" + thumbnail_url = reverse('expopage', args=["%st/%s" % (base, f.name)]) + 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}) @@ -43,7 +55,7 @@ def image_selector(request, path): @ensure_csrf_cookie def new_image_form(request, path): '''Manages a form to upload new images''' - directory = path.rsplit('/', 1)[0] + directory = get_dir(path) if request.method == 'POST': form = NewWebImageForm(request.POST, request.FILES, directory = directory) if form.is_valid(): @@ -69,6 +81,10 @@ def new_image_form(request, path): 'filepath': f'/{image_rel_path}' }) 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: change_message = form.cleaned_data["change_message"] version_control.write_and_commit([(desc_path, image_page, "utf-8"),