feat: save dashboard chart config for user

This commit is contained in:
prssanna 2020-04-01 00:09:40 +05:30
parent c1533cfb95
commit f79a43d5f3
9 changed files with 202 additions and 43 deletions

View file

@ -80,23 +80,27 @@ class Dashboard {
if (!charts.length) {
frappe.msgprint(__('No Permitted Charts on this Dashboard'), __('No Permitted Charts'))
}
this.charts = charts
.map(chart => {
return {
chart_name: chart.chart,
label: chart.chart,
...chart
}
});
this.chart_group = new frappe.widget.WidgetGroup({
title: null,
container: this.container,
type: "chart",
columns: 2,
allow_sorting: false,
widgets: this.charts,
});
frappe.dashboard_utils.get_dashboard_settings().then((settings) => {
let chart_config = settings.chart_config? JSON.parse(settings.chart_config): {};
this.charts =
charts.map(chart => {
return {
chart_name: chart.chart,
label: chart.chart,
chart_settings: chart_config[chart.chart] || {},
...chart
}
});
this.chart_group = new frappe.widget.WidgetGroup({
title: null,
container: this.container,
type: "chart",
columns: 2,
allow_sorting: false,
widgets: this.charts,
});
})
});
}

View file

@ -0,0 +1,8 @@
// Copyright (c) 2020, Frappe Technologies and contributors
// For license information, please see license.txt
frappe.ui.form.on('Dashboard Settings', {
// refresh: function(frm) {
// }
});

View file

@ -0,0 +1,51 @@
{
"actions": [],
"autoname": "Prompt",
"creation": "2020-03-31 19:41:45.785014",
"doctype": "DocType",
"editable_grid": 1,
"engine": "InnoDB",
"field_order": [
"user",
"chart_config"
],
"fields": [
{
"fieldname": "user",
"fieldtype": "Link",
"label": "User",
"options": "User",
"read_only": 1
},
{
"fieldname": "chart_config",
"fieldtype": "Code",
"label": "Chart Configuration",
"options": "JSON",
"read_only": 1
}
],
"in_create": 1,
"links": [],
"modified": "2020-04-01 00:07:26.489561",
"modified_by": "Administrator",
"module": "Desk",
"name": "Dashboard Settings",
"owner": "Administrator",
"permissions": [
{
"email": 1,
"export": 1,
"print": 1,
"read": 1,
"report": 1,
"role": "All",
"share": 1,
"write": 1
}
],
"read_only": 1,
"sort_field": "modified",
"sort_order": "DESC",
"track_changes": 1
}

View file

@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2020, Frappe Technologies and contributors
# For license information, please see license.txt
from __future__ import unicode_literals
# import frappe
from frappe.model.document import Document
import frappe
import json
class DashboardSettings(Document):
pass
@frappe.whitelist()
def create_dashboard_settings(user):
if not frappe.db.exists("Dashboard Settings", user):
doc = frappe.new_doc('Dashboard Settings')
doc.name = user
doc.insert(ignore_permissions=True)
frappe.db.commit()
return doc
def get_permission_query_conditions(user):
if not user: user = frappe.session.user
return '''(`tabDashboard Settings`.name = '{user}')'''.format(user=user)
@frappe.whitelist()
def save_chart_config(config, chart_name):
config = frappe.parse_json(config)
doc = frappe.get_doc('Dashboard Settings', frappe.session.user)
chart_config = frappe.parse_json(doc.chart_config) or {}
if not chart_name in chart_config:
chart_config[chart_name] = {}
chart_config[chart_name].update(config)
frappe.db.set_value('Dashboard Settings', frappe.session.user, 'chart_config', json.dumps(chart_config))

View file

@ -86,6 +86,7 @@ permission_query_conditions = {
"Event": "frappe.desk.doctype.event.event.get_permission_query_conditions",
"ToDo": "frappe.desk.doctype.todo.todo.get_permission_query_conditions",
"User": "frappe.core.doctype.user.user.get_permission_query_conditions",
"Dashboard Settings": "frappe.desk.doctype.dashboard_settings.dashboard_settings.get_permission_query_conditions",
"Notification Log": "frappe.desk.doctype.notification_log.notification_log.get_permission_query_conditions",
"Dashboard Chart": "frappe.desk.doctype.dashboard_chart.dashboard_chart.get_permission_query_conditions",
"Notification Settings": "frappe.desk.doctype.notification_settings.notification_settings.get_permission_query_conditions",

View file

@ -54,5 +54,26 @@ frappe.dashboard_utils = {
} else {
return Promise.resolve();
}
},
get_dashboard_settings() {
return frappe.model.with_doc('Dashboard Settings', frappe.session.user).then(settings => {
if (!settings) {
return this.create_dashboard_settings().then(settings => {
return settings;
});
} else {
return settings;
}
});
},
create_dashboard_settings() {
return frappe.xcall(
'frappe.desk.doctype.dashboard_settings.dashboard_settings.create_dashboard_settings',
{user: frappe.session.user}
).then(settings => {
return settings;
});
}
};

View file

@ -165,11 +165,18 @@ class DesktopPage {
this.allow_customization = this.data.allow_customization || false;
!this.sections["onboarding"] &&
this.data.charts.items.length &&
this.make_charts();
this.data.shortcuts.items.length && this.make_shortcuts();
this.data.cards.items.length && this.make_cards();
let create_shortcuts_and_cards = () => {
this.data.shortcuts.items.length && this.make_shortcuts();
this.data.cards.items.length && this.make_cards();
}
if (!this.sections["onboarding"] && this.data.charts.items.length) {
this.make_charts().then(() => {
create_shortcuts_and_cards();
});
} else {
create_shortcuts_and_cards();
}
});
}
@ -224,13 +231,22 @@ class DesktopPage {
}
make_charts() {
this.sections["charts"] = new frappe.widget.WidgetGroup({
title: this.data.charts.label || `${this.page_name} Dashboard`,
container: this.page,
type: "chart",
columns: 1,
allow_sorting: false,
widgets: this.data.charts.items
return frappe.dashboard_utils.get_dashboard_settings().then(settings => {
let chart_config = settings.chart_config? JSON.parse(settings.chart_config): {};
if (this.data.charts.items) {
this.data.charts.items.map(chart => {
chart.chart_settings = chart_config[chart.chart_name] || {}
});
}
this.sections["charts"] = new frappe.widget.WidgetGroup({
title: this.data.charts.label || `${this.page_name} Dashboard`,
container: this.page,
type: "chart",
columns: 1,
allow_sorting: false,
widgets: this.data.charts.items
});
});
}

View file

@ -62,6 +62,9 @@ export default class ChartWidget extends Widget {
make_chart() {
this.get_settings().then(() => {
if (!this.chart_settings) {
this.chart_settings = {};
}
this.setup_container();
this.prepare_chart_object();
this.action_area.empty();
@ -89,7 +92,7 @@ export default class ChartWidget extends Widget {
render_time_series_filters() {
let filters = [
{
label: this.chart_doc.timespan,
label: this.chart_settings.timespan || this.chart_doc.timespan,
options: [
"Select Date Range",
"Last Year",
@ -116,13 +119,16 @@ export default class ChartWidget extends Widget {
this.fetch_and_update_chart();
}
this.save_chart_config_for_user({'timespan': this.selected_timespan})
}
},
{
label: this.chart_doc.time_interval,
label: this.chart_settings.time_interval || this.chart_doc.time_interval,
options: ["Yearly", "Quarterly", "Monthly", "Weekly", "Daily"],
action: selected_item => {
this.selected_time_interval = selected_item;
this.save_chart_config_for_user({'time_interval': this.selected_time_interval})
this.fetch_and_update_chart();
}
}
@ -138,10 +144,10 @@ export default class ChartWidget extends Widget {
fetch_and_update_chart() {
this.args = {
timespan: this.selected_timespan,
time_interval: this.selected_time_interval,
from_date: this.selected_from_date,
to_date: this.selected_to_date
timespan: this.selected_timespan || this.chart_settings.timespan,
time_interval: this.selected_time_interval || this.chart_settings.time_interval,
from_date: this.selected_from_date || this.chart_settings.from_date,
to_date: this.selected_to_date || this.chart_settings.to_date
};
this.fetch(this.filters, true, this.args).then(data => {
@ -176,16 +182,18 @@ export default class ChartWidget extends Widget {
fieldname: "from_date",
placeholder: "Date Range",
input_class: "input-xs",
default: [this.chart_settings.from_date, this.chart_settings.to_date],
reqd: 1,
change: () => {
let selected_date_range = this.date_range_field.get_value();
this.selected_from_date = selected_date_range[0];
this.selected_to_date = selected_date_range[1];
if (
selected_date_range &&
selected_date_range.length == 2
) {
if (selected_date_range && selected_date_range.length == 2) {
this.save_chart_config_for_user({
'from_date': this.selected_from_date,
'to_date': this.selected_to_date,
});
this.fetch_and_update_chart();
}
}
@ -334,6 +342,7 @@ export default class ChartWidget extends Widget {
} else {
me.filters = values;
}
me.save_chart_config_for_user({'filters': me.filters});
me.fetch_and_update_chart();
}
},
@ -350,6 +359,15 @@ export default class ChartWidget extends Widget {
dialog.set_values(this.filters);
}
save_chart_config_for_user(config) {
Object.assign(this.chart_settings, config);
frappe.xcall('frappe.desk.doctype.dashboard_settings.dashboard_settings.save_chart_config',
{
'config': this.chart_settings,
'chart_name': this.chart_doc.chart_name
});
}
create_filter_group_and_add_filters(parent) {
this.filter_group = new frappe.ui.FilterGroup({
parent: parent,
@ -406,10 +424,10 @@ export default class ChartWidget extends Widget {
filters: filters,
refresh: refresh ? 1 : 0,
time_interval:
args && args.time_interval ? args.time_interval : null,
timespan: args && args.timespan ? args.timespan : null,
from_date: args && args.from_date ? args.from_date : null,
to_date: args && args.to_date ? args.to_date : null
args && args.time_interval? args.time_interval: null,
timespan: args && args.timespan? args.timespan: null,
from_date: args && args.from_date? args.from_date: null,
to_date: args && args.to_date? args.to_date: null
};
}
return frappe.xcall(method, args);
@ -481,8 +499,9 @@ export default class ChartWidget extends Widget {
}
prepare_chart_object() {
let saved_filters = this.chart_settings.filters || null;
this.filters =
this.filters || JSON.parse(this.chart_doc.filters_json || "[]");
saved_filters || this.filters || JSON.parse(this.chart_doc.filters_json || "[]");
}
get_settings() {