diff --git a/backend/accounts/utils.py b/backend/accounts/utils.py index c9a6ba8..d5e083c 100644 --- a/backend/accounts/utils.py +++ b/backend/accounts/utils.py @@ -5,6 +5,11 @@ import uuid from django.conf import settings from django.utils.timezone import now from backend.core.models import Document, Organization +from django.core.mail import EmailMultiAlternatives +from django.utils.html import format_html +from backend.core.utils import generate_first_page_image +from email.mime.image import MIMEImage + def send_confirmation_email(email): confirmation, created = EmailConfirmation.objects.get_or_create(email=email) @@ -37,12 +42,21 @@ def send_payment_email(email): fail_silently=False, ) -def send_document_email(email, document_link): - send_mail( - subject="Your Document is Ready", - message=f"You can access your document at any time here: {document_link}", - from_email=settings.EMAIL_HOST_USER, - recipient_list=[email], - fail_silently=False, +def send_document_email(email, document_link, document): + image_io = generate_first_page_image(document) + + subject = "Your Document is Ready" + html_content = format_html( + '

Your document is ready. Click the image below to view the full PDF:

' + '' + 'Document Preview', + document_link ) + msg = EmailMultiAlternatives(subject, "", "riskletdev@gmail.com", [email]) + msg.attach_alternative(html_content, "text/html") + + image_attachment = MIMEImage(image_io.getvalue(), "image/jpeg") + image_attachment.add_header('Content-ID', '') + msg.attach(image_attachment) + msg.send() diff --git a/backend/core/templates/document.html b/backend/core/templates/document.html index 1fa2cc1..b48bf35 100644 --- a/backend/core/templates/document.html +++ b/backend/core/templates/document.html @@ -1,6 +1,3 @@ -{% extends 'base.html' %} - -{% block content %}
{% if error %}

{{ error }}

@@ -64,5 +61,4 @@ .document-body { margin-bottom: 1rem; } - -{% endblock %} \ No newline at end of file + \ No newline at end of file diff --git a/backend/core/urls.py b/backend/core/urls.py index 47bc788..9df5a33 100644 --- a/backend/core/urls.py +++ b/backend/core/urls.py @@ -12,4 +12,5 @@ urlpatterns = [ path('document//', v.document, name='document'), path('preview//', v.template_preview, name='template_preview'), path("payment/", v.payment_page, name="payment_page"), + path('pdf//', v.pdf_view, name='pdf_view'), ] diff --git a/backend/core/utils.py b/backend/core/utils.py index 7e164ca..308fd15 100644 --- a/backend/core/utils.py +++ b/backend/core/utils.py @@ -1,7 +1,10 @@ from openai import OpenAI from django.conf import settings from .models import Risk, Control -import time +from weasyprint import HTML +from django.http import HttpResponse +from PIL import Image +import io def extract_organization_details(organization): excluded_fields = {"name", "email"} @@ -182,3 +185,26 @@ def get_controls_for_risk(risk, organization): if not remaining_controls: break return selected_controls if len(selected_controls) == 10 else [] + + +def generate_pdf(document): + document_link = f"http://127.0.0.1:8000/document/{document.id}/" + pdf_content = HTML(url=document_link).write_pdf() + + response = HttpResponse(pdf_content, content_type='application/pdf') + response['Content-Disposition'] = f'inline; filename=document_{document.id}.pdf' + return response + +def generate_first_page_image(document): + document_link = f"http://127.0.0.1:8000/document/{document.id}/" + + pdf_bytes = HTML(url=document_link).write_pdf() + + from pdf2image import convert_from_bytes + images = convert_from_bytes(pdf_bytes, first_page=1, last_page=1) + + img_io = io.BytesIO() + images[0].save(img_io, format="JPEG", quality=90) + img_io.seek(0) + + return img_io \ No newline at end of file diff --git a/backend/core/views.py b/backend/core/views.py index 701cd67..3f6350e 100644 --- a/backend/core/views.py +++ b/backend/core/views.py @@ -3,12 +3,11 @@ import yaml from django.shortcuts import render, redirect , get_object_or_404 from .forms import OrganizationForm -from .models import Organization,Document,Risk, DocumentTemplate,DocumentRiskControl -from backend.core.utils import get_top_risk -from django.urls import reverse +from .models import Organization,Document, DocumentTemplate,DocumentRiskControl from backend.accounts.utils import send_confirmation_email, send_document_email from django.contrib.admin.views.decorators import staff_member_required from django.template import Template, Context +from .utils import generate_pdf @@ -101,15 +100,18 @@ def template_preview(request, name): parsed_template = template.to_dict() return render(request, 'template_preview.html', {'template': parsed_template}) +def pdf_view(request, document_id): + document = get_object_or_404(Document, id=document_id) + return generate_pdf(document) + def payment_page(request): email = request.GET.get("email") - organization = Organization.objects.get(email=email) - document = Document.objects.get(organization=organization) - document_link = f"http://127.0.0.1:8000/document/{document.id}/" - + organization = get_object_or_404(Organization, email=email) + document = get_object_or_404(Document, organization=organization) + if request.method == "POST": - send_document_email(email, document_link) - return redirect(document_link) - - return render(request, "payment.html", {"email": email}) - + pdf_url = f"http://127.0.0.1:8000/pdf/{document.id}" + send_document_email(email, pdf_url, document) + return redirect(pdf_url) + + return render(request, "payment.html", {"email": email}) \ No newline at end of file