From 99734d82d39c3dd36ac904ce3cfb0eb5dd6294bb Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Thu, 12 Jun 2014 18:51:58 +0530 Subject: [PATCH] added feature to add print formats to reports using john resig's microtemplates --- frappe/public/build.json | 2 +- frappe/public/js/frappe/misc/utils.js | 9 ++++++ frappe/public/js/frappe/views/query_report.js | 30 ++++++++++++++++++- frappe/widgets/query_report.py | 19 +++++++----- 4 files changed, 51 insertions(+), 9 deletions(-) diff --git a/frappe/public/build.json b/frappe/public/build.json index 53fb50072b..7e122a7279 100644 --- a/frappe/public/build.json +++ b/frappe/public/build.json @@ -32,7 +32,6 @@ "public/css/tag-it.css", "public/css/bootstrap.css", - "public/css/bootstrap-responsive.css", "public/css/font-awesome.css", "public/css/desk.css", "public/css/appframe.css", @@ -51,6 +50,7 @@ "public/js/lib/center_image.js", "public/js/lib/bootstrap.min.js", "public/js/lib/nprogress.js", + "public/js/lib/microtemplate.js", "public/js/lib/beautify-html.js", "public/js/lib/moment/moment.min.js", "public/js/lib/moment/moment-timezone.min.js", diff --git a/frappe/public/js/frappe/misc/utils.js b/frappe/public/js/frappe/misc/utils.js index 5096536c80..3da001d209 100644 --- a/frappe/public/js/frappe/misc/utils.js +++ b/frappe/public/js/frappe/misc/utils.js @@ -247,5 +247,14 @@ frappe.utils = { var dataURL = canvas.toDataURL("image/jpeg"); setTimeout(function() { callback(dataURL); }, 10 ); } + }, + + with_print_template: function(fn) { + if(!frappe.print_template) { + $.get("/assets/frappe/html/print_template.html?q=4", + function(html) { frappe.print_template = html; fn(); }); + } else { + fn(); + } } }; diff --git a/frappe/public/js/frappe/views/query_report.js b/frappe/public/js/frappe/views/query_report.js index cdb3f67d19..52c8ef5012 100644 --- a/frappe/public/js/frappe/views/query_report.js +++ b/frappe/public/js/frappe/views/query_report.js @@ -107,8 +107,9 @@ frappe.views.QueryReport = Class.extend({ }, callback: function(r) { me.appframe.set_title(__("Query Report")+": " + __(me.report_name)); - frappe.dom.eval(r.message || ""); + frappe.dom.eval(r.message.script || ""); me.setup_filters(); + me.setup_html_format(r.message.html_format); me.refresh(); } }); @@ -124,6 +125,33 @@ frappe.views.QueryReport = Class.extend({ this.wrapper.find(".no-report-area").html(msg).toggle(true); } }, + setup_html_format: function(html_format) { + var me = this; + if(html_format) { + this.appframe.add_primary_action(__('Print'), function() { + if(!me.data) { + msgprint(__("Run the report first")); + return; + } + frappe.utils.with_print_template(function() { + var data = []; + $.each(me.data, function(i, d) { + var newd = {}; data.push(newd); + $.each(d, function(k, v) { + newd[k.replace(/ /g, "_").toLowerCase()] = v; }); + }); + var content = tmpl.render(html_format, {data: data, filters:me.get_values(), report:me}); + + var html = $.format(frappe.print_template, [ + __(me.report_name), content]); + var w = window.open(); + w.document.write(html); + w.document.close(); + }) + }, "icon-print"); + + } + }, setup_filters: function() { this.clear_filters(); var me = this; diff --git a/frappe/widgets/query_report.py b/frappe/widgets/query_report.py index a81dcb1a1f..8d1b859870 100644 --- a/frappe/widgets/query_report.py +++ b/frappe/widgets/query_report.py @@ -5,7 +5,6 @@ from __future__ import unicode_literals import frappe import os, json -import types from frappe import _ from frappe.modules import scrub, get_module_path @@ -31,11 +30,16 @@ def get_script(report_name): module_path = get_module_path(module) report_folder = os.path.join(module_path, "report", scrub(report.name)) script_path = os.path.join(report_folder, scrub(report.name) + ".js") + print_path = os.path.join(report_folder, scrub(report.name) + ".html") - script = None + script, html_format = None, None if os.path.exists(script_path): - with open(script_path, "r") as script: - script = script.read() + with open(script_path, "r") as f: + script = f.read() + + if os.path.exists(print_path): + with open(print_path, "r") as f: + html_format = f.read() if not script and report.javascript: script = report.javascript @@ -47,7 +51,10 @@ def get_script(report_name): if frappe.lang != "en": frappe.response["__messages"] = frappe.get_lang_dict("report", report_name) - return script + return { + "script": script, + "html_format": html_format + } @frappe.whitelist() def run(report_name, filters=()): @@ -148,8 +155,6 @@ def get_linked_doctypes(columns): def get_user_match_filters(doctypes, ref_doctype): match_filters = {} - doctypes_meta = {} - tables = [] for dt in doctypes: match_filters.update(frappe.widgets.reportview.build_match_conditions(dt, False))