2026-09-10 11:59:20 +05:30

61 lines
1.8 KiB
Python

import os
import frappe
def after_install():
"""Executed after the app is installed on a site."""
setup_default_files()
def after_migrate():
"""Executed after 'bench migrate' is run on a site."""
setup_default_files()
def setup_default_files():
"""Ensure default app assets (e.g., hospital.png) are added to the Frappe File DocType.
This makes the image available at '/files/hospital.png' and visible in Desk File List.
"""
files_to_setup = [
{
"file_name": "hospital.png",
"relative_path": ["public", "images", "hospital.png"],
"is_private": 0,
"folder": "Home",
}
]
for item in files_to_setup:
file_name = item["file_name"]
# 1. Check if the file already exists in File DocType
if frappe.db.exists("File", {"file_name": file_name}) or frappe.db.exists("File", {"file_url": f"/files/{file_name}"}):
continue
# 2. Locate the source file in app directory
source_path = frappe.get_app_path("asset_lite", *item["relative_path"])
if not os.path.exists(source_path):
frappe.logger().warning(f"[asset_lite] Source file not found for default file setup: {source_path}")
continue
# 3. Read binary content
with open(source_path, "rb") as f:
file_content = f.read()
# 4. Create Frappe File document
# Setting is_private=0 and providing content automatically writes to sites/<site>/public/files/<file_name>
try:
file_doc = frappe.get_doc({
"doctype": "File",
"file_name": file_name,
"content": file_content,
"is_private": item.get("is_private", 0),
"folder": item.get("folder", "Home"),
})
file_doc.insert(ignore_permissions=True)
frappe.db.commit()
frappe.logger().info(f"[asset_lite] Successfully added default file '{file_name}' to File DocType.")
except Exception as e:
frappe.logger().error(f"[asset_lite] Failed to add default file '{file_name}' to File DocType: {e}")