From e6a986559e2f5f7d1a22bc34408e15d4586e2060 Mon Sep 17 00:00:00 2001 From: prssanna Date: Fri, 10 Jul 2020 19:25:00 +0530 Subject: [PATCH] fix(patch): create new docs for customized standard dashboards --- frappe/patches.txt | 3 +- ...eate_custom_dashboards_cards_and_charts.py | 46 +++++++++++++++++++ frappe/utils/dashboard.py | 20 ++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 frappe/patches/v13_0/create_custom_dashboards_cards_and_charts.py diff --git a/frappe/patches.txt b/frappe/patches.txt index cfcbc1ceba..c936fd7398 100644 --- a/frappe/patches.txt +++ b/frappe/patches.txt @@ -292,4 +292,5 @@ execute:frappe.delete_doc("DocType", "Onboarding Slide Field") execute:frappe.delete_doc("DocType", "Onboarding Slide Help Link") frappe.patches.v13_0.update_date_filters_in_user_settings frappe.patches.v13_0.update_duration_options -frappe.patches.v13_0.replace_old_data_import # 2020-06-24 \ No newline at end of file +frappe.patches.v13_0.replace_old_data_import # 2020-06-24 +frappe.patches.v13_0.create_custom_dashboards_cards_and_charts \ No newline at end of file diff --git a/frappe/patches/v13_0/create_custom_dashboards_cards_and_charts.py b/frappe/patches/v13_0/create_custom_dashboards_cards_and_charts.py new file mode 100644 index 0000000000..750c0e3d7c --- /dev/null +++ b/frappe/patches/v13_0/create_custom_dashboards_cards_and_charts.py @@ -0,0 +1,46 @@ +import frappe +import json +from frappe.model.naming import append_number_if_name_exists +from frappe.utils.dashboard import get_dashboards_with_link + +def execute(): + if not frappe.db.table_exists('Dashboard Chart')\ + or not frappe.db.table_exists('Number Card')\ + or not frappe.db.table_exists('Dashboard'): + return + + frappe.reload_doc('desk', 'doctype', 'dashboard_chart') + frappe.reload_doc('desk', 'doctype', 'number_card') + frappe.reload_doc('desk', 'doctype', 'dashboard') + + modified_charts = get_modified_docs('Dashboard Chart') + modified_cards = get_modified_docs('Number Card') + modified_dashboards = [doc.name for doc in get_modified_docs('Dashboard')] + + for chart in modified_charts: + modified_dashboards += get_dashboards_with_link(chart.name, 'Dashboard Chart') + rename_modified_doc(chart.name, 'Dashboard Chart') + + for card in modified_cards: + modified_dashboards += get_dashboards_with_link(card.name, 'Number Card') + rename_modified_doc(card.name, 'Number Card') + + modified_dashboards = list(set(modified_dashboards)) + + for dashboard in modified_dashboards: + rename_modified_doc(dashboard, 'Dashboard') + +def get_modified_docs(doctype): + return frappe.get_all(doctype, + filters = { + 'owner': 'Administrator', + 'modified_by': ['!=', 'Administrator'] + }) + +def rename_modified_doc(docname, doctype): + new_name = docname + ' Custom' + try: + frappe.rename_doc(doctype, docname, new_name) + except: + new_name = append_number_if_name_exists(doctype, new_name) + frappe.rename_doc(doctype, docname, new_name) diff --git a/frappe/utils/dashboard.py b/frappe/utils/dashboard.py index 1ff6380973..7eaf470767 100644 --- a/frappe/utils/dashboard.py +++ b/frappe/utils/dashboard.py @@ -76,6 +76,26 @@ def get_from_date_from_timespan(to_date, timespan): return add_to_date(to_date, years=years, months=months, days=days, as_datetime=True) +def get_dashboards_with_link(docname, doctype): + dashboards = [] + links = [] + + if doctype == 'Dashboard Chart': + links = frappe.get_all('Dashboard Chart Link', + fields = ['parent'], + filters = { + 'chart': docname + }) + elif doctype == 'Number Card': + links = frappe.get_all('Number Card Link', + fields = ['parent'], + filters = { + 'card': docname + }) + + dashboards = [link.parent for link in links] + return dashboards + def sync_dashboards(app=None): """Import, overwrite fixtures from `[app]/fixtures`""" if not cint(frappe.db.get_single_value('System Settings', 'setup_complete')):