From 7176e8ce09416613511ac702c78118ead4211b7a Mon Sep 17 00:00:00 2001 From: James Ravenscroft Date: Mon, 9 Dec 2024 10:06:41 +0000 Subject: [PATCH] move to django default storage for uploads --- penparse/webui/templates/dashboard.html | 6 +++++ penparse/webui/test/test_thumbnail_view.py | 4 ++- penparse/webui/views/delete.py | 31 ++++++++++++++++++++++ penparse/webui/views/thumbnail.py | 15 ++++++----- 4 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 penparse/webui/views/delete.py diff --git a/penparse/webui/templates/dashboard.html b/penparse/webui/templates/dashboard.html index 0b81e3e..eb0aeef 100644 --- a/penparse/webui/templates/dashboard.html +++ b/penparse/webui/templates/dashboard.html @@ -30,6 +30,12 @@ class="bg-green-500 text-white px-4 py-2 rounded hover:bg-green-600 transition duration-300" >Download +
+ {% csrf_token %} + +
{% empty %} diff --git a/penparse/webui/test/test_thumbnail_view.py b/penparse/webui/test/test_thumbnail_view.py index c51efb8..dfbed55 100644 --- a/penparse/webui/test/test_thumbnail_view.py +++ b/penparse/webui/test/test_thumbnail_view.py @@ -145,4 +145,6 @@ def test_document_thumbnail_file_missing(client, cleanup_uploaded_files, caplog) assert response.status_code == 404 assert response.content == b"Document not found" - assert "The file associated with this document does not exist" in caplog.text \ No newline at end of file + assert ( + f"The file associated with memo {image_memo.id} does not exist" in caplog.text + ) diff --git a/penparse/webui/views/delete.py b/penparse/webui/views/delete.py new file mode 100644 index 0000000..f405024 --- /dev/null +++ b/penparse/webui/views/delete.py @@ -0,0 +1,31 @@ +import logging +import os + +from django.contrib import messages +from django.shortcuts import redirect +from django.core.files.storage import default_storage +from django.core.files.base import ContentFile +from ..models import ImageMemo + +from django.http import HttpRequest, HttpResponse + +from django.contrib.auth.decorators import login_required + + +logger = logging.getLogger(__name__) + + +@login_required +def delete_document(request: HttpRequest, pk: str): + + # find document with given ID (pk path param) and current user id + document = ImageMemo.objects.filter(id=pk, author__id=request.user.id).first() + + if not document: + logger.debug(f"No memo found for user={request.user.id} and memo_id={pk}") + return HttpResponse(content="Document not found", status=404) + + # delete file from storage + default_storage.delete(document.image.name) + + return redirect("dashboard") diff --git a/penparse/webui/views/thumbnail.py b/penparse/webui/views/thumbnail.py index 16b317f..e43d182 100644 --- a/penparse/webui/views/thumbnail.py +++ b/penparse/webui/views/thumbnail.py @@ -33,13 +33,12 @@ def document_thumbnail(request: HttpRequest, pk: str): thumb_path = os.path.join(thumbnail_dir, str(document.id) + ".jpg") - if not os.path.exists(thumb_path): + if not default_storage.exists(thumb_path): - if not os.path.exists(thumbnail_dir): - os.makedirs(thumbnail_dir) - - if not os.path.exists(document.image.name): - logger.warning("The file associated with this document does not exist") + if not default_storage.exists(document.image.name): + logger.warning( + f"The file associated with memo {document.id} does not exist" + ) return HttpResponse(content="Document not found", status=404) # Open the image using PIL @@ -64,9 +63,11 @@ def document_thumbnail(request: HttpRequest, pk: str): # Save the thumbnail to a BytesIO object thumb_io = BytesIO() - image.save(thumb_path, format="JPEG") + image.save(thumb_io, format="JPEG") thumb_io.seek(0) + default_storage.save(thumb_path, thumb_io) + # Return the thumbnail as an HTTP response with open(thumb_path, "rb") as f: return HttpResponse(f.read(), content_type="image/jpeg")