Просмотр исходного кода

Merge pull request #14943 from resilient-tech/fix-validate-link

version-14
Suraj Shetty 3 лет назад
committed by GitHub
Родитель
Сommit
df862ddc45
Не найден GPG ключ соответствующий данной подписи Идентификатор GPG ключа: 4AEE18F83AFDEB23
3 измененных файлов: 45 добавлений и 24 удалений
  1. +4
    -4
      cypress/integration/control_link.js
  2. +20
    -0
      frappe/client.py
  3. +21
    -20
      frappe/public/js/frappe/form/controls/link.js

+ 4
- 4
cypress/integration/control_link.js Просмотреть файл

@@ -49,19 +49,19 @@ context('Control Link', () => {
it('should unset invalid value', () => {
get_dialog_with_link().as('dialog');

cy.intercept('GET', '/api/method/frappe.client.get_value*').as('get_value');
cy.intercept('POST', '/api/method/frappe.client.validate_link*').as('validate_link');

cy.get('.frappe-control[data-fieldname=link] input')
.type('invalid value', { delay: 100 })
.blur();
cy.wait('@get_value');
cy.wait('@validate_link');
cy.get('.frappe-control[data-fieldname=link] input').should('have.value', '');
});

it('should route to form on arrow click', () => {
get_dialog_with_link().as('dialog');

cy.intercept('GET', '/api/method/frappe.client.get_value*').as('get_value');
cy.intercept('POST', '/api/method/frappe.client.validate_link*').as('validate_link');
cy.intercept('POST', '/api/method/frappe.desk.search.search_link').as('search_link');

cy.get('@todos').then(todos => {
@@ -69,7 +69,7 @@ context('Control Link', () => {
cy.get('@input').focus();
cy.wait('@search_link');
cy.get('@input').type(todos[0]).blur();
cy.wait('@get_value');
cy.wait('@validate_link');
cy.get('@input').focus();
cy.findByTitle('Open Link')
.should('be.visible')


+ 20
- 0
frappe/client.py Просмотреть файл

@@ -405,3 +405,23 @@ def is_document_amended(doctype, docname):
pass

return False

@frappe.whitelist()
def validate_link(doctype: str, docname: str):
if not isinstance(doctype, str):
frappe.throw(_("DocType must be a string"))

if not isinstance(docname, str):
frappe.throw(_("Document Name 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)

+ 21
- 20
frappe/public/js/frappe/form/controls/link.js Просмотреть файл

@@ -454,38 +454,39 @@ 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);

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));
});
}



Загрузка…
Отмена
Сохранить