diff --git a/frappe/desk/doctype/workspace/workspace.py b/frappe/desk/doctype/workspace/workspace.py index c45b249cf7..40c75bc697 100644 --- a/frappe/desk/doctype/workspace/workspace.py +++ b/frappe/desk/doctype/workspace/workspace.py @@ -127,6 +127,8 @@ class Workspace(Document): def on_trash(self): if self.public and not is_workspace_manager(): frappe.throw(_("You need to be Workspace Manager to delete a public workspace.")) + self.delete_desktop_icon() + self.delete_workspace_sidebar() self.delete_from_my_workspaces() def delete_from_my_workspaces(self): @@ -143,6 +145,25 @@ class Workspace(Document): if self.module and frappe.conf.developer_mode: delete_folder(self.module, "Workspace", self.title) + def delete_desktop_icon(self): + if self.public: + desktop_icon = frappe.get_all( + "Desktop Icon", + filters=[{"link_type": "Workspace"}, {"link_to": self.name}], + limit=1, + pluck="name", + ) + if desktop_icon: + frappe.delete_doc("Desktop Icon", desktop_icon[0]) + + def delete_workspace_sidebar(self): + if self.public: + workspace_sidebar = frappe.get_all( + "Workspace Sidebar", filters=[{"name": self.name}], limit=1, pluck="name" + ) + if workspace_sidebar: + frappe.delete_doc("Workspace Sidebar", workspace_sidebar[0]) + @staticmethod def get_module_wise_workspaces(): workspaces = frappe.get_all( diff --git a/frappe/migrate.py b/frappe/migrate.py index 623fa1bcbe..043193e5ad 100644 --- a/frappe/migrate.py +++ b/frappe/migrate.py @@ -182,6 +182,9 @@ class SiteMigration: print("Removing orphan doctypes...") frappe.model.sync.remove_orphan_doctypes() + frappe.model.sync.remove_orphan_entities() + + frappe.model.sync.delete_duplicate_icons() print("Syncing portal menu...") frappe.get_single("Portal Settings").sync_menu() diff --git a/frappe/model/sync.py b/frappe/model/sync.py index 4d6b01c14b..c177b9af17 100644 --- a/frappe/model/sync.py +++ b/frappe/model/sync.py @@ -197,3 +197,60 @@ def remove_orphan_doctypes(): update_progress_bar("Deleting orphaned DocTypes", i, len(orphan_doctypes)) frappe.db.commit() print() + + +def remove_orphan_entities(): + entites = ["Workspace", "Dashboard", "Page", "Report"] + entity_filter_map = { + "Workspace": {"public": 1}, + "Page": {"standard": "Yes"}, + "Report": {"is_standard": "Yes"}, + "Dashboard": {"is_standard": True}, + } + for entity in entites: + print(f"Removing orphan {entity}s") + all_enitities = frappe.get_all( + entity, filters=entity_filter_map.get(entity), fields=["name", "module"] + ) + for i, w in enumerate(all_enitities): + if w.module: + try: + module_path = frappe.get_module_path(w.module) + if not check_if_record_exists( + type="module", path=module_path, entity_type=entity, name=w.name + ): + print(f"Deleting entity {entity} {w.name}") + frappe.delete_doc(entity, w.name, force=True, ignore_missing=True) + update_progress_bar(f"Deleting orphaned {entity}", i, len(all_enitities)) + print() + + except Exception as e: + print(e) + # save the deleted icons + frappe.db.commit() # nosemgrep + + +def check_if_record_exists(type=None, path=None, entity_type=None, name=None): + scrubbed_name = frappe.scrub(name.lower()) + scrubbed_entity_type = frappe.scrub(entity_type.lower()) + if type == "app": + entity_path = os.path.join(path, scrubbed_entity_type, f"{scrubbed_name}.json") + else: + entity_path = os.path.join(path, scrubbed_entity_type, scrubbed_name, f"{scrubbed_name}.json") + return os.path.exists(entity_path) + + +def delete_duplicate_icons(): + # This handles app icons which are renamed. Removes the old entry from db. + for app in frappe.get_installed_apps(): + icons = frappe.get_all("Desktop Icon", filters=[{"icon_type": "App"}, {"app": app}], pluck="name") + + if len(icons) > 1: + for i in icons: + app_path = frappe.get_app_path(app) + if not check_if_record_exists(type="app", path=app_path, entity_type="Desktop Icon", name=i): + print(f"Deleting icon {i}") + frappe.delete_doc("Desktop Icon", i) + + # save the deleted icons + frappe.db.commit() # nosemgrep