feat: before_uninstall and after_uninstall hooks

This commit is contained in:
phot0n 2021-12-16 15:18:58 +05:30
parent dd336a2990
commit 1d879f741b
3 changed files with 14 additions and 1 deletions

View file

@ -1203,7 +1203,7 @@ def read_file(path, raise_not_found=False):
def get_attr(method_string):
"""Get python method object from its name."""
app_name = method_string.split(".")[0]
if not local.flags.in_install and app_name not in get_installed_apps():
if not local.flags.in_uninstall and not local.flags.in_install and app_name not in get_installed_apps():
throw(_("App {0} is not installed").format(app_name), AppNotInstalledError)
modulename = '.'.join(method_string.split('.')[:-1])

View file

@ -208,6 +208,7 @@ def remove_app(app_name, dry_run=False, yes=False, no_backup=False, force=False)
import click
site = frappe.local.site
app_hooks = frappe.get_hooks(app_name=app_name)
# dont allow uninstall app if not installed unless forced
if not force:
@ -233,6 +234,9 @@ def remove_app(app_name, dry_run=False, yes=False, no_backup=False, force=False)
frappe.flags.in_uninstall = True
for before_uninstall in app_hooks.before_uninstall or []:
frappe.get_attr(before_uninstall)()
modules = frappe.get_all("Module Def", filters={"app_name": app_name}, pluck="name")
drop_doctypes = _delete_modules(modules, dry_run=dry_run)
@ -243,6 +247,9 @@ def remove_app(app_name, dry_run=False, yes=False, no_backup=False, force=False)
frappe.get_single('Installed Applications').update_versions()
frappe.db.commit()
for after_uninstall in app_hooks.after_uninstall or []:
frappe.get_attr(after_uninstall)()
click.secho(f"Uninstalled App {app_name} from Site {site}", fg="green")
frappe.flags.in_uninstall = False

View file

@ -203,6 +203,12 @@ app_license = "{app_license}"
# before_install = "{app_name}.install.before_install"
# after_install = "{app_name}.install.after_install"
# Uninstallation
# ------------
# before_uninstall = "{app_name}.uninstall.before_uninstall"
# after_uninstall = "{app_name}.uninstall.after_uninstall"
# Desk Notifications
# ------------------
# See frappe.core.notifications.get_notification_config