|
8 | 8 | from plone import api |
9 | 9 | from datetime import datetime |
10 | 10 | from redturtle.rsync.scripts.rsync import logger |
| 11 | +from email.message import EmailMessage |
| 12 | +from email.utils import formataddr |
| 13 | +from plone.registry.interfaces import IRegistry |
| 14 | +from Products.CMFPlone.interfaces.controlpanel import IMailSchema |
| 15 | +from zope.component import getUtility |
11 | 16 |
|
12 | 17 | import json |
13 | 18 | import requests |
@@ -51,6 +56,7 @@ def __init__(self, context, request): |
51 | 56 | self.sync_uids = set() |
52 | 57 | self.start = datetime.now() |
53 | 58 | self.end = None |
| 59 | + self.send_log_template = None |
54 | 60 |
|
55 | 61 | def requests_retry_session( |
56 | 62 | self, |
@@ -113,11 +119,13 @@ def log_info(self, msg, type="info", force_sys_log=False): |
113 | 119 | """ |
114 | 120 | style = "" |
115 | 121 | if type == "error": |
116 | | - style = "padding:5px;background-color:red;color:#fff" |
| 122 | + style = "padding:2px;background-color:red;color:#fff" |
117 | 123 | if type == "warning": |
118 | | - style = "padding:5px;background-color:#ff9d00;color:#fff" |
119 | | - msg = f"[{datetime.now().strftime('%d-%m-%Y %H:%M:%S')}] {msg}" |
120 | | - self.logdata.append(f'<p style="{style}">{self.autolink(msg)}</p>') |
| 124 | + style = "padding:2px;background-color:#ff9d00;color:#fff" |
| 125 | + # msg = f"[{datetime.now().strftime('%d-%m-%Y %H:%M:%S')}] {msg}" |
| 126 | + self.logdata.append( |
| 127 | + f'<p><span style="{style}">[{datetime.now().strftime("%d-%m-%Y %H:%M:%S")}]</span> {self.autolink(msg)}</p>' |
| 128 | + ) |
121 | 129 |
|
122 | 130 | # print the message on standard output |
123 | 131 | if type == "error": |
@@ -166,6 +174,46 @@ def write_log(self): |
166 | 174 | }, |
167 | 175 | ) |
168 | 176 |
|
| 177 | + def send_log(self): |
| 178 | + """ |
| 179 | + Send the log by email. |
| 180 | + """ |
| 181 | + |
| 182 | + send_to_email = getattr(self.options, "send_to_email", None) |
| 183 | + if not send_to_email: |
| 184 | + return |
| 185 | + if not self.send_log_template: |
| 186 | + logger.warning("No email template found, skipping log send by email.") |
| 187 | + return |
| 188 | + mailhost = api.portal.get_tool(name="MailHost") |
| 189 | + if not mailhost: |
| 190 | + logger.warning("No MailHost found, skipping log send by email.") |
| 191 | + return |
| 192 | + |
| 193 | + body_view = api.content.get_view( |
| 194 | + name=self.send_log_template, context=self.context, request=self.request |
| 195 | + ) |
| 196 | + body = body_view(logs=self.logdata) |
| 197 | + encoding = api.portal.get_registry_record( |
| 198 | + "plone.email_charset", default="utf-8" |
| 199 | + ) |
| 200 | + |
| 201 | + registry = getUtility(IRegistry) |
| 202 | + mail_settings = registry.forInterface(IMailSchema, prefix="plone") |
| 203 | + email_from_address = mail_settings.email_from_address |
| 204 | + email_from_name = mail_settings.email_from_name |
| 205 | + mfrom = formataddr((email_from_name, email_from_address)) |
| 206 | + |
| 207 | + msg = EmailMessage() |
| 208 | + msg.set_content(body) |
| 209 | + msg["Subject"] = self.log_item_title(start=self.start) |
| 210 | + msg["From"] = mfrom |
| 211 | + msg["Reply-To"] = mfrom |
| 212 | + msg["To"] = send_to_email |
| 213 | + msg.replace_header("Content-Type", 'text/html; charset="utf-8"') |
| 214 | + |
| 215 | + mailhost.send(msg, charset=encoding) |
| 216 | + |
169 | 217 | def set_args(self, parser): |
170 | 218 | """ |
171 | 219 | Set some additional arguments for the rsync command. |
|
0 commit comments