59 lines
1.7 KiB
Python
59 lines
1.7 KiB
Python
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
|
||
) |