PenParse/penparse/webui/views/__init__.py

79 lines
1.9 KiB
Python
Raw Normal View History

2024-12-08 15:51:02 +00:00
import logging
import os
2024-12-15 06:35:39 +00:00
import markdown
2024-12-08 15:51:02 +00:00
from django.contrib import messages
from django.shortcuts import redirect, render
2024-12-10 16:05:24 +00:00
from django.http import HttpRequest, HttpResponse
2024-12-08 15:51:02 +00:00
from django.core.files.storage import default_storage
from django.core.files.base import ContentFile
from ..models import ImageMemo
from django.contrib.auth.decorators import login_required
logger = logging.getLogger(__name__)
2024-12-10 16:05:24 +00:00
from .thumbnail import document_thumbnail, document_image
2024-12-08 15:51:02 +00:00
from .register import register
from .upload import upload_document
from .delete import delete_document
2024-12-08 15:51:02 +00:00
def index(request):
# return HttpResponse("Hello, world. You're at the polls index.")
return render(request, "index.html")
__all__ = [
"index",
"document_thumbnail",
2024-12-15 06:35:39 +00:00
"document_image",
"register",
"dashboard",
"settings",
"view_document",
"download_document",
"upload_document",
"delete_document",
]
2024-12-08 15:51:02 +00:00
@login_required
def dashboard(request: HttpRequest):
documents = ImageMemo.objects.filter(author__id=request.user.id).order_by(
"-updated_at"
)
context = {"documents": documents}
return render(request, "dashboard.html", context)
@login_required
def settings(request):
return render(request, "settings.html")
@login_required
2024-12-10 16:05:24 +00:00
def view_document(request: HttpRequest, pk: str):
## check that the document exists and belongs to the user
# find document with given ID (pk path param) and current user id
document = ImageMemo.objects.filter(id=pk, author__id=request.user.id).first()
2024-12-15 06:35:39 +00:00
doc_markup = markdown.markdown(document.content)
2024-12-10 16:05:24 +00:00
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)
2024-12-15 06:35:39 +00:00
return render(request, "document.html", context={"document": document, "markup": doc_markup})
2024-12-08 15:51:02 +00:00
@login_required
def download_document(request):
return render(request, "document.html")