PenParse/penparse/webui/views/__init__.py

83 lines
2.2 KiB
Python
Raw Normal View History

2024-12-08 15:51:02 +00:00
import logging
import os
from django.contrib import messages
from django.shortcuts import redirect, render
from django.http import HttpRequest
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__)
from .thumbnail import document_thumbnail
from .register import register
def index(request):
# return HttpResponse("Hello, world. You're at the polls index.")
return render(request, "index.html")
__all__ = ["index", "document_thumbnail", "register", "dashboard", "settings", "view_document", "download_document", "upload_document"]
@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
def view_document(request):
return render(request, "document.html")
@login_required
def download_document(request):
return render(request, "document.html")
@login_required
def upload_document(request):
if request.method == "POST" and request.FILES.get("document"):
uploaded_file = request.FILES["document"]
# Check if the file is an image
if not uploaded_file.content_type.startswith("image/"):
messages.error(request, "Please upload an image file.")
return redirect("dashboard")
# Save the image
file_name = default_storage.save(
f"uploads/{uploaded_file.name}", ContentFile(uploaded_file.read())
)
# Create an ImageMemo instance
image_memo = ImageMemo(
image=file_name,
content="", # You can add initial content here if needed
author=request.user, # Assuming the user is authenticated
)
image_memo.save()
messages.success(request, "Image uploaded successfully!")
return redirect("dashboard") # Redirect to dashboard or appropriate page