seitime-frappe/frappe/desk/link_preview.py
Aarol D'Souza 08793c57f7
fix: force type check in whitelisted methods 2 (#37086)
* fix(diff): add type hints to whitelisted methods

* fix(global_search): add type hints to whitelisted methods

* fix(custom_html_block): add type hints to whitelisted methods

* fix(deleted_document): add type hints to whitelisted methods

* fix(log_settings): add type hints to whitelisted methods

* fix(role): add type hints to whitelisted methods

* fix(user_type): add type hints to whitelisted methods

* fix(rq_job): add type hints to whitelisted methods

* fix(link_preview): add type hints to whitelisted methods

* fix(email_account): add type hints to whitelisted methods

* fix(web_form): add type hints to whitelisted methods

* fix(web_page_view): add type hints to whitelisted methods

* fix(csvutils): add type hints to whitelisted methods

* fix(file_manager): add type hints to whitelisted methods

* fix(email_body): add type hints to whitelisted methods

* fix(email_queue): add type hints to whitelisted methods

* fix(email_template): add type hints to whitelisted methods

* fix(notification): add type hints to whitelisted methods

* fix(email_group): add type hints to whitelisted methods

* fix(inbox): add type hints to whitelisted methods

* fix(recorder): add type hints to whitelisted methods

* fix(sms_settings): add type hints to whitelisted methods

* fix: tighten type hints

* fix(data_import): add type hints to whitelisted methods

* fix(user_permission): add type hints to whitelisted methods

* fix(gantt): add type hints to whitelisted methods

* fix(like): add type hints to whitelisted methods

* fix(search): add type hints to whitelisted methods

* fix(onboarding_step): add type hints to whitelisted methods

* fix(system_console): add type hints to whitelisted methods

* fix(workspace_sidebar): add type hints to whitelisted methods

* fix(todo): add type hints to whitelisted methods

* fix: correct type hints

* fix(print_format): add type hints to whitelisted methods

* fix(client): add type hints to whitelisted methods
2026-02-19 14:58:16 +05:30

60 lines
1.7 KiB
Python

import frappe
from frappe.model import no_value_fields, table_fields
from frappe.utils.caching import http_cache
from frappe.www.printview import set_title_values_for_link_and_dynamic_link_fields
@frappe.whitelist()
@http_cache(max_age=60 * 10)
def get_preview_data(doctype: str, docname: str | int):
preview_fields = []
meta = frappe.get_meta(doctype)
if not meta.show_preview_popup:
return
preview_fields = [
field.fieldname
for field in meta.fields
if field.in_preview and field.fieldtype not in no_value_fields and field.fieldtype not in table_fields
]
# no preview fields defined, build list from mandatory fields
if not preview_fields:
preview_fields = [
field.fieldname for field in meta.fields if field.reqd and field.fieldtype not in table_fields
]
title_field = meta.get_title_field()
image_field = meta.image_field
preview_fields.append(title_field)
preview_fields.append(image_field)
preview_fields.append("name")
preview_data = frappe.get_list(doctype, filters={"name": docname}, fields=preview_fields, limit=1)
if not preview_data:
return
preview_data = preview_data[0]
formatted_preview_data = {
"preview_image": preview_data.get(image_field),
"preview_title": preview_data.get(title_field),
"name": preview_data.get("name"),
}
doc = None
if meta.show_title_field_in_link and meta.title_field:
doc = frappe.get_doc(doctype, docname)
set_title_values_for_link_and_dynamic_link_fields(meta, doc)
for key, val in preview_data.items():
if val and meta.has_field(key) and key not in [image_field, title_field, "name"]:
formatted_preview_data[meta.get_field(key).label] = frappe.format(
value=val,
doc=doc,
df=meta.get_field(key),
translated=True,
)
return formatted_preview_data