Skip to content

Commit

Permalink
feat: allow creating Days Before / After notifications for child ta…
Browse files Browse the repository at this point in the history
…ble (frappe#26982)
  • Loading branch information
sagarvora authored Jul 11, 2024
1 parent c199a76 commit b193cde
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 40 deletions.
36 changes: 20 additions & 16 deletions frappe/email/doctype/notification/notification.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) 2018, Frappe Technologies and contributors
// For license information, please see license.txt

const DATE_BASED_EVENTS = ["Days Before", "Days After"];

frappe.notification = {
setup_fieldname_select: function (frm) {
// get the doctype to update fields
Expand Down Expand Up @@ -129,6 +131,8 @@ Last comment: {{ comments[-1].comment }} by {{ comments[-1].by }}
frappe.ui.form.on("Notification", {
onload: function (frm) {
frm.set_query("document_type", function () {
if (DATE_BASED_EVENTS.includes(frm.doc.event)) return;

return {
filters: {
istable: 0,
Expand Down Expand Up @@ -166,23 +170,23 @@ frappe.ui.form.on("Notification", {
frappe.set_route("Form", "Customize Form");
},
event: function (frm) {
if (["Days Before", "Days After"].includes(frm.doc.event)) {
frm.add_custom_button(__("Get Alerts for Today"), function () {
frappe.call({
method: "frappe.email.doctype.notification.notification.get_documents_for_today",
args: {
notification: frm.doc.name,
},
callback: function (r) {
if (r.message && r.message.length > 0) {
frappe.msgprint(r.message.toString());
} else {
frappe.msgprint(__("No alerts for today"));
}
},
});
if (!DATE_BASED_EVENTS.includes(frm.doc.event) || frm.is_new()) return;

frm.add_custom_button(__("Get Alerts for Today"), function () {
frappe.call({
method: "frappe.email.doctype.notification.notification.get_documents_for_today",
args: {
notification: frm.doc.name,
},
callback: function (r) {
if (r.message && r.message.length > 0) {
frappe.msgprint(r.message.toString());
} else {
frappe.msgprint(__("No alerts for today"));
}
},
});
}
});
},
channel: function (frm) {
frm.toggle_reqd("recipients", frm.doc.channel == "Email");
Expand Down
11 changes: 5 additions & 6 deletions frappe/email/doctype/notification/notification.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
"engine": "InnoDB",
"field_order": [
"enabled",
"is_standard",
"module",
"column_break_2",
"channel",
"slack_webhook_url",
"filters",
"subject",
"event",
"document_type",
"is_standard",
"module",
"col_break_1",
"event",
"method",
"date_changed",
"days_in_advance",
Expand Down Expand Up @@ -119,7 +119,6 @@
"fieldtype": "Column Break"
},
{
"depends_on": "eval: doc.document_type",
"fieldname": "event",
"fieldtype": "Select",
"in_list_view": 1,
Expand Down Expand Up @@ -292,7 +291,7 @@
"icon": "fa fa-envelope",
"index_web_pages_for_search": 1,
"links": [],
"modified": "2024-06-17 04:03:22.591781",
"modified": "2024-07-04 05:53:40.595130",
"modified_by": "Administrator",
"module": "Email",
"name": "Notification",
Expand All @@ -315,4 +314,4 @@
"states": [],
"title_field": "subject",
"track_changes": 1
}
}
48 changes: 30 additions & 18 deletions frappe/email/doctype/notification/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from frappe.utils.safe_exec import get_safe_globals

FORMATS = {"HTML": ".html", "Markdown": ".md", "Plain Text": ".txt"}
FORBIDDEN_DOCUMENT_TYPES = frozenset(("Email Queue",))
DATE_BASED_EVENTS = frozenset(("Days Before", "Days After"))


class Notification(Document):
Expand All @@ -27,9 +29,7 @@ class Notification(Document):
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from frappe.email.doctype.notification_recipient.notification_recipient import (
NotificationRecipient,
)
from frappe.email.doctype.notification_recipient.notification_recipient import NotificationRecipient
from frappe.types import DF

attach_print: DF.Check
Expand Down Expand Up @@ -90,7 +90,7 @@ def validate(self):
if self.event == "Value Change" and not self.value_changed:
frappe.throw(_("Please specify which value field must be checked"))

self.validate_forbidden_types()
self.validate_forbidden_document_types()
self.validate_condition()
self.validate_standard()
frappe.cache.hdel("notifications", self.document_type)
Expand Down Expand Up @@ -130,12 +130,16 @@ def validate_condition(self):
except Exception:
frappe.throw(_("The Condition '{0}' is invalid").format(self.condition))

def validate_forbidden_types(self):
forbidden_document_types = ("Email Queue",)
if self.document_type in forbidden_document_types or frappe.get_meta(self.document_type).istable:
# currently notifications don't work on child tables as events are not fired for each record of child table

frappe.throw(_("Cannot set Notification on Document Type {0}").format(self.document_type))
def validate_forbidden_document_types(self):
if self.document_type in FORBIDDEN_DOCUMENT_TYPES or (
frappe.get_meta(self.document_type).istable and self.event not in DATE_BASED_EVENTS
):
# only date based events are allowed for child tables
frappe.throw(
_("Cannot set Notification with event {0} on Document Type {1}").format(
_(self.event), _(self.document_type)
)
)

def get_documents_for_today(self):
"""get list of documents that will be triggered today"""
Expand Down Expand Up @@ -237,8 +241,8 @@ def create_system_notification(self, doc, context):

notification_doc = {
"type": "Alert",
"document_type": doc.doctype,
"document_name": doc.name,
"document_type": get_reference_doctype(doc),
"document_name": get_reference_name(doc),
"subject": subject,
"from_user": doc.modified_by or doc.owner,
"email_content": frappe.render_template(self.message, context),
Expand Down Expand Up @@ -270,8 +274,8 @@ def send_an_email(self, doc, context):
# No need to add if it is already a communication.
if doc.doctype != "Communication":
communication = make_communication(
doctype=doc.doctype,
name=doc.name,
doctype=get_reference_doctype(doc),
name=get_reference_name(doc),
content=message,
subject=subject,
sender=sender,
Expand All @@ -294,8 +298,8 @@ def send_an_email(self, doc, context):
cc=cc,
bcc=bcc,
message=message,
reference_doctype=doc.doctype,
reference_name=doc.name,
reference_doctype=get_reference_doctype(doc),
reference_name=get_reference_name(doc),
attachments=attachments,
expose_recipients="header",
print_letterhead=((attachments and attachments[0].get("print_letterhead")) or False),
Expand All @@ -306,8 +310,8 @@ def send_a_slack_msg(self, doc, context):
send_slack_message(
webhook_url=self.slack_webhook_url,
message=frappe.render_template(self.message, context),
reference_doctype=doc.doctype,
reference_name=doc.name,
reference_doctype=get_reference_doctype(doc),
reference_name=get_reference_name(doc),
)

def send_sms(self, doc, context):
Expand Down Expand Up @@ -543,3 +547,11 @@ def get_emails_from_template(template, context):

emails = frappe.render_template(template, context) if "{" in template else template
return filter(None, emails.replace(",", "\n").split("\n"))


def get_reference_doctype(doc):
return doc.parenttype if doc.meta.istable else doc.doctype


def get_reference_name(doc):
return doc.parent if doc.meta.istable else doc.name

0 comments on commit b193cde

Please sign in to comment.