feat: 添加PDF报告邮件发送功能

- 在core/tasks.py中添加celery_send_pdf_report_email任务,用于生成PDF报告并发送邮件
- 在test_redis_celery.py中添加对应的测试用例test_celery_redis_pdf_email
- 更新main函数中的测试计数和结果输出逻辑
This commit is contained in:
2026-01-19 21:36:23 +08:00
parent e2f389a325
commit 83bbcd8ff7
2 changed files with 273 additions and 10 deletions

View File

@@ -5,6 +5,9 @@ from django.core.mail.backends.smtp import EmailBackend
from django.utils import timezone
from datetime import timedelta
import traceback
import os
import re
from django.db.models import F
@shared_task(bind=True, max_retries=3, default_retry_delay=60)
@@ -327,3 +330,196 @@ def celery_send_html_report_email(self, include_attachment=False):
'error': error_msg,
'timestamp': timezone.now().isoformat()
}
@shared_task(bind=True, max_retries=3, default_retry_delay=60)
def celery_send_pdf_report_email(self):
"""
Celery异步生成PDF报告并发送邮件
1. 检查WeasyPrint是否可用
2. 生成PDF报告
3. 发送包含PDF附件的邮件
"""
task_id = self.request.id if hasattr(self, 'request') else 'unknown'
logger.info(f"[任务 {task_id}] 开始执行PDF报告邮件发送任务")
try:
# 检查WeasyPrint是否可用
weasyprint_available = False
try:
from weasyprint import HTML
weasyprint_available = True
except ImportError:
logger.error("[任务 {task_id}] WeasyPrint库无法导入PDF功能将不可用")
raise ValueError("WeasyPrint库无法导入PDF功能将不可用")
if not weasyprint_available:
raise ValueError("WeasyPrint库不可用无法生成PDF报告")
# 从数据库获取邮件配置
from core.models import SystemConfig, ReadingRecord, InsightRecord, TodayPlan, FamilyTask
from django.conf import settings
from django.template.loader import render_to_string
config = SystemConfig.get_config()
# 优先使用sender_email其次使用smtp_username作为发件人
from_email = config.sender_email or config.smtp_username
if not from_email:
raise ValueError("未配置发件邮箱")
# 验证发件人邮箱格式
email_pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
if not re.match(email_pattern, from_email):
raise ValueError(f"发件人邮箱格式不正确: {from_email}")
to_email = config.recipient_email or from_email
# 验证收件人邮箱格式
if not re.match(email_pattern, to_email):
raise ValueError(f"收件人邮箱格式不正确: {to_email}")
recipient_list = [to_email] if isinstance(to_email, str) else to_email
# 获取SMTP配置
host = config.smtp_server or 'localhost'
port = config.smtp_port or 587
username = config.smtp_username or ''
password = config.smtp_password or ''
use_tls = True # 默认使用TLS
# 准备报告数据
today = timezone.now().date()
yesterday = today - timedelta(days=1)
# 获取昨日记录
yesterday_reading = ReadingRecord.objects.filter(date=yesterday)
yesterday_insight = InsightRecord.objects.filter(date=yesterday)
# 获取今日计划
today_plan = TodayPlan.objects.filter(date=today)
# 获取家庭事项统计
family_task_stats = FamilyTask.objects.values('type').annotate(count=FamilyTask.objects.values('id').filter(type=F('type')).count())
# 准备上下文数据
context = {
'today': today,
'yesterday': yesterday,
'yesterday_reading': yesterday_reading,
'yesterday_insight': yesterday_insight,
'today_plan': today_plan,
'family_task_stats': family_task_stats,
}
# 渲染HTML模板
html_string = render_to_string('core/report_pdf.html', context)
# 生成PDF
today_str = today.strftime('%Y-%m-%d')
pdf_file = f"report_{today_str}.pdf"
pdf_path = os.path.join(settings.REPORTS_ROOT, pdf_file)
# 确保报告目录存在
os.makedirs(settings.REPORTS_ROOT, exist_ok=True)
# 使用WeasyPrint生成PDF
from weasyprint import HTML
HTML(string=html_string).write_pdf(pdf_path)
logger.info(f"[任务 {task_id}] PDF报告生成成功: {pdf_path}")
# 创建邮件后端
backend = EmailBackend(
host=host,
port=port,
username=username,
password=password,
use_tls=use_tls,
fail_silently=False
)
# 创建邮件
subject = f"[Celery] 家庭日报 {today_str} - PDF报告"
body = f"""
这是一封包含PDF报告的邮件。
报告日期: {today_str}
发送时间: {timezone.now().strftime('%Y-%m-%d %H:%M:%S')}
任务ID: {task_id}
报告包含以下内容:
- 昨日阅读记录 ({yesterday_reading.count()} 条)
- 昨日感悟记录 ({yesterday_insight.count()} 条)
- 今日计划 ({today_plan.count()} 项)
- 家庭事项统计
请查看附件获取完整报告。
---
家庭日报系统
自动发送Celery异步任务
"""
email = EmailMessage(
subject=subject,
body=body,
from_email=from_email,
to=recipient_list,
connection=backend
)
email.content_subtype = 'plain'
email.encoding = 'utf-8'
# 添加PDF附件
with open(pdf_path, 'rb') as f:
email.attach(pdf_file, f.read(), 'application/pdf')
logger.info(f"[任务 {task_id}] 正在发送包含PDF附件的邮件...")
# 发送邮件
sent_count = email.send(fail_silently=False)
if sent_count > 0:
logger.success(f"[任务 {task_id}] 包含PDF附件的邮件发送成功")
# 清理生成的PDF文件
if os.path.exists(pdf_path):
os.remove(pdf_path)
logger.info(f"[任务 {task_id}] 已清理生成的PDF文件: {pdf_path}")
return {
'status': 'success',
'task_id': task_id,
'type': 'pdf_report',
'sent_to': len(recipient_list),
'pdf_file': pdf_file,
'timestamp': timezone.now().isoformat()
}
else:
raise Exception("邮件发送返回0")
except Exception as e:
error_msg = str(e)
error_traceback = traceback.format_exc()
logger.error(f"[任务 {task_id}] PDF报告邮件发送失败: {error_msg}")
logger.error(f"[任务 {task_id}] 错误详情:\n{error_traceback}")
# 检查是否应该重试
if hasattr(self, 'request') and self.request.retries < self.max_retries:
logger.info(f"[任务 {task_id}] 准备第 {self.request.retries + 1} 次重试...")
# 重试延迟指数增长
retry_delay = 60 * (2 ** self.request.retries)
logger.info(f"[任务 {task_id}] {retry_delay}秒后进行重试")
raise self.retry(exc=e, countdown=retry_delay)
else:
logger.error(f"[任务 {task_id}] 已达到最大重试次数 {self.max_retries},放弃重试")
# 返回错误信息而不是抛出异常
return {
'status': 'failed',
'task_id': task_id,
'type': 'pdf_report',
'error': error_msg,
'retries': self.request.retries if hasattr(self, 'request') else 0,
'timestamp': timezone.now().isoformat()
}