From 26bb7e423704fcd2b65167df768cdc61bf1cc381 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Wed, 17 Jul 2019 11:55:25 +0530 Subject: [PATCH] fix: Form UX - Fetch contacts - Message to enable Auto Repeat --- .../doctype/auto_repeat/auto_repeat.js | 31 ++++++-------- .../doctype/auto_repeat/auto_repeat.py | 41 +++++++------------ 2 files changed, 27 insertions(+), 45 deletions(-) diff --git a/frappe/automation/doctype/auto_repeat/auto_repeat.js b/frappe/automation/doctype/auto_repeat/auto_repeat.js index 61585ed7cf..a11de1d881 100644 --- a/frappe/automation/doctype/auto_repeat/auto_repeat.js +++ b/frappe/automation/doctype/auto_repeat/auto_repeat.js @@ -28,13 +28,21 @@ frappe.ui.form.on('Auto Repeat', { }, refresh: function(frm) { - //if document is not saved do not show schedule and document link + // auto repeat message + if (frm.is_new()) { + let customize_form_link = `${__('Customize Form')}`; + frm.dashboard.set_headline(__('To configure Auto Repeat, enable "Allow Auto Repeat" from {0}.', [customize_form_link])); + } + + // view document button if (!frm.is_dirty()) { let label = __('View {0}', [__(frm.doc.reference_doctype)]); - frm.add_custom_button(__(label), () => + frm.add_custom_button(label, () => frappe.set_route("List", frm.doc.reference_doctype, { auto_repeat: frm.doc.name }) ); } + + // auto repeat schedule frappe.auto_repeat.render_schedule(frm); }, @@ -51,22 +59,7 @@ frappe.ui.form.on('Auto Repeat', { }, get_contacts: function(frm) { - frappe.call({ - method: "frappe.automation.doctype.auto_repeat.auto_repeat.get_contacts", - args: { - reference_doctype: frm.doc.reference_doctype, - reference_name: frm.doc.reference_document - }, - callback: function(r) { - if (r.message) { - frm.set_value("recipients", r.message.join()); - frm.refresh_field("recipients"); - } - else { - frappe.msgprint("No Contacts linked to Reference Document", "Message"); - } - } - }); + frm.call('fetch_linked_contacts'); }, preview_message: function(frm) { @@ -80,7 +73,7 @@ frappe.ui.form.on('Auto Repeat', { message: frm.doc.message }, callback: function(r) { - if(r.message) { + if (r.message) { frappe.msgprint(r.message.message, r.message.subject) } } diff --git a/frappe/automation/doctype/auto_repeat/auto_repeat.py b/frappe/automation/doctype/auto_repeat/auto_repeat.py index 83f492a926..9222db5ffd 100644 --- a/frappe/automation/doctype/auto_repeat/auto_repeat.py +++ b/frappe/automation/doctype/auto_repeat/auto_repeat.py @@ -223,6 +223,21 @@ class AutoRepeat(Document): make(doctype=new_doc.doctype, name=new_doc.name, recipients=self.recipients, subject=subject, content=message, attachments=attachments, send_email=1) + def fetch_linked_contacts(self): + if self.reference_doctype and self.reference_document: + res = frappe.db.get_all('Contact', + fields=['email_id'], + filters=[ + ['Dynamic Link', 'link_doctype', '=', self.reference_doctype], + ['Dynamic Link', 'link_name', '=', self.reference_document] + ]) + + email_ids = list(set([d.email_id for d in res])) + if not email_ids: + frappe.msgprint(_('No contacts linked to document'), alert=True) + else: + self.recipients = ', '.join(email_ids) + def disable_auto_repeat(self): frappe.db.set_value('Auto Repeat', self.name, 'disabled', 1) @@ -346,32 +361,6 @@ def get_auto_repeat_doctypes(doctype, txt, searchfield, start, page_len, filters repeatable_docs.append([dt[0]]) return repeatable_docs -@frappe.whitelist() -def get_contacts(reference_doctype, reference_name): - docfields = frappe.get_meta(reference_doctype).fields - - contact_fields = [] - for field in docfields: - if field.fieldtype == "Link" and field.options == "Contact": - contact_fields.append(field.fieldname) - - if contact_fields: - contacts = [] - for contact_field in contact_fields: - contacts.append(frappe.db.get_value(reference_doctype, reference_name, contact_field)) - else: - return - - if contacts: - emails = [] - for contact in contacts: - emails.append(frappe.db.get_value("Contact", contact, "email_id")) - - return emails - else: - return - - @frappe.whitelist() def update_reference(docname, reference): result = ""