Slanje dokumenta u PDF fajlu
This commit is contained in:
@@ -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