* Remove six for PY2 compatability since our dependencies are not, PY2 is legacy. * Removed usages of utils from future/past libraries since they are deprecated. This includes 'from __future__ ...' and 'from past...' statements. * Removed compatibility imports for PY2, switched from six imports to standard library imports. * Removed utils code blocks that handle operations depending on PY2/3 versions. * Removed 'from __future__ ...' lines from templates/code generators * Used PY3 syntaxes in place of PY2 compatible blocks. eg: metaclass
60 lines
2.2 KiB
Python
60 lines
2.2 KiB
Python
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
|
# MIT License. See license.txt
|
|
|
|
import frappe
|
|
|
|
# select doctypes that are accessed by the user (not read_only) first, so that the
|
|
# the validation message shows the user-facing doctype first.
|
|
# For example Journal Entry should be validated before GL Entry (which is an internal doctype)
|
|
|
|
dynamic_link_queries = [
|
|
"""select `tabDocField`.parent,
|
|
`tabDocType`.read_only, `tabDocType`.in_create,
|
|
`tabDocField`.fieldname, `tabDocField`.options
|
|
from `tabDocField`, `tabDocType`
|
|
where `tabDocField`.fieldtype='Dynamic Link' and
|
|
`tabDocType`.`name`=`tabDocField`.parent
|
|
order by `tabDocType`.read_only, `tabDocType`.in_create""",
|
|
|
|
"""select `tabCustom Field`.dt as parent,
|
|
`tabDocType`.read_only, `tabDocType`.in_create,
|
|
`tabCustom Field`.fieldname, `tabCustom Field`.options
|
|
from `tabCustom Field`, `tabDocType`
|
|
where `tabCustom Field`.fieldtype='Dynamic Link' and
|
|
`tabDocType`.`name`=`tabCustom Field`.dt
|
|
order by `tabDocType`.read_only, `tabDocType`.in_create""",
|
|
]
|
|
|
|
def get_dynamic_link_map(for_delete=False):
|
|
'''Build a map of all dynamically linked tables. For example,
|
|
if Note is dynamically linked to ToDo, the function will return
|
|
`{"Note": ["ToDo"], "Sales Invoice": ["Journal Entry Detail"]}`
|
|
|
|
Note: Will not map single doctypes
|
|
'''
|
|
if getattr(frappe.local, 'dynamic_link_map', None)==None or frappe.flags.in_test:
|
|
# Build from scratch
|
|
dynamic_link_map = {}
|
|
for df in get_dynamic_links():
|
|
meta = frappe.get_meta(df.parent)
|
|
if meta.issingle:
|
|
# always check in Single DocTypes
|
|
dynamic_link_map.setdefault(meta.name, []).append(df)
|
|
else:
|
|
try:
|
|
links = frappe.db.sql_list("""select distinct {options} from `tab{parent}`""".format(**df))
|
|
for doctype in links:
|
|
dynamic_link_map.setdefault(doctype, []).append(df)
|
|
except frappe.db.TableMissingError: # noqa: E722
|
|
pass
|
|
|
|
frappe.local.dynamic_link_map = dynamic_link_map
|
|
return frappe.local.dynamic_link_map
|
|
|
|
def get_dynamic_links():
|
|
'''Return list of dynamic link fields as DocField.
|
|
Uses cache if possible'''
|
|
df = []
|
|
for query in dynamic_link_queries:
|
|
df += frappe.db.sql(query, as_dict=True)
|
|
return df
|