ferrero-opentext/Python-Version/scripts/shared/notifier.py
DJP 65f2c9c68e Add Box file deletion, email notifications, and log rotation for A2→A3
Major Features Added:

1. Delete Files from Box After Upload
   - After successful DAM upload, delete file from Box
   - Prevents reprocessing same files
   - Keeps Box folder clean
   - Only deletes on success (keeps on failure for retry)

2. Email Notification for Each Upload
   - New template: a2_to_a3_file_uploaded
   - Sends email immediately after each successful upload
   - Includes: filename, clean filename, asset ID, tracking ID
   - Don't wait for "all done" - notify per file
   - Recipients: configured in .env (REPORT_EMAILS)

3. Log Rotation for Both Scripts
   - Uses RotatingFileHandler
   - Max file size: 10MB per log file
   - Backup count: 28 files (approximately 1 month)
   - Auto-rotates when log reaches 10MB
   - Keeps logs/a1_to_a2.log (current)
   - Backups: logs/a1_to_a2.log.1, .2, .3, etc.
   - Automatically deletes logs older than 28 rotations
   - Applied to both A1→A2 and A2→A3 scripts

Flow Changes:
A2→A3 now:
1. Poll Box folder
2. Find V2 files
3. Download from Box
4. Upload to DAM
5. Delete from Box  NEW
6. Send email notification  NEW
7. Store derivative record
8. Exit

Log Management:
- Active logs: ~10MB max
- Rotated backups: 28 files = ~280MB total
- Automatic cleanup (no manual intervention needed)
- 1 week of detailed logs + 3 weeks of backups

Database:
- Added dam_asset_id and upload_status columns to derivative_assets
- Fixed store_derivative_asset() to use existing schema columns

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-30 19:41:02 -04:00

198 lines
8.1 KiB
Python

"""
Notifier - Email and Webhook Notifications
Handles SMTP emails (Mailgun) and outgoing webhooks
Compatible with Python 3.6+
"""
import requests
import logging
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from jinja2 import Template
logger = logging.getLogger('Notifier')
class Notifier:
def __init__(self, config):
self.config = config
self.enabled = config['notifications']['enabled']
# SMTP configuration (preferred method)
smtp_config = config['notifications'].get('smtp', {})
self.smtp_server = smtp_config.get('server')
self.smtp_port = smtp_config.get('port', 587)
self.smtp_user = smtp_config.get('user')
self.smtp_password = smtp_config.get('password')
self.sender_email = smtp_config.get('sender_email')
self.recipients = config['notifications']['recipients']
self.webhook_config = config.get('webhooks', {})
def send_email(self, template_name, recipients, data):
"""
Send email via SMTP (Mailgun)
Args:
template_name: Name of email template
recipients: List of email addresses or single email
data: Template data dict
"""
if not self.enabled:
logger.info("Notifications disabled, skipping email")
return
if not self.smtp_server or not self.smtp_user:
logger.warning("SMTP not configured, skipping email")
return
try:
# Simple templates (full template system would load from YAML)
templates = {
'a1_to_a2_complete': {
'subject': "✅ Master Assets Downloaded - Campaign {campaign_name}",
'html': """
<h2>Master Assets Downloaded Successfully</h2>
<p><strong>Campaign:</strong> {{ campaign_name }} ({{ campaign_id }})</p>
<p><strong>Campaign Number:</strong> {{ campaign_number }}</p>
<p><strong>Assets Downloaded:</strong> {{ asset_count }}</p>
<p><strong>Status Updated:</strong> A1 → A2</p>
<hr>
<p>All assets have been downloaded and uploaded to Box with tracking IDs.</p>
"""
},
'a2_to_a3_complete': {
'subject': "✅ Localized Assets Uploaded - Campaign {campaign_name}",
'html': """
<h2>Localized Assets Uploaded Successfully</h2>
<p><strong>Campaign:</strong> {{ campaign_name }}</p>
<p><strong>Campaign ID:</strong> {{ campaign_id }}</p>
<p><strong>Assets Uploaded:</strong> {{ asset_count }}</p>
<p><strong>Status Updated:</strong> A2 → A3</p>
<hr>
<p>All localized assets have been uploaded to DAM.</p>
"""
},
'upload_failed': {
'subject': "❌ Upload Failed - {filename}",
'html': """
<h2 style="color: red;">Upload Failed</h2>
<p><strong>Filename:</strong> {{ filename }}</p>
<p><strong>Tracking ID:</strong> {{ tracking_id }}</p>
<p><strong>Error:</strong> {{ error }}</p>
<hr>
<p>Please investigate the error.</p>
"""
},
'a1_to_a2_partial': {
'subject': "⚠️ Partial Download - Campaign {campaign_name}",
'html': """
<h2 style="color: orange;">Campaign Partially Processed</h2>
<p><strong>Campaign:</strong> {{ campaign_name }} ({{ campaign_id }})</p>
<p><strong>Total Assets:</strong> {{ total_assets }}</p>
<p><strong>Successful:</strong> {{ successful }}</p>
<p><strong>Failed:</strong> {{ failed }}</p>
<hr>
<p style="color: red;"><strong>Status NOT updated.</strong> Campaign remains at A1.</p>
<p>Please review failed assets and retry.</p>
"""
},
'a2_to_a3_file_uploaded': {
'subject': "✅ Asset Uploaded to DAM - {clean_filename}",
'html': """
<h2>Asset Uploaded Successfully</h2>
<p><strong>Original Filename:</strong> {{ filename }}</p>
<p><strong>Clean Filename:</strong> {{ clean_filename }}</p>
<p><strong>DAM Asset ID:</strong> {{ asset_id }}</p>
<p><strong>Tracking ID:</strong> {{ tracking_id }}</p>
<hr>
<p>Asset has been processed and uploaded to the DAM Final Assets folder.</p>
<p><strong>Note:</strong> File has been deleted from Box processing folder.</p>
"""
}
}
template_config = templates.get(template_name, {
'subject': 'Ferrero Automation Notification',
'html': '<p>{}</p>'.format(data)
})
# Render subject and body
subject = template_config['subject'].format(**data)
html_template = Template(template_config['html'])
html_body = html_template.render(**data)
# Prepare email
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = self.sender_email
msg['To'] = ', '.join(recipients) if isinstance(recipients, list) else recipients
# Attach HTML body
html_part = MIMEText(html_body, 'html')
msg.attach(html_part)
# Send via SMTP
with smtplib.SMTP(self.smtp_server, self.smtp_port) as server:
server.starttls()
server.login(self.smtp_user, self.smtp_password)
server.send_message(msg)
logger.info("Email sent via SMTP: {} to {}".format(template_name, recipients))
except Exception as e:
logger.error("Email error: {}".format(str(e)))
def send_webhook(self, url, payload):
"""
Send outgoing webhook notification
Args:
url: Webhook URL
payload: dict to send as JSON
Returns:
bool: Success status
"""
try:
# Get webhook config if exists
webhook_config = None
for name, config in self.webhook_config.items():
if config.get('url') == url:
webhook_config = config
break
if not webhook_config:
webhook_config = {'timeout_seconds': 10, 'auth': {}}
# Prepare headers
headers = {'Content-Type': 'application/json'}
# Add auth if configured
auth_config = webhook_config.get('auth', {})
if auth_config.get('type') == 'bearer' and auth_config.get('token'):
headers['Authorization'] = 'Bearer {}'.format(auth_config['token'])
elif auth_config.get('type') == 'basic':
# Could add basic auth here if needed
pass
# Send webhook
response = requests.post(
url,
json=payload,
headers=headers,
timeout=webhook_config.get('timeout_seconds', 10)
)
if response.status_code in [200, 201, 202]:
logger.info("Webhook sent successfully: {}".format(url))
return True
else:
logger.warning("Webhook failed: HTTP {} - {}".format(
response.status_code, response.text[:200]
))
return False
except Exception as e:
logger.error("Webhook error: {}".format(str(e)))
return False