diff --git a/asset_lite/constants.py b/asset_lite/constants.py index 12c3b73..46612b1 100644 --- a/asset_lite/constants.py +++ b/asset_lite/constants.py @@ -15,7 +15,12 @@ DEFAULT_PRINT_FORMAT = "Service Report" DEFAULT_KK_PRINT_FORMAT = "Service Report(KK)" # Naming Series Defaults -DEFAULT_ASSET_NAMING_SERIES = "ASM-KFSH-.YYYY.-" +DEFAULT_ASSET_NAMING_SERIES = "ASM-HOSP-.YYYY.-" + + +# Logos +DEFAULT_HOSPITAL_LOGO = "/files/hospital.png" +DEFAULT_CLUSTER_LOGO = "/files/hospital.png" @frappe.whitelist(allow_guest=False) @@ -39,4 +44,8 @@ def get_app_defaults(): "DEFAULT_HOSPITAL_ABBR": DEFAULT_HOSPITAL_ABBR, "DEFAULT_PRINT_FORMAT": DEFAULT_PRINT_FORMAT, "DEFAULT_ASSET_NAMING_SERIES": DEFAULT_ASSET_NAMING_SERIES, + "DEFAULT_HOSPITAL_LOGO": DEFAULT_HOSPITAL_LOGO, + "DEFAULT_CLUSTER_LOGO": DEFAULT_CLUSTER_LOGO, } + + diff --git a/asset_lite/hooks.py b/asset_lite/hooks.py index 5f56cfc..e9b0bd5 100644 --- a/asset_lite/hooks.py +++ b/asset_lite/hooks.py @@ -89,7 +89,8 @@ on_session_creation = "asset_lite.api.api.set_default_homepage" # ------------ # before_install = "asset_lite.install.before_install" -# after_install = "asset_lite.install.after_install" +after_install = "asset_lite.install.after_install" +after_migrate = "asset_lite.install.after_migrate" # Uninstallation # ------------ diff --git a/asset_lite/install.py b/asset_lite/install.py new file mode 100644 index 0000000..34bf0a0 --- /dev/null +++ b/asset_lite/install.py @@ -0,0 +1,60 @@ +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//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"[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}") diff --git a/asset_lite/public/images/hospital.png b/asset_lite/public/images/hospital.png new file mode 100644 index 0000000..a0165d9 Binary files /dev/null and b/asset_lite/public/images/hospital.png differ