92 lines
2.7 KiB
Python
92 lines
2.7 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Send a one-off Daily Digest email for a specific date using the app's template.
|
|
|
|
Default date: 14.10.2025 (dd.mm.yyyy)
|
|
Default recipient: suriyakumar.vijayanayagam@gmail.com
|
|
|
|
Usage examples:
|
|
python scripts/send_specific_report.py
|
|
python scripts/send_specific_report.py --date 14.10.2025 --to you@example.com
|
|
"""
|
|
|
|
import argparse
|
|
import os
|
|
import sys
|
|
from datetime import datetime
|
|
from zoneinfo import ZoneInfo
|
|
|
|
# Ensure project root on PYTHONPATH when running from scripts/
|
|
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
if PROJECT_ROOT not in sys.path:
|
|
sys.path.insert(0, PROJECT_ROOT)
|
|
|
|
from app_core.config.settings import AppSettings
|
|
from app_core.services.mailer_service import MailerService
|
|
|
|
|
|
def parse_args() -> argparse.Namespace:
|
|
parser = argparse.ArgumentParser(description="Send a daily digest for a specific date")
|
|
parser.add_argument(
|
|
"--date",
|
|
help="Target date in dd.mm.yyyy format (e.g., 14.10.2025)",
|
|
default="14.10.2025",
|
|
)
|
|
parser.add_argument(
|
|
"--to",
|
|
help="Recipient email (comma-separated for multiple)",
|
|
default="suriyakumar.vijayanayagam@gmail.com",
|
|
)
|
|
return parser.parse_args()
|
|
|
|
|
|
def parse_ddmmyyyy(value: str) -> datetime.date:
|
|
try:
|
|
return datetime.strptime(value, "%d.%m.%Y").date()
|
|
except ValueError as ex:
|
|
raise SystemExit(f"Invalid date '{value}'. Use dd.mm.yyyy (e.g., 14.10.2025)") from ex
|
|
|
|
|
|
def main() -> int:
|
|
args = parse_args()
|
|
target_date = parse_ddmmyyyy(args.date)
|
|
recipients = [e.strip() for e in args.to.split(",") if e.strip()]
|
|
|
|
print("=" * 60)
|
|
print(f"SENDING Daily Digest for {target_date} to: {', '.join(recipients)}")
|
|
print("=" * 60)
|
|
|
|
settings = AppSettings()
|
|
service = MailerService(settings)
|
|
|
|
# Fetch rows for the date
|
|
df = service.fetch_daily_rows(target_date)
|
|
if df.empty:
|
|
print(f"❌ No rows found for {target_date}. Nothing to send.")
|
|
return 1
|
|
|
|
# Build HTML using first row context + full DataFrame for per-store summary
|
|
row = df.iloc[0].to_dict()
|
|
html = service.build_email_html(row, df)
|
|
|
|
subject = f"Daily Digest - {target_date}"
|
|
print(f"Subject: {subject}")
|
|
|
|
ok, msg = service.send_email(recipients, subject, html)
|
|
if ok:
|
|
print("Email sent successfully")
|
|
service.log_email(recipients, subject, str(target_date), "sent", None)
|
|
print("Logged in database")
|
|
return 0
|
|
else:
|
|
print(f"Email failed: {msg}")
|
|
service.log_email(recipients, subject, str(target_date), "failed", msg)
|
|
print("Failure logged in database")
|
|
return 2
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|
|
|
|
|