diff --git a/frappe/core/doctype/dynamic_link/dynamic_link.py b/frappe/core/doctype/dynamic_link/dynamic_link.py index 21f82f522a..bc5ac9a8b2 100644 --- a/frappe/core/doctype/dynamic_link/dynamic_link.py +++ b/frappe/core/doctype/dynamic_link/dynamic_link.py @@ -11,3 +11,17 @@ class DynamicLink(Document): def on_doctype_update(): frappe.db.add_index("Dynamic Link", ["link_doctype", "link_name"]) + +def deduplicate_dynamic_links(doc): + links, duplicate = [], False + for l in doc.links: + t = (l.link_doctype, l.link_name) + if not t in links: + links.append(t) + else: + duplicate = True + + if duplicate: + doc.links = [] + for l in links: + doc.append('links', dict(link_doctype=l[0], link_name=l[1])) diff --git a/frappe/email/doctype/contact/contact.py b/frappe/email/doctype/contact/contact.py index 598e239ca0..e43a997f28 100644 --- a/frappe/email/doctype/contact/contact.py +++ b/frappe/email/doctype/contact/contact.py @@ -6,6 +6,7 @@ import frappe from frappe.utils import cstr, has_gravatar from frappe import _ from frappe.model.document import Document +from frappe.core.doctype.dynamic_link.dynamic_link import deduplicate_dynamic_links class Contact(Document): def autoname(self): @@ -23,6 +24,8 @@ class Contact(Document): if self.email_id: self.image = has_gravatar(self.email_id) + deduplicate_dynamic_links(self) + def set_user(self): if not self.user and self.email_id: self.user = frappe.db.get_value("User", {"email": self.email_id}) diff --git a/frappe/geo/doctype/address/address.py b/frappe/geo/doctype/address/address.py index 79b2c6a6bc..7f59199640 100644 --- a/frappe/geo/doctype/address/address.py +++ b/frappe/geo/doctype/address/address.py @@ -12,6 +12,7 @@ from frappe.model.document import Document from jinja2 import TemplateSyntaxError from frappe.utils.user import is_website_user from frappe.model.naming import make_autoname +from frappe.core.doctype.dynamic_link.dynamic_link import deduplicate_dynamic_links class Address(Document): def __setup__(self): @@ -33,6 +34,7 @@ class Address(Document): def validate(self): self.link_address() self.validate_reference() + deduplicate_dynamic_links(self) def link_address(self): """Link address based on owner"""