feat(report): 添加定时生成PDF报告功能并重构邮件发送任务
将PDF生成逻辑从邮件发送任务中分离,新增独立定时任务 更新README文档说明PDF生成配置和使用方法
This commit is contained in:
@@ -32,9 +32,9 @@ from .models import (
|
||||
)
|
||||
|
||||
@shared_task
|
||||
def send_daily_report():
|
||||
"""发送每日报告"""
|
||||
logger.info("开始执行每日报告发送任务")
|
||||
def generate_daily_pdf_report():
|
||||
"""生成每日PDF报告"""
|
||||
logger.info("开始执行每日PDF报告生成任务")
|
||||
|
||||
# 检查WeasyPrint是否可用
|
||||
if not is_weasyprint_available():
|
||||
@@ -44,14 +44,6 @@ def send_daily_report():
|
||||
today = timezone.now().date()
|
||||
today_str = today.strftime('%Y-%m-%d')
|
||||
|
||||
# 获取系统配置
|
||||
config = SystemConfig.get_config()
|
||||
|
||||
# 检查邮件配置是否完整
|
||||
if not all([config.smtp_server, config.smtp_username, config.smtp_password, config.recipient_email]):
|
||||
logger.error("邮件配置不完整,无法发送邮件")
|
||||
return False
|
||||
|
||||
# 生成报告数据
|
||||
report_date = today
|
||||
yesterday = report_date - timedelta(days=1)
|
||||
@@ -92,9 +84,32 @@ def send_daily_report():
|
||||
from weasyprint import HTML
|
||||
HTML(string=html_string).write_pdf(pdf_path)
|
||||
logger.info(f"PDF报告生成成功: {pdf_path}")
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"PDF报告生成失败: {str(e)}")
|
||||
return False
|
||||
|
||||
@shared_task
|
||||
def send_daily_report():
|
||||
"""发送每日报告"""
|
||||
logger.info("开始执行每日报告发送任务")
|
||||
|
||||
# 先生成PDF报告
|
||||
pdf_generated = generate_daily_pdf_report()
|
||||
if not pdf_generated:
|
||||
logger.error("PDF报告生成失败,无法发送邮件")
|
||||
return False
|
||||
|
||||
today = timezone.now().date()
|
||||
today_str = today.strftime('%Y-%m-%d')
|
||||
|
||||
# 获取系统配置
|
||||
config = SystemConfig.get_config()
|
||||
|
||||
# 检查邮件配置是否完整
|
||||
if not all([config.smtp_server, config.smtp_username, config.smtp_password, config.recipient_email]):
|
||||
logger.error("邮件配置不完整,无法发送邮件")
|
||||
return False
|
||||
|
||||
# 发送邮件
|
||||
subject = f"家庭日报 - {today_str}"
|
||||
@@ -102,6 +117,10 @@ def send_daily_report():
|
||||
from_email = config.smtp_username
|
||||
recipient_list = [config.recipient_email]
|
||||
|
||||
# PDF文件路径
|
||||
pdf_file = f"report_{today_str}.pdf"
|
||||
pdf_path = os.path.join(settings.REPORTS_ROOT, pdf_file)
|
||||
|
||||
try:
|
||||
email = EmailMessage(
|
||||
subject=subject,
|
||||
|
||||
Reference in New Issue
Block a user