From 0bd9f37ebe091bd68173ddde19d11ea0edbdec5d Mon Sep 17 00:00:00 2001 From: mbauskar Date: Tue, 7 Mar 2017 17:17:14 +0530 Subject: [PATCH] [minor] filters for both address and contact dynamic link lin_doctype field --- frappe/email/doctype/contact/contact.js | 11 +++++++++- frappe/email/doctype/contact/contact.py | 10 --------- frappe/geo/address_and_contact.py | 27 ++++++++++++++++++++++++- frappe/geo/doctype/address/address.js | 9 +++++++++ 4 files changed, 45 insertions(+), 12 deletions(-) diff --git a/frappe/email/doctype/contact/contact.js b/frappe/email/doctype/contact/contact.js index c550e99cd8..0c7ef40f2b 100644 --- a/frappe/email/doctype/contact/contact.js +++ b/frappe/email/doctype/contact/contact.js @@ -29,7 +29,15 @@ frappe.ui.form.on("Contact", { }); }); } - cur_frm.set_query("link_doctype", "links", "frappe.email.doctype.contact.contact.contact_links") + frm.set_query('link_doctype', "links", function() { + return { + query: "frappe.geo.address_and_contact.filter_dynamic_link_doctypes", + filters: { + fieldtype: "Link", + options: frm.doc.doctype, + } + } + }); }, validate: function(frm) { // clear linked customer / supplier / sales partner on saving... @@ -40,6 +48,7 @@ frappe.ui.form.on("Contact", { } } }); + frappe.ui.form.on("Dynamic Link", { link_name:function(frm, cdt, cdn){ var child = locals[cdt][cdn]; diff --git a/frappe/email/doctype/contact/contact.py b/frappe/email/doctype/contact/contact.py index 107b476a55..6f24165c81 100644 --- a/frappe/email/doctype/contact/contact.py +++ b/frappe/email/doctype/contact/contact.py @@ -149,13 +149,3 @@ def contact_query(doctype, txt, searchfield, start, page_len, filters): 'link_name': link_name, 'link_doctype': link_doctype }) - -def contact_links(doctype, txt, searchfield, start, page_len, filters): - if not txt: txt = "" - txt = txt.lower() - return [[d] for d in get_link_doctypes() if txt in d.lower()] - -@frappe.whitelist() -def get_link_doctypes(): - return [x.parent for x in frappe.db.get_values("DocField", {"fieldname": 'contact_html'}, "parent", order_by="name", as_dict=1)] - diff --git a/frappe/geo/address_and_contact.py b/frappe/geo/address_and_contact.py index 8f6bc3fdf7..717471dbd1 100644 --- a/frappe/geo/address_and_contact.py +++ b/frappe/geo/address_and_contact.py @@ -138,4 +138,29 @@ def delete_contact_and_address(doctype, docname): for name in items: doc = frappe.get_doc(parenttype, name) if len(doc.links)==1: - doc.delete() \ No newline at end of file + doc.delete() + +def filter_dynamic_link_doctypes(doctype, txt, searchfield, start, page_len, filters): + if not txt: txt = "" + + txt = txt.lower() + txt = "%%%s%%" % (txt) + + filters.update({ + "parent": ("like", txt) + }) + + doctypes = frappe.db.get_all("DocField", filters=filters, fields=["parent"], + order_by="parent asc", distinct=True, as_list=True) + + filters.pop("parent") + filters.update({ + "dt": ("not in", [doctype[0] for doctype in doctypes]), + "dt": ("like", txt), + }) + + _doctypes = frappe.db.get_all("Custom Field", filters=filters, fields=["dt"], + order_by="dt asc", as_list=True) + + all_doctypes = doctypes + _doctypes + return sorted(all_doctypes, key=lambda item: item[0]) \ No newline at end of file diff --git a/frappe/geo/doctype/address/address.js b/frappe/geo/doctype/address/address.js index aff7b85860..41691e03fe 100644 --- a/frappe/geo/doctype/address/address.js +++ b/frappe/geo/doctype/address/address.js @@ -13,6 +13,15 @@ frappe.ui.form.on("Address", { }); } } + frm.set_query('link_doctype', "links", function() { + return { + query: "frappe.geo.address_and_contact.filter_dynamic_link_doctypes", + filters: { + fieldtype: "Link", + options: frm.doc.doctype, + } + } + }); }, validate: function(frm) { // clear linked customer / supplier / sales partner on saving...