Merge pull request #19734 from barredterra/load-address-and-contact-display

This commit is contained in:
Suraj Shetty 2023-05-24 15:39:04 +05:30 committed by GitHub
commit d2b7f47709
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 65 deletions

View file

@ -1,77 +1,17 @@
# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and Contributors
# License: MIT. See LICENSE
import functools
import re
import frappe
from frappe import _
def load_address_and_contact(doc, key=None):
def load_address_and_contact(doc, key=None) -> None:
"""Loads address list and contact list in `__onload`"""
from frappe.contacts.doctype.address.address import get_address_display, get_condensed_address
from frappe.contacts.doctype.address.address import get_address_display_list
from frappe.contacts.doctype.contact.contact import get_contact_display_list
filters = [
["Dynamic Link", "link_doctype", "=", doc.doctype],
["Dynamic Link", "link_name", "=", doc.name],
["Dynamic Link", "parenttype", "=", "Address"],
]
address_list = frappe.get_list("Address", filters=filters, fields=["*"], order_by="creation asc")
address_list = [a.update({"display": get_address_display(a)}) for a in address_list]
address_list = sorted(
address_list,
key=functools.cmp_to_key(
lambda a, b: (int(a.is_primary_address - b.is_primary_address))
or (1 if a.modified - b.modified else 0)
),
reverse=True,
)
doc.set_onload("addr_list", address_list)
contact_list = []
filters = [
["Dynamic Link", "link_doctype", "=", doc.doctype],
["Dynamic Link", "link_name", "=", doc.name],
["Dynamic Link", "parenttype", "=", "Contact"],
]
contact_list = frappe.get_list("Contact", filters=filters, fields=["*"])
for contact in contact_list:
contact["email_ids"] = frappe.get_all(
"Contact Email",
filters={"parenttype": "Contact", "parent": contact.name, "is_primary": 0},
fields=["email_id"],
)
contact["phone_nos"] = frappe.get_all(
"Contact Phone",
filters={
"parenttype": "Contact",
"parent": contact.name,
"is_primary_phone": 0,
"is_primary_mobile_no": 0,
},
fields=["phone"],
)
if contact.address:
address = frappe.get_doc("Address", contact.address)
contact["address"] = get_condensed_address(address)
contact_list = sorted(
contact_list,
key=functools.cmp_to_key(
lambda a, b: (int(a.is_primary_contact - b.is_primary_contact))
or (1 if a.modified - b.modified else 0)
),
reverse=True,
)
doc.set_onload("contact_list", contact_list)
doc.set_onload("addr_list", get_address_display_list(doc.doctype, doc.name))
doc.set_onload("contact_list", get_contact_display_list(doc.doctype, doc.name))
def has_permission(doc, ptype, user):

View file

@ -291,3 +291,23 @@ def get_condensed_address(doc):
def update_preferred_address(address, field):
frappe.db.set_value("Address", address, field, 0)
def get_address_display_list(doctype: str, name: str) -> list[dict]:
if not frappe.has_permission("Address", "read"):
return []
address_list = frappe.get_list(
"Address",
filters=[
["Dynamic Link", "link_doctype", "=", doctype],
["Dynamic Link", "link_name", "=", name],
["Dynamic Link", "parenttype", "=", "Address"],
],
fields=["*"],
order_by="is_primary_address DESC, creation ASC",
)
for a in address_list:
a["display"] = get_address_display(a)
return address_list

View file

@ -341,3 +341,45 @@ def get_full_name(
full_name = company
return full_name
def get_contact_display_list(doctype: str, name: str) -> list[dict]:
from frappe.contacts.doctype.address.address import get_condensed_address
if not frappe.has_permission("Contact", "read"):
return []
contact_list = frappe.get_list(
"Contact",
filters=[
["Dynamic Link", "link_doctype", "=", doctype],
["Dynamic Link", "link_name", "=", name],
["Dynamic Link", "parenttype", "=", "Contact"],
],
fields=["*"],
order_by="is_primary_contact DESC, creation ASC",
)
for contact in contact_list:
contact["email_ids"] = frappe.get_all(
"Contact Email",
filters={"parenttype": "Contact", "parent": contact.name, "is_primary": 0},
fields=["email_id"],
)
contact["phone_nos"] = frappe.get_all(
"Contact Phone",
filters={
"parenttype": "Contact",
"parent": contact.name,
"is_primary_phone": 0,
"is_primary_mobile_no": 0,
},
fields=["phone"],
)
if contact.address and frappe.has_permission("Address", "read"):
address = frappe.get_doc("Address", contact.address)
contact["address"] = get_condensed_address(address)
return contact_list