Merge branch '12-napraviti-slanje-pdf-dokumenta' into 'master'
Slanje dokumenta u PDF fajlu Closes #12 See merge request kbr4/riskletpy!12
This commit was merged in pull request #61.
This commit is contained in:
@@ -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(
|
||||
'<p>Your document is ready. Click the image below to view the full PDF:</p>'
|
||||
'<a href="{}" target="_blank">'
|
||||
'<img src="cid:first_page" style="width:100%;max-width:300px;" alt="Document Preview"/></a>',
|
||||
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', '<first_page>')
|
||||
msg.attach(image_attachment)
|
||||
msg.send()
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
{% block content %}
|
||||
<div class="document-container">
|
||||
{% if error %}
|
||||
<p style="color: red;">{{ error }}</p>
|
||||
@@ -64,5 +61,4 @@
|
||||
.document-body {
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
</style>
|
||||
{% endblock %}
|
||||
</style>
|
||||
@@ -12,4 +12,5 @@ urlpatterns = [
|
||||
path('document/<uuid:document_id>/', v.document, name='document'),
|
||||
path('preview/<str:name>/', v.template_preview, name='template_preview'),
|
||||
path("payment/", v.payment_page, name="payment_page"),
|
||||
path('pdf/<uuid:document_id>/', v.pdf_view, name='pdf_view'),
|
||||
]
|
||||
|
||||
@@ -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
|
||||
@@ -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})
|
||||
Reference in New Issue
Block a user