refactor!: Deprecate ignore_permissions & flags in communication.make
API
This commit is contained in:
parent
525f1656ad
commit
da2dbfaae9
2 changed files with 99 additions and 24 deletions
|
|
@ -22,12 +22,30 @@ OUTGOING_EMAIL_ACCOUNT_MISSING = _("""
|
|||
|
||||
|
||||
@frappe.whitelist()
|
||||
def make(doctype=None, name=None, content=None, subject=None, sent_or_received = "Sent",
|
||||
sender=None, sender_full_name=None, recipients=None, communication_medium="Email", send_email=False,
|
||||
print_html=None, print_format=None, attachments='[]', send_me_a_copy=False, cc=None, bcc=None,
|
||||
flags=None, read_receipt=None, print_letterhead=True, email_template=None, communication_type=None,
|
||||
ignore_permissions=False) -> Dict[str, str]:
|
||||
"""Make a new communication.
|
||||
def make(
|
||||
doctype=None,
|
||||
name=None,
|
||||
content=None,
|
||||
subject=None,
|
||||
sent_or_received="Sent",
|
||||
sender=None,
|
||||
sender_full_name=None,
|
||||
recipients=None,
|
||||
communication_medium="Email",
|
||||
send_email=False,
|
||||
print_html=None,
|
||||
print_format=None,
|
||||
attachments="[]",
|
||||
send_me_a_copy=False,
|
||||
cc=None,
|
||||
bcc=None,
|
||||
read_receipt=None,
|
||||
print_letterhead=True,
|
||||
email_template=None,
|
||||
communication_type=None,
|
||||
**kwargs,
|
||||
) -> Dict[str, str]:
|
||||
"""Make a new communication. Checks for email permissions for specified Document.
|
||||
|
||||
:param doctype: Reference DocType.
|
||||
:param name: Reference Document name.
|
||||
|
|
@ -44,17 +62,69 @@ def make(doctype=None, name=None, content=None, subject=None, sent_or_received =
|
|||
:param send_me_a_copy: Send a copy to the sender (default **False**).
|
||||
:param email_template: Template which is used to compose mail .
|
||||
"""
|
||||
is_error_report = (doctype=="User" and name==frappe.session.user and subject=="Error Report")
|
||||
send_me_a_copy = cint(send_me_a_copy)
|
||||
if kwargs:
|
||||
from frappe.utils.commands import warn
|
||||
warn(
|
||||
f"Options {kwargs} used in frappe.core.doctype.communication.email.make "
|
||||
"are deprecated or unsupported",
|
||||
category=DeprecationWarning
|
||||
)
|
||||
|
||||
if not ignore_permissions:
|
||||
if doctype and name and not is_error_report and not frappe.has_permission(doctype, "email", name) and not (flags or {}).get('ignore_doctype_permissions'):
|
||||
raise frappe.PermissionError("You are not allowed to send emails related to: {doctype} {name}".format(
|
||||
doctype=doctype, name=name))
|
||||
if doctype and name and not frappe.has_permission(doctype=doctype, ptype="email", doc=name):
|
||||
raise frappe.PermissionError(
|
||||
f"You are not allowed to send emails related to: {doctype} {name}"
|
||||
)
|
||||
|
||||
if not sender:
|
||||
sender = get_formatted_email(frappe.session.user)
|
||||
return _make(
|
||||
doctype=doctype,
|
||||
name=name,
|
||||
content=content,
|
||||
subject=subject,
|
||||
sent_or_received=sent_or_received,
|
||||
sender=sender,
|
||||
sender_full_name=sender_full_name,
|
||||
recipients=recipients,
|
||||
communication_medium=communication_medium,
|
||||
send_email=send_email,
|
||||
print_html=print_html,
|
||||
print_format=print_format,
|
||||
attachments=attachments,
|
||||
send_me_a_copy=cint(send_me_a_copy),
|
||||
cc=cc,
|
||||
bcc=bcc,
|
||||
read_receipt=read_receipt,
|
||||
print_letterhead=print_letterhead,
|
||||
email_template=email_template,
|
||||
communication_type=communication_type,
|
||||
)
|
||||
|
||||
|
||||
def _make(
|
||||
doctype=None,
|
||||
name=None,
|
||||
content=None,
|
||||
subject=None,
|
||||
sent_or_received="Sent",
|
||||
sender=None,
|
||||
sender_full_name=None,
|
||||
recipients=None,
|
||||
communication_medium="Email",
|
||||
send_email=False,
|
||||
print_html=None,
|
||||
print_format=None,
|
||||
attachments="[]",
|
||||
send_me_a_copy=False,
|
||||
cc=None,
|
||||
bcc=None,
|
||||
read_receipt=None,
|
||||
print_letterhead=True,
|
||||
email_template=None,
|
||||
communication_type=None,
|
||||
) -> Dict[str, str]:
|
||||
"""Internal method to make a new communication that ignores Permission checks.
|
||||
"""
|
||||
|
||||
sender = sender or get_formatted_email(frappe.session.user)
|
||||
recipients = list_to_str(recipients) if isinstance(recipients, list) else recipients
|
||||
cc = list_to_str(cc) if isinstance(cc, list) else cc
|
||||
bcc = list_to_str(bcc) if isinstance(bcc, list) else bcc
|
||||
|
|
@ -87,17 +157,21 @@ def make(doctype=None, name=None, content=None, subject=None, sent_or_received =
|
|||
|
||||
if cint(send_email):
|
||||
if not comm.get_outgoing_email_account():
|
||||
frappe.throw(msg=OUTGOING_EMAIL_ACCOUNT_MISSING, exc=frappe.OutgoingEmailError)
|
||||
frappe.throw(
|
||||
msg=OUTGOING_EMAIL_ACCOUNT_MISSING, exc=frappe.OutgoingEmailError
|
||||
)
|
||||
|
||||
comm.send_email(print_html=print_html, print_format=print_format,
|
||||
send_me_a_copy=send_me_a_copy, print_letterhead=print_letterhead)
|
||||
comm.send_email(
|
||||
print_html=print_html,
|
||||
print_format=print_format,
|
||||
send_me_a_copy=send_me_a_copy,
|
||||
print_letterhead=print_letterhead,
|
||||
)
|
||||
|
||||
emails_not_sent_to = comm.exclude_emails_list(include_sender=send_me_a_copy)
|
||||
|
||||
return {
|
||||
"name": comm.name,
|
||||
"emails_not_sent_to": ", ".join(emails_not_sent_to)
|
||||
}
|
||||
return {"name": comm.name, "emails_not_sent_to": ", ".join(emails_not_sent_to)}
|
||||
|
||||
|
||||
def validate_email(doc: "Communication") -> None:
|
||||
"""Validate Email Addresses of Recipients and CC"""
|
||||
|
|
|
|||
|
|
@ -186,7 +186,7 @@ def get_context(context):
|
|||
|
||||
def send_an_email(self, doc, context):
|
||||
from email.utils import formataddr
|
||||
from frappe.core.doctype.communication.email import make as make_communication
|
||||
from frappe.core.doctype.communication.email import _make as make_communication
|
||||
subject = self.subject
|
||||
if "{" in subject:
|
||||
subject = frappe.render_template(self.subject, context)
|
||||
|
|
@ -216,7 +216,8 @@ def get_context(context):
|
|||
# Add mail notification to communication list
|
||||
# No need to add if it is already a communication.
|
||||
if doc.doctype != 'Communication':
|
||||
make_communication(doctype=doc.doctype,
|
||||
make_communication(
|
||||
doctype=doc.doctype,
|
||||
name=doc.name,
|
||||
content=message,
|
||||
subject=subject,
|
||||
|
|
@ -228,7 +229,7 @@ def get_context(context):
|
|||
cc=cc,
|
||||
bcc=bcc,
|
||||
communication_type='Automated Message',
|
||||
ignore_permissions=True)
|
||||
)
|
||||
|
||||
def send_a_slack_msg(self, doc, context):
|
||||
send_slack_message(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue