Files
old-riskletpy/backend/accounts/utils.py

247 lines
8.9 KiB
Python
Raw Normal View History

2025-02-13 16:38:29 +01:00
from django.core.mail import send_mail
from django.urls import reverse
from .models import EmailConfirmation
import uuid
from django.conf import settings
from django.utils.timezone import now
from backend.core.models import Document, Organization
2025-02-24 10:19:27 +01:00
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
2025-03-19 13:48:52 +01:00
site_domain = settings.SITE_DOMAIN
2025-02-13 16:38:29 +01:00
def send_confirmation_email(email):
confirmation, created = EmailConfirmation.objects.get_or_create(email=email)
if not created:
confirmation.uuid = uuid.uuid4()
confirmation.created_at = now()
confirmation.save()
2025-03-19 13:48:52 +01:00
confirmation_link = f"{site_domain}{reverse('confirm_email', args=[confirmation.uuid])}"
2025-02-13 16:38:29 +01:00
2025-10-08 18:04:10 +02:00
content_html = format_html(
"""
<p>Dear user,</p>
<p>To start generating your document, please confirm your email address by clicking the button below:</p>
<p style="text-align:center;margin:32px 0;">
<a href="{}" style="background:orange;color:#19161C;padding:12px 32px;
border-radius:6px;text-decoration:none;font-weight:600;">Confirm Email</a>
</p>
<p style="color:#929292;">If you did not request this, you can safely ignore this message.</p>
<p>Best regards,<br>Risklet Team</p>
""",
confirmation_link
)
html_message = render_email_template(content_html, title="Confirm Your Email")
msg = EmailMultiAlternatives(
subject="Confirm your email to start document generation",
body=html_message,
from_email=settings.DEFAULT_FROM_EMAIL,
2025-10-08 18:04:10 +02:00
to=[email]
)
2025-10-08 18:04:10 +02:00
msg.content_subtype = "html"
msg.send()
def send_payment_email(email):
2025-03-19 13:48:52 +01:00
payment_link = f"{site_domain}{reverse('core:payment_page')}?email={email}"
2025-10-08 18:04:10 +02:00
content_html = format_html(
"""
<p>Dear customer,</p>
<p>Your document is almost ready. Please complete the payment process to unlock the final version.</p>
<p style="text-align:center;margin:32px 0;">
<a href="{}" style="background:#28a745;color:#fff;padding:12px 32px;
border-radius:6px;text-decoration:none;font-weight:600;display:inline-block;">
Proceed to Payment
</a>
</p>
<p style="color:#929292;">If you have already completed the payment, you can ignore this message.</p>
<p>Best regards,<br>Risklet Team</p>
""",
payment_link,
)
html_message = render_email_template(content_html, title="Complete Your Payment")
msg = EmailMultiAlternatives(
subject="Complete your payment to finish document generation",
body=html_message,
from_email=settings.DEFAULT_FROM_EMAIL,
2025-10-08 18:04:10 +02:00
to=[email],
)
2025-10-08 18:04:10 +02:00
msg.content_subtype = "html"
msg.send()
2025-08-17 18:43:55 +02:00
def send_documet_to_expert(email, document):
document_link = f"{site_domain}{reverse('core:document', args=[document.id])}"
edit_link = f"{site_domain}{reverse('admin:core_document_change', args=[document.id])}"
2025-10-08 18:04:10 +02:00
content_html = format_html(
"""
<p>Hello,</p>
<p>A new document has been assigned to you for expert analysis. Please review the details below:</p>
<p style="text-align:center;margin:24px 0;">
<a href="{}" style="display:inline-block;margin:0 8px;background:#0E3B43;color:#fff;
padding:12px 28px;border-radius:6px;text-decoration:none;font-weight:600;">
View Document
</a>
<a href="{}" style="display:inline-block;margin:0 8px;background:#28a745;color:#fff;
padding:12px 28px;border-radius:6px;text-decoration:none;font-weight:600;">
Edit in Admin
</a>
</p>
<p>Thank you for your contribution.</p>
<p>Best regards,<br>Risklet Team</p>
""",
document_link,
edit_link,
)
html_message = render_email_template(content_html, title="New Expert Assignment")
2025-10-08 18:04:10 +02:00
msg = EmailMultiAlternatives(
subject="Document assigned for expert analysis",
body=html_message,
from_email=settings.DEFAULT_FROM_EMAIL,
2025-10-08 18:04:10 +02:00
to=[email],
)
2025-10-08 18:04:10 +02:00
msg.content_subtype = "html"
msg.send()
def send_document_to_reviewer(email, document):
document_link = f"{site_domain}{reverse('core:document', args=[document.id])}"
edit_link = f"{site_domain}{reverse('admin:core_document_change', args=[document.id])}"
2025-10-08 18:04:10 +02:00
content_html = format_html(
"""
<p>Hello,</p>
<p>We encountered an issue while generating the document. Please review and complete the document before sending it to the customer.</p>
<p style="text-align:center;margin:24px 0;">
<a href="{}" style="display:inline-block;margin:0 8px;background:#0E3B43;color:#fff;
padding:12px 28px;border-radius:6px;text-decoration:none;font-weight:600;">
View Document
</a>
<a href="{}" style="display:inline-block;margin:0 8px;background:#28a745;color:#fff;
padding:12px 28px;border-radius:6px;text-decoration:none;font-weight:600;">
Edit in Admin
</a>
</p>
<p>Once you finish, save the document and share it with the customer.</p>
<p>Thank you for your help.<br>Risklet Team</p>
""",
document_link,
edit_link,
)
html_message = render_email_template(content_html, title="Document Review Required")
msg = EmailMultiAlternatives(
subject="Document review required",
body=html_message,
2025-08-17 18:43:55 +02:00
from_email=settings.DEFAULT_FROM_EMAIL,
2025-10-08 18:04:10 +02:00
to=[email],
2025-08-17 18:43:55 +02:00
)
2025-10-08 18:04:10 +02:00
msg.content_subtype = "html"
msg.send()
2025-08-17 18:43:55 +02:00
2025-02-24 10:19:27 +01:00
def send_document_email(email, document_link, document):
image_io = generate_first_page_image(document)
2025-10-08 18:04:10 +02:00
content_html = format_html(
"""
<p>Your document is ready. Click the preview below to open the full PDF:</p>
<p style="text-align:center;margin:32px 0;">
<a href="{}" target="_blank" style="display:inline-block;">
<img src="cid:first_page" style="width:100%;max-width:320px;border:1px solid #0E3B43;
border-radius:12px;" alt="Document Preview">
</a>
</p>
<p>If you have any questions, feel free to reach out.</p>
<p>Best regards,<br>Risklet Team</p>
""",
document_link,
)
2025-10-08 18:04:10 +02:00
html_message = render_email_template(content_html, title="Your Document is Ready")
2025-10-08 18:04:10 +02:00
msg = EmailMultiAlternatives(
subject="Your document is ready",
body=html_message,
from_email=settings.DEFAULT_FROM_EMAIL,
to=[email],
)
msg.content_subtype = "html"
2025-02-24 10:19:27 +01:00
image_attachment = MIMEImage(image_io.getvalue(), "image/jpeg")
2025-10-08 18:04:10 +02:00
image_attachment.add_header("Content-ID", "<first_page>")
2025-02-24 10:19:27 +01:00
msg.attach(image_attachment)
msg.send()
2025-10-08 18:04:10 +02:00
def render_email_template(content_html, title = 'Risklet Notifications'):
logo_url = f"https://risklet.com/static/img/risklet-icon.png"
return format_html(
"""
<div style="
margin:32px auto;
background:#fff;
border:1px solid #0E3B43;
border-radius:16px;
font-family:'Darker Grotesque',Arial,sans-serif;
color:#0E3B43;
box-shadow:0 4px 24px rgba(14,59,67,0.07);
">
<div style="
background:#0E3B43;
margin:0;
padding:24px 0;
text-align:center;
border-top-left-radius:16px;
border-top-right-radius:16px;
">
<img src="{}" alt="Risklet Logo" style="
display:block;
margin:0 auto 12px auto;
height:56px;
width:auto;
border:none;
">
<h2 style="
margin:0;
color:#fff;
font-weight:800;
font-size:24px;
letter-spacing:1px;
">
Risklet
</h2>
</div>
<div style="padding:32px 24px;">
<h2 style="
color:#0E3B43;
text-align:center;
margin:0 0 24px 0;
font-weight:800;
letter-spacing:1px;
">
{}
</h2>
<div style="font-size:1.15em;line-height:1.7;">
{}
</div>
<div style="
margin-top:32px;
text-align:center;
color:#0E3B43;
font-size:1em;
font-weight:600;
">
Risklet &mdash; Smart cybersecurity risk assessment
</div>
</div>
</div>
""",
logo_url,
title,
content_html,
)