Merge pull request #23129 from anupamvs/email-digest

fix: multiselect recipients in Email Digest
This commit is contained in:
Jannat Patel 2021-07-29 12:36:49 +05:30 committed by GitHub
commit 285463b031
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 389 additions and 1514 deletions

View File

@ -295,3 +295,4 @@ erpnext.patches.v13_0.add_missing_fg_item_for_stock_entry
erpnext.patches.v13_0.update_subscription_status_in_memberships
erpnext.patches.v13_0.update_export_type_for_gst
erpnext.patches.v13_0.update_tds_check_field #3
erpnext.patches.v13_0.update_recipient_email_digest

View File

@ -0,0 +1,21 @@
# Copyright (c) 2020, Frappe and Contributors
# License: GNU General Public License v3. See license.txt
from __future__ import unicode_literals
import frappe
def execute():
frappe.reload_doc("setup", "doctype", "Email Digest")
frappe.reload_doc("setup", "doctype", "Email Digest Recipient")
email_digests = frappe.db.get_list('Email Digest', fields=['name', 'recipient_list'])
for email_digest in email_digests:
if email_digest.recipient_list:
for recipient in email_digest.recipient_list.split("\n"):
doc = frappe.get_doc({
'doctype': 'Email Digest Recipient',
'parenttype': 'Email Digest',
'parentfield': 'recipients',
'parent': email_digest.name,
'recipient': recipient
})
doc.insert()

View File

@ -1,78 +1,31 @@
// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
// License: GNU General Public License v3. See license.txt
cur_frm.cscript.refresh = function(doc, dt, dn) {
doc = locals[dt][dn];
cur_frm.add_custom_button(__('View Now'), function() {
frappe.call({
method: 'erpnext.setup.doctype.email_digest.email_digest.get_digest_msg',
args: {
name: doc.name
},
callback: function(r) {
var d = new frappe.ui.Dialog({
title: __('Email Digest: ') + dn,
width: 800
frappe.ui.form.on("Email Digest", {
refresh: function(frm) {
if (!frm.is_new()) {
frm.add_custom_button(__('View Now'), function() {
frappe.call({
method: 'erpnext.setup.doctype.email_digest.email_digest.get_digest_msg',
args: {
name: frm.doc.name
},
callback: function(r) {
let d = new frappe.ui.Dialog({
title: __('Email Digest: {0}', [frm.doc.name]),
width: 800
});
$(d.body).html(r.message);
d.show();
}
});
$(d.body).html(r.message);
d.show();
}
});
}, "fa fa-eye-open", "btn-default");
if (!cur_frm.is_new()) {
cur_frm.add_custom_button(__('Send Now'), function() {
return cur_frm.call('send', null, (r) => {
frappe.show_alert(__('Message Sent'));
});
});
frm.add_custom_button(__('Send Now'), function() {
return frm.call('send', null, () => {
frappe.show_alert({ message: __("Message Sent"), indicator: 'green'});
});
});
}
}
};
cur_frm.cscript.addremove_recipients = function(doc, dt, dn) {
// Get user list
return cur_frm.call('get_users', null, function(r) {
// Open a dialog and display checkboxes against email addresses
doc = locals[dt][dn];
var d = new frappe.ui.Dialog({
title: __('Add/Remove Recipients'),
width: 400
});
$.each(r.user_list, function(i, v) {
var fullname = frappe.user.full_name(v.name);
if(fullname !== v.name) fullname = fullname + " <" + v.name + ">";
if(v.enabled==0) {
fullname = repl("<span style='color: red'> %(name)s (" + __("disabled user") + ")</span>", {name: v.name});
}
$('<div class="checkbox"><label>\
<input type="checkbox" data-id="' + v.name + '"'+
(v.checked ? 'checked' : '') +
'> '+ fullname +'</label></div>').appendTo(d.body);
});
// Display add recipients button
d.set_primary_action("Update", function() {
cur_frm.cscript.add_to_rec_list(doc, d.body, r.user_list.length);
});
cur_frm.rec_dialog = d;
d.show();
});
}
cur_frm.cscript.add_to_rec_list = function(doc, dialog, length) {
// add checked users to list of recipients
var rec_list = [];
$(dialog).find('input:checked').each(function(i, input) {
rec_list.push($(input).attr('data-id'));
});
doc.recipient_list = rec_list.join('\n');
cur_frm.rec_dialog.hide();
cur_frm.save();
cur_frm.refresh_fields();
}
});

File diff suppressed because it is too large Load Diff

View File

@ -47,19 +47,13 @@ class EmailDigest(Document):
# send email only to enabled users
valid_users = [p[0] for p in frappe.db.sql("""select name from `tabUser`
where enabled=1""")]
recipients = list(filter(lambda r: r in valid_users,
self.recipient_list.split("\n")))
original_user = frappe.session.user
if recipients:
for user_id in recipients:
frappe.set_user(user_id)
frappe.set_user_lang(user_id)
if self.recipients:
for row in self.recipients:
msg_for_this_recipient = self.get_msg_html()
if msg_for_this_recipient:
if msg_for_this_recipient and row.recipient in valid_users:
frappe.sendmail(
recipients=user_id,
recipients=row.recipient,
subject=_("{0} Digest").format(self.frequency),
message=msg_for_this_recipient,
reference_doctype = self.doctype,

View File

@ -0,0 +1,33 @@
{
"actions": [],
"creation": "2020-06-08 12:19:40.428949",
"doctype": "DocType",
"editable_grid": 1,
"engine": "InnoDB",
"field_order": [
"recipient"
],
"fields": [
{
"fieldname": "recipient",
"fieldtype": "Link",
"in_list_view": 1,
"label": "Recipient",
"options": "User",
"reqd": 1
}
],
"index_web_pages_for_search": 1,
"istable": 1,
"links": [],
"modified": "2020-08-24 23:10:23.217572",
"modified_by": "Administrator",
"module": "Setup",
"name": "Email Digest Recipient",
"owner": "Administrator",
"permissions": [],
"quick_entry": 1,
"sort_field": "modified",
"sort_order": "DESC",
"track_changes": 1
}

View File

@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2020, Frappe Technologies Pvt. Ltd. and contributors
# For license information, please see license.txt
from __future__ import unicode_literals
# import frappe
from frappe.model.document import Document
class EmailDigestRecipient(Document):
pass

View File

@ -45,9 +45,16 @@ def enable_shopping_cart(args):
def create_email_digest():
from frappe.utils.user import get_system_managers
system_managers = get_system_managers(only_name=True)
if not system_managers:
return
recipients = []
for d in system_managers:
recipients.append({
'recipient': d
})
companies = frappe.db.sql_list("select name FROM `tabCompany`")
for company in companies:
if not frappe.db.exists("Email Digest", "Default Weekly Digest - " + company):
@ -56,7 +63,7 @@ def create_email_digest():
"name": "Default Weekly Digest - " + company,
"company": company,
"frequency": "Weekly",
"recipient_list": "\n".join(system_managers)
"recipients": recipients
})
for df in edigest.meta.get("fields", {"fieldtype": "Check"}):
@ -72,7 +79,7 @@ def create_email_digest():
"name": "Scheduler Errors",
"company": companies[0],
"frequency": "Daily",
"recipient_list": "\n".join(system_managers),
"recipients": recipients,
"scheduler_errors": 1,
"enabled": 1
})