""" Celery tasks for contact submission notifications. """ from celery import shared_task from django.core.mail import send_mail from django.conf import settings from django.template.loader import render_to_string from django.utils.html import strip_tags @shared_task def send_contact_confirmation_email(contact_id): """ Send confirmation email to user who submitted contact form. Args: contact_id: UUID of the ContactSubmission """ from .models import ContactSubmission try: contact = ContactSubmission.objects.get(id=contact_id) # Render email template html_message = render_to_string('emails/contact_confirmation.html', { 'name': contact.name, 'ticket_number': contact.ticket_number, 'subject': contact.subject, 'category': contact.get_category_display(), 'message': contact.message, }) plain_message = strip_tags(html_message) # Send email send_mail( subject=f'Contact Form Received - Ticket #{contact.ticket_number}', message=plain_message, from_email=settings.DEFAULT_FROM_EMAIL, recipient_list=[contact.email], html_message=html_message, fail_silently=False, ) return f"Confirmation email sent to {contact.email}" except ContactSubmission.DoesNotExist: return f"Contact submission {contact_id} not found" except Exception as e: # Log error but don't fail the task print(f"Error sending contact confirmation: {str(e)}") raise @shared_task def notify_admins_new_contact(contact_id): """ Notify admin team of new contact submission. Args: contact_id: UUID of the ContactSubmission """ from .models import ContactSubmission from apps.users.models import User try: contact = ContactSubmission.objects.get(id=contact_id) # Get all admin and moderator emails admin_emails = User.objects.filter( role__in=['admin', 'moderator'] ).values_list('email', flat=True) if not admin_emails: return "No admin emails found" # Render email template html_message = render_to_string('emails/contact_admin_notification.html', { 'ticket_number': contact.ticket_number, 'name': contact.name, 'email': contact.email, 'subject': contact.subject, 'category': contact.get_category_display(), 'message': contact.message, 'admin_url': f"{settings.SITE_URL}/admin/contact/contactsubmission/{contact.id}/change/", }) plain_message = strip_tags(html_message) # Send email send_mail( subject=f'New Contact Submission - Ticket #{contact.ticket_number}', message=plain_message, from_email=settings.DEFAULT_FROM_EMAIL, recipient_list=list(admin_emails), html_message=html_message, fail_silently=False, ) return f"Admin notification sent to {len(admin_emails)} admin(s)" except ContactSubmission.DoesNotExist: return f"Contact submission {contact_id} not found" except Exception as e: # Log error but don't fail the task print(f"Error sending admin notification: {str(e)}") raise @shared_task def send_contact_resolution_email(contact_id): """ Send email to user when their contact submission is resolved. Args: contact_id: UUID of the ContactSubmission """ from .models import ContactSubmission try: contact = ContactSubmission.objects.get(id=contact_id) if contact.status != 'resolved': return f"Contact {contact_id} is not resolved yet" # Render email template html_message = render_to_string('emails/contact_resolved.html', { 'name': contact.name, 'ticket_number': contact.ticket_number, 'subject': contact.subject, 'resolved_by': contact.resolved_by.username if contact.resolved_by else 'Support Team', }) plain_message = strip_tags(html_message) # Send email send_mail( subject=f'Your Support Ticket Has Been Resolved - #{contact.ticket_number}', message=plain_message, from_email=settings.DEFAULT_FROM_EMAIL, recipient_list=[contact.email], html_message=html_message, fail_silently=False, ) return f"Resolution email sent to {contact.email}" except ContactSubmission.DoesNotExist: return f"Contact submission {contact_id} not found" except Exception as e: # Log error but don't fail the task print(f"Error sending resolution email: {str(e)}") raise