Merge pull request #35136 from sokumon/refactor-auto-generate

This commit is contained in:
Soham Kulkarni 2025-12-09 19:09:42 +05:30 committed by GitHub
commit ec04d47bed
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 133 additions and 134 deletions

View file

@ -528,7 +528,7 @@ def get_sentry_dsn():
def get_sidebar_items():
from frappe.utils.install import auto_generate_sidebar_from_module
from frappe.desk.doctype.workspace_sidebar.workspace_sidebar import auto_generate_sidebar_from_module
sidebars = frappe.get_all(
"Workspace Sidebar", fields=["name", "header_icon"], filters={"name": ["not like", "%My Workspaces%"]}

View file

@ -12,6 +12,7 @@ from frappe.boot import get_allowed_pages, get_allowed_reports
from frappe.model.document import Document
from frappe.modules.export_file import strip_default_fields
from frappe.modules.utils import create_directory_on_app_path
from frappe.utils.caching import site_cache
class WorkspaceSidebar(Document):
@ -240,3 +241,134 @@ def add_to_my_workspace(workspace):
except Exception as e:
frappe.log_error(title="Error in Adding Private Workspaces", message=e)
@site_cache()
def auto_generate_sidebar_from_module():
"""Auto generate sidebar from module"""
sidebars = []
for module in frappe.get_all("Module Def", pluck="name"):
if not (
frappe.db.exists("Workspace Sidebar", {"module": module})
or frappe.db.exists("Workspace Sidebar", {"name": module})
):
module_info = get_module_info(module)
sidebar_items = create_sidebar_items(module_info)
sidebar = frappe.new_doc("Workspace Sidebar")
sidebar.title = module
sidebar.items = sidebar_items
sidebar.module = module
sidebar.header_icon = "hammer"
sidebar.app = frappe.local.module_app.get(frappe.scrub(module), None)
sidebars.append(sidebar)
return sidebars
def get_module_info(module_name):
entities = ["Workspace", "Dashboard", "DocType", "Report", "Page"]
module_info = {}
for entity in entities:
module_info[entity] = {}
filters = [{"module": module_name}]
pluck = "name"
fieldnames = ["name"]
if entity.lower() == "doctype":
filters.append({"istable": 0})
if entity.lower() == "page":
fieldnames.append("title")
pluck = None
module_info[entity] = frappe.get_all(
entity, filters=filters, fields=fieldnames, pluck=pluck, order_by="creation asc"
)
# if module info has no workspaces, then move doctypes to the front
if not module_info.get("Workspace"):
module_info = {
"DocType": module_info.get("DocType"),
"Workspace": module_info.get("Workspace"),
"Report": module_info.get("Report"),
"Dashboard": module_info.get("Dashboard"),
"Page": module_info.get("Page"),
}
top_doctypes = choose_top_doctypes(module_info.get("DocType"))
if top_doctypes:
module_info["DocType"] = choose_top_doctypes(module_info.get("DocType"))
return module_info
def choose_top_doctypes(doctype_names):
doctype_limit = 3
if len(doctype_names) > doctype_limit:
try:
doctype_count_map = {}
for doctype in doctype_names:
doctype_count_map[doctype] = frappe.db.count(doctype)
top_doctypes = [
name
for name, count in sorted(doctype_count_map.items(), key=lambda x: x[1], reverse=True)[
:doctype_limit
]
]
return top_doctypes
except frappe.db.ProgrammingError:
# catches table not found errors
return None
def create_sidebar_items(module_info):
sidebar_items = []
idx = 1
section_entities = {"report": "Reports", "dashboard": "Dashboards", "page": "Pages"}
for entity, items in module_info.items():
section_break_added = False
entity_lower = entity.lower()
if entity_lower in section_entities:
if entity_lower == "report":
section_break = add_section_breaks("Reports", idx)
elif entity_lower in ("dashboard", "page") and len(items) > 1:
section_break = add_section_breaks(section_entities[entity_lower], idx)
section_break_added = True
sidebar_items.append(section_break)
idx += 1
for item in items:
item_info = {"label": item, "type": "Link", "link_type": entity, "link_to": item, "idx": idx}
if entity_lower == "report":
item_info["child"] = 1
item_info["icon"] = "table"
if entity_lower == "page":
item_info["label"] = item.get("title")
item_info["link_to"] = item.get("name")
if entity_lower == "workspace":
item_info["icon"] = "home"
item_info["icon"] = "wallpaper"
if entity_lower == "page":
item_info["icon"] = "panel-top"
if entity_lower == "doctype" and "settings" in item.lower():
item_info["icon"] = "settings"
if section_break_added:
item_info["child"] = 1
sidebar_item = frappe.new_doc("Workspace Sidebar Item")
sidebar_item.update(item_info)
sidebar_items.append(sidebar_item)
idx += 1
return sidebar_items
def add_section_breaks(label, idx):
section_break = frappe.new_doc("Workspace Sidebar Item")
section_break.update({"label": label, "type": "Section Break", "idx": idx})
return section_break

View file

@ -5,7 +5,6 @@ import getpass
import frappe
from frappe.geo.doctype.country.country import import_country_and_currency
from frappe.utils import cint
from frappe.utils.caching import site_cache
from frappe.utils.password import update_password
@ -222,135 +221,3 @@ def delete_desktop_icon_and_sidebar(app_name, dry_run=False):
if dry_run:
# Delete icons and sidebars
frappe.db.commit() # nosemgrep
@site_cache()
def auto_generate_sidebar_from_module():
"""Auto generate sidebar from module"""
sidebars = []
for module in frappe.get_all("Module Def", pluck="name"):
if not (
frappe.db.exists("Workspace Sidebar", {"module": module})
or frappe.db.exists("Workspace Sidebar", {"name": module})
):
module_info = get_module_info(module)
sidebar_items = create_sidebar_items(module_info)
sidebar = frappe.new_doc("Workspace Sidebar")
sidebar.title = module
sidebar.items = sidebar_items
sidebar.module = module
sidebar.header_icon = "hammer"
sidebar.app = frappe.local.module_app.get(frappe.scrub(module), None)
sidebars.append(sidebar)
return sidebars
def get_module_info(module_name):
entities = ["Workspace", "Dashboard", "DocType", "Report", "Page"]
module_info = {}
for entity in entities:
module_info[entity] = {}
filters = [{"module": module_name}]
pluck = "name"
fieldnames = ["name"]
if entity.lower() == "doctype":
filters.append({"istable": 0})
if entity.lower() == "page":
fieldnames.append("title")
pluck = None
module_info[entity] = frappe.get_all(
entity, filters=filters, fields=fieldnames, pluck=pluck, order_by="creation asc"
)
# if module info has no workspaces, then move doctypes to the front
if not module_info.get("Workspace"):
module_info = {
"DocType": module_info.get("DocType"),
"Workspace": module_info.get("Workspace"),
"Report": module_info.get("Report"),
"Dashboard": module_info.get("Dashboard"),
"Page": module_info.get("Page"),
}
top_doctypes = choose_top_doctypes(module_info.get("DocType"))
if top_doctypes:
module_info["DocType"] = choose_top_doctypes(module_info.get("DocType"))
return module_info
def choose_top_doctypes(doctype_names):
doctype_limit = 3
if len(doctype_names) > doctype_limit:
try:
doctype_count_map = {}
for doctype in doctype_names:
doctype_count_map[doctype] = frappe.db.count(doctype)
top_doctypes = [
name
for name, count in sorted(doctype_count_map.items(), key=lambda x: x[1], reverse=True)[
:doctype_limit
]
]
return top_doctypes
except frappe.db.ProgrammingError:
# catches table not found errors
return None
def create_sidebar_items(module_info):
sidebar_items = []
idx = 1
section_entities = {"report": "Reports", "dashboard": "Dashboards", "page": "Pages"}
for entity, items in module_info.items():
section_break_added = False
entity_lower = entity.lower()
if entity_lower in section_entities:
if entity_lower == "report":
section_break = add_section_breaks("Reports", idx)
elif entity_lower in ("dashboard", "page") and len(items) > 1:
section_break = add_section_breaks(section_entities[entity_lower], idx)
section_break_added = True
sidebar_items.append(section_break)
idx += 1
for item in items:
print(entity, item)
item_info = {"label": item, "type": "Link", "link_type": entity, "link_to": item, "idx": idx}
if entity_lower == "report":
item_info["child"] = 1
item_info["icon"] = "table"
if entity_lower == "page":
item_info["label"] = item.get("title")
item_info["link_to"] = item.get("name")
if entity_lower == "workspace":
item_info["icon"] = "home"
item_info["icon"] = "wallpaper"
if entity_lower == "page":
item_info["icon"] = "panel-top"
if entity_lower == "doctype" and "settings" in item.lower():
item_info["icon"] = "settings"
if section_break_added:
item_info["child"] = 1
sidebar_item = frappe.new_doc("Workspace Sidebar Item")
sidebar_item.update(item_info)
sidebar_items.append(sidebar_item)
idx += 1
return sidebar_items
def add_section_breaks(label, idx):
section_break = frappe.new_doc("Workspace Sidebar Item")
section_break.update({"label": label, "type": "Section Break", "idx": idx})
return section_break