Refactor email templates and add notification tasks

Moved email templates to a unified 'email' directory and added new base, header, and footer templates for emails. Implemented notification tasks in commerce for various order and refund events, and updated Carrier model to trigger notifications on shipping state changes.
This commit is contained in:
David Bruno Vontor
2025-11-21 15:05:32 +01:00
parent 102855f812
commit 37f36b3466
16 changed files with 209 additions and 0 deletions

View File

@@ -10,6 +10,7 @@ from django.core.validators import MaxValueValidator, MinValueValidator
from weasyprint import HTML from weasyprint import HTML
import os import os
from backend.commerce.tasks import notify_order_sended
from configuration.models import ShopConfiguration from configuration.models import ShopConfiguration
from thirdparty.zasilkovna.models import ZasilkovnaPacket from thirdparty.zasilkovna.models import ZasilkovnaPacket
from thirdparty.stripe.models import StripeModel from thirdparty.stripe.models import StripeModel
@@ -243,9 +244,16 @@ class Carrier(models.Model):
self.returning = False self.returning = False
self.save() self.save()
elif self.shipping_method == self.SHIPPING.STORE:
self.state = self.STATE.READY_TO_PICKUP
self.save()
else: else:
raise ValidationError("Tato metoda dopravy nepodporuje objednání přepravy.") raise ValidationError("Tato metoda dopravy nepodporuje objednání přepravy.")
notify_order_sended.delay(order=self.orders.first(), user=self.orders.first().user)
#... další logika pro jiné způsoby dopravy #... další logika pro jiné způsoby dopravy
#TODO: přidat notifikace uživateli, jak pro zásilkovnu, tak pro vyzvednutí v obchodě! #TODO: přidat notifikace uživateli, jak pro zásilkovnu, tak pro vyzvednutí v obchodě!

View File

@@ -1,4 +1,8 @@
from .models import Order from .models import Order
from account.models import User
from account.tasks import send_email_with_context
from celery import shared_task
from django.utils import timezone from django.utils import timezone
def delete_expired_orders(): def delete_expired_orders():
@@ -8,3 +12,132 @@ def delete_expired_orders():
return count return count
# -- NOTIFICATIONS CARRIER --
# Zásilkovna
@shared_task
def notify_zasilkovna_sended(order:Order = None, user:User = None, **kwargs):
if not order or not user:
raise ValueError("Order and User must be provided for notification.")
if kwargs:
print("Additional kwargs received in notify_order_sended:", kwargs)
send_email_with_context(
user.email,
subject="Your order has been shipped",
template_name="emails/order_sended.txt",
html_template_name="emails/order_sended.html",
context={
"user": user,
"order": order,
})
pass
# Shop
@shared_task
def notify_Ready_to_pickup(order:Order = None, user:User = None, **kwargs):
if not order or not user:
raise ValueError("Order and User must be provided for notification.")
if kwargs:
print("Additional kwargs received in notify_order_sended:", kwargs)
send_email_with_context(
user.email,
subject="Your order has been shipped",
template_name="emails/order_sended.txt",
html_template_name="emails/order_sended.html",
context={
"user": user,
"order": order,
})
pass
# -- NOTIFICATIONS ORDER --
@shared_task
def notify_order_successfuly_created(order:Order = None, user:User = None, **kwargs):
if not order or not user:
raise ValueError("Order and User must be provided for notification.")
if kwargs:
print("Additional kwargs received in notify_order_successfuly_created:", kwargs)
send_email_with_context(
user.email,
subject="Your order has been successfully created",
template_name="emails/order_created.txt",
html_template_name="emails/order_created.html",
context={
"user": user,
"order": order,
})
pass
@shared_task
def notify_about_missing_payment(order:Order = None, user:User = None, **kwargs):
if not order or not user:
raise ValueError("Order and User must be provided for notification.")
if kwargs:
print("Additional kwargs received in notify_about_missing_payment:", kwargs)
send_email_with_context(
user.email,
subject="Payment missing for your order",
template_name="emails/order_missing_payment.txt",
html_template_name="emails/order_missing_payment.html",
context={
"user": user,
"order": order,
})
pass
def notify_refund_items_arrived(order:Order = None, user:User = None, **kwargs):
if not order or not user:
raise ValueError("Order and User must be provided for notification.")
if kwargs:
print("Additional kwargs received in notify_refund_items_arrived:", kwargs)
send_email_with_context(
user.email,
subject="Your refund items have arrived",
template_name="emails/order_refund_items_arrived.txt",
html_template_name="emails/order_refund_items_arrived.html",
context={
"user": user,
"order": order,
})
pass
# Refund accepted, retuning money
@shared_task
def notify_refund_accepted(order:Order = None, user:User = None, **kwargs):
if not order or not user:
raise ValueError("Order and User must be provided for notification.")
if kwargs:
print("Additional kwargs received in notify_refund_accepted:", kwargs)
send_email_with_context(
user.email,
subject="Your refund has been accepted",
template_name="emails/order_refund_accepted.txt",
html_template_name="emails/order_refund_accepted.html",
context={
"user": user,
"order": order,
})
pass

View File

@@ -0,0 +1,51 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Email</title>
</head>
<body style="margin:0; padding:0; background-color:#f2f2f2;">
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="background-color:#f2f2f2;">
<tr>
<td align="center" style="padding:20px 0;">
<!-- HLAVNÍ KONTEJNER -->
<table width="600" cellpadding="0" cellspacing="0" border="0" style="background-color:#ffffff; border-collapse:collapse;">
<!-- HEADER -->
<tr>
<td style="padding:0; margin:0;">
{% include 'email/components/header.html' %}
</td>
</tr>
<!-- CONTENT -->
<tr>
<td style="padding:20px; font-family:Arial, Helvetica, sans-serif; font-size:14px; line-height:20px; color:#000000;">
{% if content_template %}
{% include content_template %}
{% else %}
<p>missing content_template !!!</p>
{% endif %}
</td>
</tr>
<!-- FOOTER -->
<tr>
<td style="padding:0; margin:0;">
{% include 'email/components/footer.html' %}
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>

View File

@@ -0,0 +1,8 @@
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="background-color:#eeeeee;">
<tr>
<td align="center" style="padding:15px; font-size:12px; color:#666666; font-family:Arial, Helvetica, sans-serif;">
Tento e-mail byl odeslán automaticky.<br>
© {{ site_name|default:"E-shop" }}
</td>
</tr>
</table>

View File

@@ -0,0 +1,9 @@
<table width="100%" cellpadding="0" cellspacing="0" border="0" style="background-color:#222;">
<tr>
<td align="center" style="padding:20px; color:#ffffff; font-family:Arial, Helvetica, sans-serif;">
<h2 style="margin:0; font-size:22px;">
{{ site_name|default:"E-shop" }}
</h2>
</td>
</tr>
</table>