From b3694b34cfb4fe9bb42da84ba9e4866021c2641a Mon Sep 17 00:00:00 2001 From: Shariq Ansari Date: Fri, 9 Aug 2024 12:12:53 +0530 Subject: [PATCH] refactor: common api to get apps with details --- frappe/apps.py | 41 +++++++++++++------ .../system_settings/system_settings.js | 3 +- frappe/core/doctype/user/user.js | 3 +- frappe/www/apps.py | 27 +----------- 4 files changed, 34 insertions(+), 40 deletions(-) diff --git a/frappe/apps.py b/frappe/apps.py index 22f04fc145..1963032ef4 100644 --- a/frappe/apps.py +++ b/frappe/apps.py @@ -4,20 +4,36 @@ import re import frappe +from frappe import _ @frappe.whitelist() def get_apps(): apps = frappe.get_installed_apps() - app_list = [] + app_list = [ + { + "name": "frappe", + "icon_url": "/assets/frappe/images/frappe-framework-logo.svg", + "title": _("Admin"), + "route": "/app", + } + ] for app in apps: if app == "frappe": - app_list.append(app) - else: - app_icon_url = frappe.get_hooks("app_icon_url", app_name=app) - app_icon_route = frappe.get_hooks("app_icon_route", app_name=app) - if app_icon_url and app_icon_route: - app_list.append(app) + continue + app_icon_url = frappe.get_hooks("app_icon_url", app_name=app) + app_icon_route = frappe.get_hooks("app_icon_route", app_name=app) + if app_icon_url and app_icon_route: + app_title = frappe.get_hooks("app_title", app_name=app) + icon_title = frappe.get_hooks("app_icon_title", app_name=app) + app_list.append( + { + "name": app, + "icon_url": app_icon_url[0], + "title": _(icon_title[0] if icon_title else app_title[0] if app_title else app), + "route": app_icon_route[0], + } + ) return app_list @@ -30,9 +46,10 @@ def get_route(app_name): def is_desk_apps(apps): for app in apps: - route = frappe.get_hooks(app_name=app).get("app_icon_route") + # check if route is /app or /app/* and not /app1 or /app1/* pattern = r"^/app(/.*)?$" - if route and not re.match(pattern, route[0]): + route = app.get("route") + if route and not re.match(pattern, route): return False return True @@ -46,11 +63,9 @@ def get_default_path(): return get_route(user_default_app) apps = get_apps() - _apps = [app for app in apps if app != "frappe"] + _apps = [app for app in apps if app.get("name") != "frappe"] if len(_apps) == 1: - first_app = _apps[0] - if first_app: - return get_route(first_app) + return _apps[0].get("route") or "/apps" elif is_desk_apps(_apps): return "/app" return "/apps" diff --git a/frappe/core/doctype/system_settings/system_settings.js b/frappe/core/doctype/system_settings/system_settings.js index 9a1d045dce..e69470861a 100644 --- a/frappe/core/doctype/system_settings/system_settings.js +++ b/frappe/core/doctype/system_settings/system_settings.js @@ -18,7 +18,8 @@ frappe.ui.form.on("System Settings", { }); frappe.xcall("frappe.apps.get_apps").then((r) => { - frm.set_df_property("default_app", "options", [" ", ...r]); + let apps = r?.map((r) => r.name) || []; + frm.set_df_property("default_app", "options", [" ", ...apps]); }); frm.trigger("set_rounding_method_options"); diff --git a/frappe/core/doctype/user/user.js b/frappe/core/doctype/user/user.js index cd82ad2edb..5f7ba80321 100644 --- a/frappe/core/doctype/user/user.js +++ b/frappe/core/doctype/user/user.js @@ -104,7 +104,8 @@ frappe.ui.form.on("User", { let doc = frm.doc; frappe.xcall("frappe.apps.get_apps").then((r) => { - frm.set_df_property("default_app", "options", [" ", ...r]); + let apps = r?.map((r) => r.name) || []; + frm.set_df_property("default_app", "options", [" ", ...apps]); }); if (frm.is_new()) { diff --git a/frappe/www/apps.py b/frappe/www/apps.py index afeb36626f..a8dddefa38 100644 --- a/frappe/www/apps.py +++ b/frappe/www/apps.py @@ -3,7 +3,7 @@ import frappe from frappe import _ -from frappe.website.utils import get_home_page +from frappe.apps import get_apps def get_context(): @@ -13,27 +13,4 @@ def get_context(): if frappe.session.data.user_type == "Website User": frappe.throw(_("You are not permitted to access this page."), frappe.PermissionError) - _apps = frappe.get_installed_apps() - app_shortcuts = [ - { - "name": "frappe", - "icon_url": "/assets/frappe/images/frappe-framework-logo.svg", - "title": _("Admin"), - "route": "/app", - } - ] - for app in _apps: - app_icon_url = frappe.get_hooks("app_icon_url", app_name=app) - app_icon_route = frappe.get_hooks("app_icon_route", app_name=app) - if app_icon_url and app_icon_route: - app_title = frappe.get_hooks("app_title", app_name=app) - icon_title = frappe.get_hooks("app_icon_title", app_name=app) - app_shortcuts.append( - { - "name": app, - "icon_url": app_icon_url[0], - "title": _(icon_title[0] if icon_title else app_title[0] if app_title else app), - "route": app_icon_route[0], - } - ) - return {"apps": app_shortcuts} + return {"apps": get_apps()}