Skip to content

Commit

Permalink
Merge pull request #3 from CodeVenturers/vishnu
Browse files Browse the repository at this point in the history
chore: validate template
  • Loading branch information
Vishnu7025 authored Sep 4, 2024
2 parents f2875b1 + 863f336 commit 5e414bd
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 207 deletions.
24 changes: 18 additions & 6 deletions report_print/overrides/report.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2024, Codeventurers
# Copyright (c) 2024, Code Venturers and contributors
# For license information, please see license.txt


Expand All @@ -7,6 +7,8 @@
from frappe.desk.query_report import get_report_doc
from frappe.modules import get_module_path, scrub
from frappe.model.utils import render_include
from frappe.utils import get_html_format



@frappe.whitelist()
Expand All @@ -19,15 +21,28 @@ def get_script(report_name):
# custom modules are virtual modules those exists in DB but not in disk.
module_path = "" if is_custom_module else get_module_path(module)
report_folder = module_path and os.path.join(module_path, "report", scrub(report.name))
script_path = report_folder and os.path.join(report_folder, scrub(report.name) + ".js")

report_override_js = frappe.get_hooks("report_override_js", {})
if report_override_js.get(report_name):
script_path = os.path.join(frappe.get_app_path("swarnam"), report_override_js.get(report_name)[0])
else:
script_path = report_folder and os.path.join(report_folder, scrub(report.name) + ".js")

script = None
if os.path.exists(script_path):
with open(script_path) as f:
script = f.read()
script += f"\n\n//# sourceURL={scrub(report.name)}.js"

html_format = get_html_format()
if report_print := frappe.db.get_value(
"Report Print Format", {"default": 1, "report": report.name, "disabled": 0}, "html"
):
html_format = report_print
else:
print_path = report_folder and os.path.join(
report_folder, scrub(report.name) + ".html"
)
html_format = get_html_format(print_path)

if not script and report.javascript:
script = report.javascript
Expand All @@ -43,6 +58,3 @@ def get_script(report_name):
"filters": report.filters,
"custom_report_name": report.name if report.get("is_custom_report") else None,
}

def get_html_format():
return frappe.db.get_value("Report Print Format", "", "html")
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@
// For license information, please see license.txt

frappe.ui.form.on('Report Print Format', {
// refresh: function(frm) {

// }
refresh: function(frm) {
if (!frm.doc.default){
frm.add_custom_button(__("Set as Default"), function () {
frm.call({
doc: frm.doc,
method: "make_default",
callback: function () {
frm.refresh();
},
});
});
}
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,13 @@
"engine": "InnoDB",
"field_order": [
"report",
"module",
"default_print_language",
"column_break_3",
"standard",
"custom_format",
"disabled",
"default",
"section_break_6",
"print_format_type",
"raw_printing",
"html",
"raw_commands",
"section_break_9",
"margin_top",
"margin_bottom",
"margin_left",
"margin_right",
"align_labels_right",
"show_section_headings",
"line_breaks",
"absolute_value",
"column_break_11",
"font_size",
"font",
"page_number",
"css_section",
"css",
"custom_html_help",
"section_break_13",
"print_format_help",
"format_data",
"print_format_builder",
"print_format_builder_beta"
"css"
],
"fields": [
{
Expand All @@ -52,162 +27,28 @@
"options": "Report",
"reqd": 1
},
{
"fieldname": "module",
"fieldtype": "Link",
"label": "Module",
"options": "Module Def"
},
{
"fieldname": "default_print_language",
"fieldtype": "Link",
"label": "Default Print Language",
"options": "Language"
},
{
"fieldname": "column_break_3",
"fieldtype": "Column Break"
},
{
"default": "No",
"fieldname": "standard",
"fieldtype": "Select",
"in_filter": 1,
"label": "Standard",
"no_copy": 1,
"oldfieldname": "standard",
"oldfieldtype": "Select",
"options": "No\nYes",
"reqd": 1,
"search_index": 1
},
{
"default": "0",
"fieldname": "custom_format",
"fieldtype": "Check",
"label": "Custom Format"
},
{
"default": "0",
"fieldname": "disabled",
"fieldtype": "Check",
"label": "Disabled"
},
{
"depends_on": "custom_format",
"fieldname": "section_break_6",
"fieldtype": "Section Break"
},
{
"default": "Jinja",
"depends_on": "custom_format",
"fieldname": "print_format_type",
"fieldtype": "Select",
"label": "Print Format Type",
"options": "Jinja\nJS"
},
{
"default": "0",
"fieldname": "raw_printing",
"fieldtype": "Check",
"label": "Raw Printing"
},
{
"depends_on": "eval:!doc.raw_printing",
"fieldname": "html",
"fieldtype": "Code",
"label": "HTML",
"oldfieldname": "html",
"oldfieldtype": "Text Editor",
"options": "Jinja"
},
{
"depends_on": "raw_printing",
"description": "Any string-based printer languages can be used. Writing raw commands requires knowledge of the printer's native language provided by the printer manufacturer. Please refer to the developer manual provided by the printer manufacturer on how to write their native commands. These commands are rendered on the server side using the Jinja Templating Language.",
"fieldname": "raw_commands",
"fieldtype": "Code",
"label": "Raw Commands",
"options": "Jinja"
},
{
"depends_on": "eval:!doc.custom_format",
"fieldname": "section_break_9",
"fieldtype": "Section Break",
"label": "Style Settings"
},
{
"default": "15",
"fieldname": "margin_top",
"fieldtype": "Float",
"label": "Margin Top"
},
{
"default": "15",
"fieldname": "margin_bottom",
"fieldtype": "Float",
"label": "Margin Bottom"
},
{
"default": "15",
"fieldname": "margin_left",
"fieldtype": "Float",
"label": "Margin Left"
},
{
"default": "15",
"fieldname": "margin_right",
"fieldtype": "Float",
"label": "Margin Right"
},
{
"default": "0",
"fieldname": "align_labels_right",
"fieldtype": "Check",
"label": "Align Labels to the Right"
},
{
"default": "0",
"fieldname": "show_section_headings",
"fieldtype": "Check",
"label": "Show Section Headings"
},
{
"default": "0",
"fieldname": "line_breaks",
"fieldtype": "Check",
"label": "Show Line Breaks after Sections"
},
{
"default": "0",
"depends_on": "doc_type",
"description": "If checked, negative numeric values of Currency, Quantity or Count would be shown as positive",
"fieldname": "absolute_value",
"fieldtype": "Check",
"label": "Show Absolute Values"
},
{
"fieldname": "column_break_11",
"fieldtype": "Column Break"
},
{
"default": "14",
"fieldname": "font_size",
"fieldtype": "Int",
"label": "Font Size"
},
{
"depends_on": "eval:!doc.custom_format",
"fieldname": "font",
"fieldtype": "Data",
"label": "Google Font"
},
{
"default": "Hide",
"fieldname": "page_number",
"fieldtype": "Select",
"label": "Page Number",
"options": "Hide\nTop Left\nTop Center\nTop Right\nBottom Left\nBottom Center\nBottom Right"
},
{
"depends_on": "eval:!doc.raw_printing",
"fieldname": "css_section",
Expand All @@ -219,48 +60,19 @@
"label": "Custom CSS",
"options": "CSS"
},
{
"fieldname": "custom_html_help",
"fieldtype": "HTML",
"label": "Custom HTML Help",
"options": "<h3>Custom CSS Help</h3>\n\n<p>Notes:</p>\n\n<ol>\n<li>All field groups (label + value) are set attributes <code>data-fieldtype</code> and <code>data-fieldname</code></li>\n<li>All values are given class <code>value</code></li>\n<li>All Section Breaks are given class <code>section-break</code></li>\n<li>All Column Breaks are given class <code>column-break</code></li>\n</ol>\n\n<h4>Examples</h4>\n\n<p>1. Left align integers</p>\n\n<pre><code>[data-fieldtype=\"Int\"] .value { text-align: left; }</code></pre>\n\n<p>1. Add border to sections except the last section</p>\n\n<pre><code>.section-break { padding: 30px 0px; border-bottom: 1px solid #eee; }\n.section-break:last-child { padding-bottom: 0px; border-bottom: 0px; }</code></pre>\n"
},
{
"depends_on": "custom_format",
"fieldname": "section_break_13",
"fieldtype": "Section Break"
},
{
"depends_on": "custom_format",
"fieldname": "print_format_help",
"fieldtype": "HTML",
"label": "Print Format Help",
"options": "<h3>Print Format Help</h3>\n<hr>\n<h4>Introduction</h4>\n<p>Print Formats are rendered on the server side using the Jinja Templating Language. All forms have access to the <code>doc</code> object which contains information about the document that is being formatted. You can also access common utilities via the <code>frappe</code> module.</p>\n<p>For styling, the Boostrap CSS framework is provided and you can enjoy the full range of classes.</p>\n<hr>\n<h4>References</h4>\n<ol>\n\t<li><a href=\"http://jinja.pocoo.org/docs/templates/\" target=\"_blank\">Jinja Templating Language</a></li>\n\t<li><a href=\"http://getbootstrap.com\" target=\"_blank\">Bootstrap CSS Framework</a></li>\n</ol>\n<hr>\n<h4>Example</h4>\n<pre><code>&lt;h3&gt;{{ doc.select_print_heading or \"Invoice\" }}&lt;/h3&gt;\n&lt;div class=\"row\"&gt;\n\t&lt;div class=\"col-md-3 text-right\"&gt;Customer Name&lt;/div&gt;\n\t&lt;div class=\"col-md-9\"&gt;{{ doc.customer_name }}&lt;/div&gt;\n&lt;/div&gt;\n&lt;div class=\"row\"&gt;\n\t&lt;div class=\"col-md-3 text-right\"&gt;Date&lt;/div&gt;\n\t&lt;div class=\"col-md-9\"&gt;{{ doc.get_formatted(\"invoice_date\") }}&lt;/div&gt;\n&lt;/div&gt;\n&lt;table class=\"table table-bordered\"&gt;\n\t&lt;tbody&gt;\n\t\t&lt;tr&gt;\n\t\t\t&lt;th&gt;Sr&lt;/th&gt;\n\t\t\t&lt;th&gt;Item Name&lt;/th&gt;\n\t\t\t&lt;th&gt;Description&lt;/th&gt;\n\t\t\t&lt;th class=\"text-right\"&gt;Qty&lt;/th&gt;\n\t\t\t&lt;th class=\"text-right\"&gt;Rate&lt;/th&gt;\n\t\t\t&lt;th class=\"text-right\"&gt;Amount&lt;/th&gt;\n\t\t&lt;/tr&gt;\n\t\t{%- for row in doc.items -%}\n\t\t&lt;tr&gt;\n\t\t\t&lt;td style=\"width: 3%;\"&gt;{{ row.idx }}&lt;/td&gt;\n\t\t\t&lt;td style=\"width: 20%;\"&gt;\n\t\t\t\t{{ row.item_name }}\n\t\t\t\t{% if row.item_code != row.item_name -%}\n\t\t\t\t&lt;br&gt;Item Code: {{ row.item_code}}\n\t\t\t\t{%- endif %}\n\t\t\t&lt;/td&gt;\n\t\t\t&lt;td style=\"width: 37%;\"&gt;\n\t\t\t\t&lt;div style=\"border: 0px;\"&gt;{{ row.description }}&lt;/div&gt;&lt;/td&gt;\n\t\t\t&lt;td style=\"width: 10%; text-align: right;\"&gt;{{ row.qty }} {{ row.uom or row.stock_uom }}&lt;/td&gt;\n\t\t\t&lt;td style=\"width: 15%; text-align: right;\"&gt;{{\n\t\t\t\trow.get_formatted(\"rate\", doc) }}&lt;/td&gt;\n\t\t\t&lt;td style=\"width: 15%; text-align: right;\"&gt;{{\n\t\t\t\trow.get_formatted(\"amount\", doc) }}&lt;/td&gt;\n\t\t&lt;/tr&gt;\n\t\t{%- endfor -%}\n\t&lt;/tbody&gt;\n&lt;/table&gt;</code></pre>\n<hr>\n<h4>Common Functions</h4>\n<table class=\"table table-bordered\">\n\t<tbody>\n\t\t<tr>\n\t\t\t<td style=\"width: 30%;\"><code>doc.get_formatted(\"[fieldname]\", [parent_doc])</code></td>\n\t\t\t<td>Get document value formatted as Date, Currency, etc. Pass parent <code>doc</code> for currency type fields.</td>\n\t\t</tr>\n\t\t<tr>\n\t\t\t<td style=\"width: 30%;\"><code>frappe.db.get_value(\"[doctype]\", \"[name]\", \"fieldname\")</code></td>\n\t\t\t<td>Get value from another document.</td>\n\t\t</tr>\n\t</tbody>\n</table>\n"
},
{
"fieldname": "format_data",
"fieldtype": "Code",
"hidden": 1,
"label": "Format Data"
},
{
"default": "0",
"fieldname": "print_format_builder",
"fieldname": "default",
"fieldtype": "Check",
"hidden": 1,
"label": "Print Format Builder"
},
{
"default": "0",
"fieldname": "print_format_builder_beta",
"fieldtype": "Check",
"label": "Print Format Builder Beta"
"label": "Default",
"no_copy": 1,
"read_only": 1
}
],
"icon": "fa fa-print",
"index_web_pages_for_search": 1,
"links": [],
"modified": "2024-08-29 01:52:40.846934",
"modified": "2024-08-31 11:55:05.658178",
"modified_by": "Administrator",
"module": "Report Print",
"name": "Report Print Format",
Expand All @@ -282,5 +94,7 @@
"sort_field": "modified",
"sort_order": "DESC",
"states": [],
"track_changes": 1
"track_changes": 1,
"track_seen": 1,
"track_views": 1
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
# Copyright (c) 2024, Code Venturers and contributors
# For license information, please see license.txt

# import frappe
import frappe
from frappe import _, bold
from frappe.utils.jinja import validate_template

from frappe.model.document import Document


class ReportPrintFormat(Document):
pass
def validate(self):
if self.html:
validate_template(self.html)

@frappe.whitelist()
def make_default(self):
if self.disabled:
frappe.throw(_("The document {} is disabled..".format(bold(self.name))))

if default_print := frappe.db.get_value(
"Report Print Format", {"report": self.report, "default": 1}, "name"
):
report_print_doc = frappe.get_doc("Report Print Format", default_print)
report_print_doc.db_set("default", 0)
self.db_set("default", 1)

frappe.msgprint(
_("{0} is now default print format for {1} report").format(
frappe.bold(self.name), frappe.bold(self.report)
)
)

0 comments on commit 5e414bd

Please # to comment.