furry/shop/emails.py

206 lines
5.6 KiB
Python

from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from django.utils.translation import gettext as _
from django.conf import settings
from django.urls import reverse
from django.contrib.sites.shortcuts import get_current_site
def send_order_confirmation(request, order):
"""
Sendet eine Bestellbestätigungs-E-Mail an den Kunden
"""
context = {
'order': order,
'logo_url': f"{settings.STATIC_URL}images/logo.png",
'order_url': request.build_absolute_uri(
reverse('shop:my_orders')
)
}
# HTML-Version
html_content = render_to_string(
'shop/emails/order_confirmation.html',
context
)
# Text-Version
text_content = render_to_string(
'shop/emails/order_confirmation.txt',
context
)
subject = _('Order Confirmation - Order #{}').format(order.id)
from_email = settings.DEFAULT_FROM_EMAIL
to_email = order.shipping_address.email
msg = EmailMultiAlternatives(
subject,
text_content,
from_email,
[to_email]
)
msg.attach_alternative(html_content, "text/html")
msg.send()
def send_order_status_update(request, order, update=None):
"""
Sendet eine E-Mail über Statusänderungen der Bestellung
"""
context = {
'order': order,
'update': update,
'logo_url': f"{settings.STATIC_URL}images/logo.png",
'order_url': request.build_absolute_uri(
reverse('shop:my_orders')
)
}
# HTML-Version
html_content = render_to_string(
'shop/emails/order_status_update.html',
context
)
# Text-Version
text_content = render_to_string(
'shop/emails/order_status_update.txt',
context
)
subject = _('Order Status Update - Order #{}').format(order.id)
from_email = settings.DEFAULT_FROM_EMAIL
to_email = order.shipping_address.email
msg = EmailMultiAlternatives(
subject,
text_content,
from_email,
[to_email]
)
msg.attach_alternative(html_content, "text/html")
msg.send()
def send_shipping_confirmation(request, order):
"""
Sendet eine Versandbestätigungs-E-Mail mit Tracking-Nummer
"""
context = {
'order': order,
'logo_url': f"{settings.STATIC_URL}images/logo.png",
'order_url': request.build_absolute_uri(
reverse('shop:my_orders')
)
}
# HTML-Version
html_content = render_to_string(
'shop/emails/shipping_confirmation.html',
context
)
# Text-Version
text_content = render_to_string(
'shop/emails/shipping_confirmation.txt',
context
)
subject = _('Your Order Has Been Shipped - Order #{}').format(order.id)
from_email = settings.DEFAULT_FROM_EMAIL
to_email = order.shipping_address.email
msg = EmailMultiAlternatives(
subject,
text_content,
from_email,
[to_email]
)
msg.attach_alternative(html_content, "text/html")
msg.send()
def send_admin_notification(request, order, notification_type, extra_context=None):
"""
Sendet eine Benachrichtigung an den Shop-Administrator
"""
context = {
'order': order,
'notification_type': notification_type,
'logo_url': f"{settings.STATIC_URL}images/logo.png",
'order_url': request.build_absolute_uri(
reverse('admin:shop_order_change', args=[order.id])
)
}
if extra_context:
context.update(extra_context)
# HTML-Version
html_content = render_to_string(
'shop/emails/admin_notification.html',
context
)
# Text-Version
text_content = render_to_string(
'shop/emails/admin_notification.txt',
context
)
# Betreff basierend auf Benachrichtigungstyp
subjects = {
'new_order': _('New Order Received - Order #{}'),
'payment_failed': _('Payment Failed - Order #{}'),
'custom_design': _('New Custom Design Order #{}'),
'fursuit_order': _('New Fursuit Order #{}'),
}
subject = subjects.get(notification_type, _('Order Notification - Order #{}')).format(order.id)
# E-Mail an alle konfigurierten Admin-E-Mail-Adressen senden
admin_emails = [email for name, email in settings.ADMINS]
if admin_emails:
msg = EmailMultiAlternatives(
subject,
text_content,
settings.DEFAULT_FROM_EMAIL,
admin_emails
)
msg.attach_alternative(html_content, "text/html")
msg.send()
def send_low_stock_notification(request, product):
"""
Benachrichtigt den Admin über niedrigen Lagerbestand
"""
context = {
'product': product,
'logo_url': f"{settings.STATIC_URL}images/logo.png",
'product_url': request.build_absolute_uri(
reverse('admin:shop_product_change', args=[product.id])
)
}
# HTML-Version
html_content = render_to_string(
'shop/emails/low_stock_notification.html',
context
)
# Text-Version
text_content = render_to_string(
'shop/emails/low_stock_notification.txt',
context
)
subject = _('Low Stock Alert - {}').format(product.name)
admin_emails = [email for name, email in settings.ADMINS]
if admin_emails:
msg = EmailMultiAlternatives(
subject,
text_content,
settings.DEFAULT_FROM_EMAIL,
admin_emails
)
msg.attach_alternative(html_content, "text/html")
msg.send()