fix(email): Maintain multi-tenancy translation sanity in error messages

Defining translated strings as globals means the strings are evaluated
at import time. So the value of these variables is set based on which
site or user initializes the module. This makes it possible for  an
english user to get Russian errors.

---

Other changes: Remove unreachable code, avoid importing _socket.error alias to
OSError
This commit is contained in:
Gavin D'souza 2022-06-07 13:03:19 +05:30
parent df3601b67b
commit 1b1479ef37
4 changed files with 25 additions and 39 deletions

View file

@ -298,8 +298,6 @@ def apply(doc=None, method=None, doctype=None, name=None):
if reopened:
break
# print(f"Rule:{assignment_rule}\nDoc: {doc}\nReOpened: {reopened}")
assignment_rule.close_assignments(doc)

View file

@ -22,14 +22,6 @@ if TYPE_CHECKING:
from frappe.core.doctype.communication.communication import Communication
OUTGOING_EMAIL_ACCOUNT_MISSING = _(
"""
Unable to send mail because of a missing email account.
Please setup default Email Account from Setup > Email > Email Account
"""
)
@frappe.whitelist()
def make(
doctype=None,
@ -170,7 +162,12 @@ def _make(
if cint(send_email):
if not comm.get_outgoing_email_account():
frappe.throw(msg=OUTGOING_EMAIL_ACCOUNT_MISSING, exc=frappe.OutgoingEmailError)
frappe.throw(
_(
"Unable to send mail because of a missing email account. Please setup default Email Account from Setup > Email > Email Account"
),
exc=frappe.OutgoingEmailError,
)
comm.send_email(
print_html=print_html,

View file

@ -23,10 +23,6 @@ from frappe.utils.error import raise_error_on_no_output
from frappe.utils.jinja import render_template
from frappe.utils.user import get_system_managers
OUTGOING_EMAIL_ACCOUNT_MISSING = _(
"Please setup default Email Account from Setup > Email > Email Account"
)
class SentEmailInInbox(Exception):
pass
@ -319,7 +315,7 @@ class EmailAccount(Document):
@classmethod
@raise_error_on_no_output(
keep_quiet=lambda: not cint(frappe.get_system_settings("setup_complete")),
error_message=OUTGOING_EMAIL_ACCOUNT_MISSING,
error_message=_("Please setup default Email Account from Setup > Email > Email Account"),
error_type=frappe.OutgoingEmailError,
) # noqa
@cache_email_account("outgoing_email_account")

View file

@ -1,24 +1,12 @@
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and Contributors
# License: MIT. See LICENSE
import smtplib
import _socket
import frappe
from frappe import _
from frappe.utils import cint, cstr
CONNECTION_FAILED = _("Could not connect to outgoing email server")
AUTH_ERROR_TITLE = _("Invalid Credentials")
AUTH_ERROR = _("Incorrect email or password. Please check your login credentials.")
SOCKET_ERROR_TITLE = _("Incorrect Configuration")
SOCKET_ERROR = _("Invalid Outgoing Mail Server or Port")
SEND_MAIL_FAILED = _("Unable to send emails at this time")
EMAIL_ACCOUNT_MISSING = _(
"Email Account not setup. Please create a new Email Account from Setup > Email > Email Account"
)
class InvalidEmailCredentials(frappe.ValidationError):
pass
@ -65,7 +53,12 @@ class SMTPServer:
self._session = None
if not self.server:
frappe.msgprint(EMAIL_ACCOUNT_MISSING, raise_exception=frappe.OutgoingEmailError)
frappe.msgprint(
_(
"Email Account not setup. Please create a new Email Account from Setup > Email > Email Account"
),
raise_exception=frappe.OutgoingEmailError,
)
@property
def port(self):
@ -93,7 +86,9 @@ class SMTPServer:
try:
_session = SMTP(self.server, self.port)
if not _session:
frappe.msgprint(CONNECTION_FAILED, raise_exception=frappe.OutgoingEmailError)
frappe.msgprint(
_("Could not connect to outgoing email server"), raise_exception=frappe.OutgoingEmailError
)
self.secure_session(_session)
if self.login and self.password:
@ -106,16 +101,12 @@ class SMTPServer:
self._session = _session
return self._session
except smtplib.SMTPAuthenticationError as e:
except smtplib.SMTPAuthenticationError:
self.throw_invalid_credentials_exception()
except _socket.error as e:
except OSError:
# Invalid mail server -- due to refusing connection
frappe.throw(SOCKET_ERROR, title=SOCKET_ERROR_TITLE)
except smtplib.SMTPException:
frappe.msgprint(SEND_MAIL_FAILED)
raise
frappe.throw(_("Invalid Outgoing Mail Server or Port"), title=_("Incorrect Configuration"))
def is_session_active(self):
if self._session:
@ -130,4 +121,8 @@ class SMTPServer:
@classmethod
def throw_invalid_credentials_exception(cls):
frappe.throw(AUTH_ERROR, title=AUTH_ERROR_TITLE, exc=InvalidEmailCredentials)
frappe.throw(
_("Incorrect email or password. Please check your login credentials."),
title=_("Invalid Credentials"),
exc=InvalidEmailCredentials,
)