From 3217782b3e87f44b371aa3286fd842dd153ae549 Mon Sep 17 00:00:00 2001 From: anandbaburajan Date: Tue, 26 Dec 2023 11:27:35 +0530 Subject: [PATCH] chore: remove custom fields on uninstall --- lending/hooks.py | 2 +- lending/install.py | 30 --------------------------- lending/uninstall.py | 49 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 31 deletions(-) create mode 100644 lending/uninstall.py diff --git a/lending/hooks.py b/lending/hooks.py index 503ed4c8..9f658492 100644 --- a/lending/hooks.py +++ b/lending/hooks.py @@ -80,7 +80,7 @@ # Uninstallation # ------------ -# before_uninstall = "lending.uninstall.before_uninstall" +before_uninstall = "lending.uninstall.before_uninstall" # after_uninstall = "lending.uninstall.after_uninstall" # Desk Notifications diff --git a/lending/install.py b/lending/install.py index 44b6c684..0945bcbe 100644 --- a/lending/install.py +++ b/lending/install.py @@ -234,33 +234,3 @@ def after_install(): make_property_setter_for_journal_entry() print("\nRunning post-install patches to patch existing data...\n") run_patches(get_post_install_patches()) - - -def before_uninstall(): - delete_custom_fields(LOAN_CUSTOM_FIELDS) - - -def delete_custom_fields(custom_fields): - """ - :param custom_fields: a dict like `{'Customer': [{fieldname: 'test', ...}]}` - """ - - for doctypes, fields in custom_fields.items(): - if isinstance(fields, dict): - # only one field - fields = [fields] - - if isinstance(doctypes, str): - # only one doctype - doctypes = (doctypes,) - - for doctype in doctypes: - frappe.db.delete( - "Custom Field", - { - "fieldname": ("in", [field["fieldname"] for field in fields]), - "dt": doctype, - }, - ) - - frappe.clear_cache(doctype=doctype) diff --git a/lending/uninstall.py b/lending/uninstall.py new file mode 100644 index 00000000..a78855d0 --- /dev/null +++ b/lending/uninstall.py @@ -0,0 +1,49 @@ +import click + +import frappe + +from lending.install import LOAN_CUSTOM_FIELDS + + +def before_uninstall(): + try: + print("Removing customizations created by the Frappe Lending app...") + delete_custom_fields(LOAN_CUSTOM_FIELDS) + + except Exception as e: + BUG_REPORT_URL = "https://github.com/frappe/lending/issues/new" + click.secho( + "Removing Customizations for Frappe Lending failed due to an error." + " Please try again or" + f" report the issue on {BUG_REPORT_URL} if not resolved.", + fg="bright_red", + ) + raise e + + click.secho("Frappe Lending app customizations have been removed successfully.", fg="green") + + +def delete_custom_fields(custom_fields): + """ + :param custom_fields: a dict like `{'Customer': [{fieldname: 'test', ...}]}` + """ + + for doctypes, fields in custom_fields.items(): + if isinstance(fields, dict): + # only one field + fields = [fields] + + if isinstance(doctypes, str): + # only one doctype + doctypes = (doctypes,) + + for doctype in doctypes: + frappe.db.delete( + "Custom Field", + { + "fieldname": ("in", [field["fieldname"] for field in fields]), + "dt": doctype, + }, + ) + + frappe.clear_cache(doctype=doctype)