Skip to content

Commit

Permalink
chore: remove custom fields on uninstall
Browse files Browse the repository at this point in the history
  • Loading branch information
anandbaburajan committed Dec 26, 2023
1 parent c177b1f commit 3217782
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 31 deletions.
2 changes: 1 addition & 1 deletion lending/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 0 additions & 30 deletions lending/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
49 changes: 49 additions & 0 deletions lending/uninstall.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 3217782

Please sign in to comment.