32 lines
894 B
Python
32 lines
894 B
Python
|
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")
|