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"], "fallback_relative_path": ["public", "asm_app", "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("asm_ui_app", *item["relative_path"]) if not os.path.exists(source_path) and "fallback_relative_path" in item: source_path = frappe.get_app_path("asm_ui_app", *item["fallback_relative_path"]) if not os.path.exists(source_path): frappe.logger().warning(f"[asm_ui_app] 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//public/files/ 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"[asm_ui_app] Successfully added default file '{file_name}' to File DocType.") except Exception as e: frappe.logger().error(f"[asm_ui_app] Failed to add default file '{file_name}' to File DocType: {e}")