custom_ui/custom_ui/api/db/general.py
2025-12-30 12:33:29 -06:00

59 lines
1.7 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import frappe
from custom_ui.db_utils import build_history_entries
def get_doc_history(doctype, docname):
"""Get the history of changes for a specific document."""
# Fetch comments
comments = frappe.get_all(
"Comment",
filters={
"reference_doctype": doctype,
"reference_name": docname
},
fields=["*"],
order_by="creation desc"
)
versions = frappe.get_all(
"Version",
filters={"docname": docname, "ref_doctype": doctype},
fields=["*"],
order_by="creation desc"
)
history_entries = build_history_entries(comments, versions)
print(f"DEBUG: Retrieved history for {doctype} {docname}: {history_entries}")
return history_entries
def get_docs_history(doctypes_with_names):
"""Get history for multiple documents."""
all_history = {}
for doctype, docname in doctypes_with_names:
history = get_doc_history(doctype, docname)
all_history[f"{doctype}:{docname}"] = history
return all_history
def search_any_field(doctype, text):
meta = frappe.get_meta(doctype)
like = f"%{text}%"
conditions = []
# 1⃣ Explicitly include `name`
conditions.append("`name` LIKE %s")
# 2⃣ Include searchable DocFields
for field in meta.fields:
if field.fieldtype in ("Data", "Small Text", "Text", "Link"):
conditions.append(f"`{field.fieldname}` LIKE %s")
query = f"""
SELECT name
FROM `tab{doctype}`
WHERE {" OR ".join(conditions)}
LIMIT 20
"""
return frappe.db.sql(
query,
[like] * len(conditions),
as_dict=True
)