diff --git a/ckanext/mailcraft/config.py b/ckanext/mailcraft/config.py index 49d8936..f48f3b3 100644 --- a/ckanext/mailcraft/config.py +++ b/ckanext/mailcraft/config.py @@ -12,6 +12,9 @@ CONF_MAIL_PER_PAGE = "ckanext.mailcraft.mail_per_page" DEF_MAIL_PER_PAGE = 20 +CONF_SAVE_TO_DASHBOARD = "ckanext.mailcraft.save_to_dashboard" +DEF_SAVE_TO_DASHBOARD = False + def get_conn_timeout() -> int: """Return a timeout for an SMTP connection""" @@ -33,3 +36,8 @@ def stop_outgoing_emails() -> bool: def get_mail_per_page() -> int: """Return a number of mails to show per page""" return tk.asint(tk.config.get(CONF_MAIL_PER_PAGE, DEF_MAIL_PER_PAGE)) + + +def is_save_to_dashboard_enabled() -> bool: + """Check if we are saving outgoing emails to dashboard""" + return tk.asbool(tk.config.get(CONF_SAVE_TO_DASHBOARD, DEF_SAVE_TO_DASHBOARD)) diff --git a/ckanext/mailcraft/config_declaration.yaml b/ckanext/mailcraft/config_declaration.yaml index e641423..7c5477a 100644 --- a/ckanext/mailcraft/config_declaration.yaml +++ b/ckanext/mailcraft/config_declaration.yaml @@ -21,3 +21,8 @@ groups: type: int description: Specify a number of mails to show per page on a dashboard default: 20 + + - key: ckanext.mailcraft.save_to_dashboard + type: bool + description: Specify do we need to save outgoing emails to dashboard + default: false diff --git a/ckanext/mailcraft/mailer.py b/ckanext/mailcraft/mailer.py index 68ff310..eae267b 100644 --- a/ckanext/mailcraft/mailer.py +++ b/ckanext/mailcraft/mailer.py @@ -90,6 +90,18 @@ def create_reset_key(self, user: model.User) -> None: def verify_reset_link(self, user: model.User, key: Optional[str]) -> bool: pass + def save_to_dashboard( + self, + msg: EmailMessage, + body_html: str, + state: str = mc_model.Email.State.success, + extras: Optional[dict[str, Any]] = None, + ) -> None: + if not mc_config.is_save_to_dashboard_enabled(): + return + + mc_model.Email.save_mail(msg, body_html, state, extras or {}) + class DefaultMailer(Mailer): def mail_recipients( @@ -130,18 +142,18 @@ def mail_recipients( try: if mc_config.stop_outgoing_emails(): - self._save_email( + self.save_to_dashboard( msg, body_html, mc_model.Email.State.stopped, dict(msg.items()) ) else: self._send_email(recipients, msg) except MailerException: - self._save_email( + self.save_to_dashboard( msg, body_html, mc_model.Email.State.failed, dict(msg.items()) ) else: if not mc_config.stop_outgoing_emails(): - self._save_email(msg, body_html) + self.save_to_dashboard(msg, body_html) def add_attachments(self, msg: EmailMessage, attachments) -> None: """Add attachments on an email message @@ -198,15 +210,6 @@ def get_connection(self) -> smtplib.SMTP: return conn - def _save_email( - self, - msg: EmailMessage, - body_html: str, - state: str = mc_model.Email.State.success, - extras: Optional[dict[str, Any]] = None, - ) -> None: - mc_model.Email.save_mail(msg, body_html, state, extras or {}) - def _send_email(self, recipients, msg: EmailMessage): conn = self.get_connection()