Browse Source

fix: query parent on Dashboard Chart and number card

Querying docfields directly like this is not allowed

(cherry picked from commit 466d59f2e1)
version-14
Ankush Menat 2 years ago
committed by Ankush Menat
parent
commit
19e9b3bc9b
3 changed files with 48 additions and 34 deletions
  1. +13
    -17
      frappe/desk/doctype/dashboard_chart/dashboard_chart.js
  2. +22
    -0
      frappe/desk/doctype/dashboard_chart/dashboard_chart.py
  3. +13
    -17
      frappe/desk/doctype/number_card/number_card.js

+ 13
- 17
frappe/desk/doctype/dashboard_chart/dashboard_chart.js View File

@@ -532,25 +532,21 @@ frappe.ui.form.on("Dashboard Chart", {
frm.set_df_property("parent_document_type", "hidden", !doc_is_table);

if (document_type && doc_is_table) {
let parent = await frappe.db.get_list("DocField", {
filters: {
fieldtype: "Table",
options: document_type,
},
fields: ["parent"],
});
let parents = await frappe.xcall(
"frappe.desk.doctype.dashboard_chart.dashboard_chart.get_parent_doctypes",
{ child_type: document_type }
);

parent &&
frm.set_query("parent_document_type", function () {
return {
filters: {
name: ["in", parent.map(({ parent }) => parent)],
},
};
});
frm.set_query("parent_document_type", function () {
return {
filters: {
name: ["in", parents],
},
};
});

if (parent.length === 1) {
frm.set_value("parent_document_type", parent[0].parent);
if (parents.length === 1) {
frm.set_value("parent_document_type", parents[0]);
}
}
},


+ 22
- 0
frappe/desk/doctype/dashboard_chart/dashboard_chart.py View File

@@ -392,3 +392,25 @@ class DashboardChart(Document):
json.loads(self.custom_options)
except ValueError as error:
frappe.throw(_("Invalid json added in the custom options: {0}").format(error))


@frappe.whitelist()
def get_parent_doctypes(child_type: str) -> list[str]:
"""Get all parent doctypes that have the child doctype."""
assert isinstance(child_type, str)

standard = frappe.get_all(
"DocField",
fields="parent",
filters={"fieldtype": "Table", "options": child_type},
pluck="parent",
)

custom = frappe.get_all(
"Custom Field",
fields="dt",
filters={"fieldtype": "Table", "options": child_type},
pluck="dt",
)

return standard + custom

+ 13
- 17
frappe/desk/doctype/number_card/number_card.js View File

@@ -472,25 +472,21 @@ frappe.ui.form.on("Number Card", {
frm.set_df_property("parent_document_type", "hidden", !doc_is_table);

if (document_type && doc_is_table) {
let parent = await frappe.db.get_list("DocField", {
filters: {
fieldtype: "Table",
options: document_type,
},
fields: ["parent"],
});
let parents = await frappe.xcall(
"frappe.desk.doctype.dashboard_chart.dashboard_chart.get_parent_doctypes",
{ child_type: document_type }
);

parent &&
frm.set_query("parent_document_type", function () {
return {
filters: {
name: ["in", parent.map(({ parent }) => parent)],
},
};
});
frm.set_query("parent_document_type", function () {
return {
filters: {
name: ["in", parents],
},
};
});

if (parent.length === 1) {
frm.set_value("parent_document_type", parent[0].parent);
if (parents.length === 1) {
frm.set_value("parent_document_type", parents[0]);
}
}
},


Loading…
Cancel
Save