fix: restore validate_link

This commit is contained in:
Sagar Vora 2021-11-10 22:51:12 +05:30
parent 20e3ed9ff9
commit 9503e7788a
2 changed files with 39 additions and 20 deletions

View file

@ -405,3 +405,20 @@ def is_document_amended(doctype, docname):
pass
return False
@frappe.whitelist()
def validate_link(doctype: str, docname):
if not isinstance(doctype, str):
frappe.throw(_("DocType must be a string"))
if doctype != "DocType" and not (
frappe.has_permission(doctype, "select")
or frappe.has_permission(doctype, "read")
):
frappe.throw(
_("You do not have Read or Select Permissions for {}")
.format(frappe.bold(doctype)),
frappe.PermissionError
)
return frappe.db.get_value(doctype, docname, cache=True)

View file

@ -454,38 +454,40 @@ frappe.ui.form.ControlLink = class ControlLink extends frappe.ui.form.ControlDat
validate_link_and_fetch(df, options, docname, value) {
if (!value) return;
return new Promise((resolve) => {
return new Promise(async (resolve) => {
const fetch_map = this.fetch_map;
const columns_to_fetch = Object.values(fetch_map);
// if default and no fetch, no need to validate
if ($.isEmptyObject(fetch_map) && df.__default_value === value) {
if (!columns_to_fetch.length && df.__default_value === value) {
return resolve(value);
}
const name = await frappe.xcall("frappe.client.validate_link", {
doctype: options,
docname: value
});
if (!name) return resolve("");
if (!docname || !columns_to_fetch.length) return resolve(name);
console.log(columns_to_fetch);
frappe.db.get_value(
options,
value,
["name", ...Object.values(fetch_map)],
columns_to_fetch,
(response) => {
if (!response.name) {
return resolve("");
for (const [target_field, source_field] of Object.entries(fetch_map)) {
frappe.model.set_value(
df.parent,
docname,
target_field,
response[source_field],
df.fieldtype,
);
}
if (docname) {
for (const [target_field, source_field] of Object.entries(fetch_map)) {
frappe.model.set_value(
df.parent,
docname,
target_field,
response[source_field],
df.fieldtype,
);
}
}
return resolve(response.name);
}
)
).always(() => resolve(name));
});
}