Skip to content

Commit

Permalink
refactor(minor): call whitelisted method to get meta (#301)
Browse files Browse the repository at this point in the history
* refactor(minor): call whitelisted method to get meta

using with_doctype stores meta in localstorage which causes it to be full and is not required to replaced it with get_meta method in print designer

* fix: send dynamicField instead of field.

some keys are not in the field object such as parentField, so we need to send the dynamicField object instead.
  • Loading branch information
maharshivpatel authored Jun 24, 2024
1 parent 4fd5e74 commit c236179
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ def render_user_text_withdoc(string, doctype, docname=None, row=None, send_to_ji
return render_user_text(string=string, doc=doc, row=row, send_to_jinja=send_to_jinja)


@frappe.whitelist(allow_guest=False)
def get_meta(doctype):
return frappe.get_meta(doctype).as_dict()


@frappe.whitelist(allow_guest=False)
def render_user_text(string, doc, row=None, send_to_jinja=None):
if not row:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,41 +277,11 @@ const selectField = async (field, fieldtype) => {
});
if (isRemoved) return;
let index = fieldnames.value.length;
let rowValue = null;
if (props.table) {
rowValue = MainStore.docData[props.table.fieldname][0];
}
let value = await getFormattedValue(field, rowValue);
if (!value) {
if (["Image, Attach Image"].indexOf(field.fieldtype) != -1) {
value = null;
} else {
switch (field.fieldname) {
case "page":
value = "0";
break;
case "topage":
value = "999";
break;
case "date":
value = frappe.datetime.now_date();
break;
case "time":
value = frappe.datetime.now_time();
break;
default:
value = `{{ ${
previewRef.value.parentField ? previewRef.value.parentField + "." : ""
}${field.fieldname} }}`;
}
}
}
let dynamicField = {
doctype: doctype.value,
parentField: previewRef.value.parentField,
fieldname: field.fieldname,
options: field.options,
value,
fieldtype,
label: props.table ? field.label : `${field.label} :`,
suffix: null,
Expand All @@ -323,6 +293,11 @@ const selectField = async (field, fieldtype) => {
labelStyle: {},
nextLine: !props.table,
};
let rowValue = null;
if (props.table) {
rowValue = MainStore.docData[props.table.fieldname][0];
}
dynamicField["value"] = await getFormattedValue(dynamicField, rowValue);
if (previewRef.value.selectedEl) {
index = previewRef.value.selectedEl.index + 1;
fieldnames.value.splice(index, 0, dynamicField);
Expand Down
63 changes: 29 additions & 34 deletions print_designer/public/js/print_designer/store/fetchMetaAndData.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,34 @@
import { watch, markRaw } from "vue";
import { useMainStore } from "./MainStore";
import { useElementStore } from "./ElementStore";
export const fetchMeta = () => {
export const fetchMeta = async () => {
const MainStore = useMainStore();
frappe.model.clear_doc("Print Format", MainStore.printDesignName);
frappe.model.with_doc("Print Format", MainStore.printDesignName, () => {
let print_format = frappe.get_doc("Print Format", MainStore.printDesignName);
MainStore.doctype = print_format.doc_type;
frappe.model.with_doctype(print_format.doc_type, () => {
MainStore.rawMeta = markRaw(frappe.get_meta(print_format.doc_type));
let metaFields = frappe.get_meta(print_format.doc_type).fields.filter((df) => {
if (
["Section Break", "Column Break", "Tab Break", "Image"].includes(df.fieldtype)
) {
return false;
} else {
return true;
}
});
metaFields.map((field) => {
let obj = {};
["fieldname", "fieldtype", "label", "options", "print_hide"].forEach((attr) => {
obj[attr] = field[attr];
});
MainStore.metaFields.push({ ...obj });
});
metaFields.map((field) => {
if (field["fieldtype"] == "Table") {
getMeta(field.options, field.fieldname);
}
});
fetchDoc();
!MainStore.getTableMetaFields.length && (MainStore.controls.Table.isDisabled = true);
MainStore.doctype = await getValue("Print Format", MainStore.printDesignName, "doc_type");
MainStore.rawMeta = await frappe.xcall(
"print_designer.print_designer.page.print_designer.print_designer.get_meta",
{ doctype: MainStore.doctype }
);
let metaFields = MainStore.rawMeta.fields.filter((df) => {
if (["Section Break", "Column Break", "Tab Break", "Image"].includes(df.fieldtype)) {
return false;
} else {
return true;
}
});
metaFields.map((field) => {
let obj = {};
["fieldname", "fieldtype", "label", "options", "print_hide"].forEach((attr) => {
obj[attr] = field[attr];
});
MainStore.metaFields.push({ ...obj });
});
metaFields.map((field) => {
if (field["fieldtype"] == "Table") {
getMeta(field.options, field.fieldname);
}
});
fetchDoc();
!MainStore.getTableMetaFields.length && (MainStore.controls.Table.isDisabled = true);
return;
};

Expand All @@ -43,15 +38,15 @@ export const getMeta = async (doctype, parentField) => {
if (MainStore.metaFields.find((o) => o.fieldname == parentField)["childfields"]) {
return MainStore.metaFields[parentField]["childfields"];
}
let result;
const exculdeFields = ["Section Break", "Column Break", "Tab Break", "HTML"];
if (parentMetaField.fieldtype != "Table") {
// Remove Link Field
exculdeFields.push("Link");
}
await frappe.model.with_doctype(doctype, async () => {
result = await frappe.get_meta(doctype);
});
const result = await frappe.xcall(
"print_designer.print_designer.page.print_designer.print_designer.get_meta",
{ doctype }
);
let childfields = result.fields.filter((df) => {
if (
exculdeFields.includes(df.fieldtype) ||
Expand Down

0 comments on commit c236179

Please sign in to comment.