formula_editor/formula_editor/api.py

75 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import frappe
@frappe.whitelist()
def get_doctype_fields(doctype):
meta = frappe.get_meta(doctype)
fields = []
# Получаем все числовые поля
for field in meta.fields:
if field.fieldtype in ["Int", "Float", "Currency"]:
fields.append({"fieldname": field.fieldname, "label": field.label, "fieldtype": field.fieldtype})
# Проверяем таблицы внутри DocType (например, items в Sales Invoice)
for child in meta.fields:
if child.fieldtype == "Table":
child_meta = frappe.get_meta(child.options)
for sub_field in child_meta.fields:
if sub_field.fieldtype in ["Int", "Float", "Currency"]:
fields.append({
"fieldname": f"{child.fieldname}_{sub_field.fieldname}",
"label": f"{child.label}{sub_field.label}",
"fieldtype": sub_field.fieldtype
})
return fields
@frappe.whitelist()
def get_all_doctype_fields(doctype):
"""Получение отфильтрованных полей DocType включая расширенные типы для фильтров"""
meta = frappe.get_meta(doctype)
fields = []
# Типы полей, которые нужно исключить
excluded_types = [
"Tab Break", "Column Break", "Button", "Section Break", "Column Break",
"HTML", "Table", "Attach", "Attach Image", "Code", "Text Editor", "Image",
"Heading", "Fold", "Table MultiSelect"
]
# Получаем все поля, которые не входят в исключенные типы
for field in meta.fields:
if field.fieldname and field.fieldtype not in excluded_types:
field_data = {
"fieldname": field.fieldname,
"label": field.label or field.fieldname,
"fieldtype": field.fieldtype
}
# Добавляем опции для полей типа Select и Link
if field.fieldtype in ["Select", "Link"] and field.options:
field_data["options"] = field.options
fields.append(field_data)
return fields
@frappe.whitelist()
def on_trash(doc, method=None):
"""Удаляет клиентские скрипты, созданные для DocType при удалении Formula Editor"""
if not doc.target_doctype:
return
# Ищем клиентские скрипты, созданные для целевого DocType
client_scripts = frappe.get_all(
"Client Script",
filters={
"dt": doc.target_doctype,
"name": ["like", "Formula_Editor_Script_for_%"]
},
fields=["name"]
)
# Удаляем найденные скрипты
for script in client_scripts:
try:
frappe.delete_doc("Client Script", script.name, force=True)
frappe.db.commit()
except Exception as e:
frappe.log_error(f"Не удалось удалить клиентский скрипт {script.name}: {str(e)}")