[fixes] font-awesome in print format, custom css, empty sections removed from print formats

This commit is contained in:
Rushabh Mehta 2015-09-09 14:48:46 +05:30
parent 7e6a2ca256
commit 188ff8f2e9
6 changed files with 126 additions and 33 deletions

View file

@ -281,6 +281,72 @@
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"bold": 0,
"collapsible": 0,
"fieldname": "css_section",
"fieldtype": "Section Break",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"bold": 0,
"collapsible": 0,
"fieldname": "css",
"fieldtype": "Code",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"label": "Custom CSS",
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"bold": 0,
"collapsible": 0,
"fieldname": "custom_html_help",
"fieldtype": "HTML",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"label": "Custom HTML Help",
"no_copy": 0,
"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-left: 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",
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"bold": 0,
@ -381,7 +447,7 @@
"issingle": 0,
"istable": 0,
"max_attachments": 0,
"modified": "2015-07-15 08:01:06.284031",
"modified": "2015-09-09 05:46:11.025962",
"modified_by": "Administrator",
"module": "Print",
"name": "Print Format",

View file

@ -12,7 +12,7 @@ class TestPrintFormat(unittest.TestCase):
def test_print_user(self, style=None):
print_html = frappe.get_print("User", "Administrator", style=style)
self.assertTrue("<label>First Name</label>" in print_html)
self.assertTrue(re.findall('<div class="col-xs-7[\s]*">[\s]*Administrator[\s]*</div>', print_html))
self.assertTrue(re.findall('<div class="col-xs-7[^"]*">[\s]*Administrator[\s]*</div>', print_html))
return print_html
def test_print_user_standard(self):

View file

@ -5,7 +5,10 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ title }}</title>
<meta name="generator" content="frappe">
<link type="text/css" rel="stylesheet" href="/assets/frappe/css/bootstrap.css">
<link type="text/css" rel="stylesheet"
href="/assets/frappe/css/bootstrap.css">
<link type="text/css" rel="stylesheet"
href="/assets/frappe/css/font-awesome.css">
<style>
{{ css }}
</style>

View file

@ -18,9 +18,6 @@ base_template_path = "templates/pages/print.html"
standard_format = "templates/print_formats/standard.html"
def get_context(context):
if not frappe.form_dict.format:
frappe.form_dict.format = standard_format
if not frappe.form_dict.doctype or not frappe.form_dict.name:
return {
"body": """<h1>Error</h1>
@ -31,14 +28,30 @@ def get_context(context):
doc = frappe.get_doc(frappe.form_dict.doctype, frappe.form_dict.name)
meta = frappe.get_meta(doc.doctype)
print_format = get_print_format_doc()
return {
"body": get_html(doc, print_format = frappe.form_dict.format,
meta=meta, trigger_print = frappe.form_dict.trigger_print, no_letterhead=frappe.form_dict.no_letterhead),
"css": get_print_style(frappe.form_dict.style, frappe.form_dict.format),
"body": get_html(doc, print_format = print_format,
meta=meta, trigger_print = frappe.form_dict.trigger_print,
no_letterhead=frappe.form_dict.no_letterhead),
"css": get_print_style(frappe.form_dict.style, print_format),
"comment": frappe.session.user,
"title": doc.get(meta.title_field) if meta.title_field else doc.name
}
def get_print_format_doc(print_format_name=None):
"""Returns print format document"""
if not print_format_name:
print_format_name = frappe.form_dict.format or "Standard"
if not print_format_name:
print_format_name = "Standard"
if print_format_name == "Standard":
return None
else:
return frappe.get_doc("Print Format", print_format_name)
def get_html(doc, name=None, print_format=None, meta=None,
no_letterhead=None, trigger_print=False):
@ -71,11 +84,7 @@ def get_html(doc, name=None, print_format=None, meta=None,
format_data, format_data_map = [], {}
# determine template
if print_format in ("Standard", standard_format):
template = "standard"
else:
print_format = frappe.get_doc("Print Format", print_format)
if print_format:
if print_format.standard=="Yes" or print_format.custom_format:
template = jenv.from_string(get_print_format(doc.doctype,
print_format))
@ -97,8 +106,12 @@ def get_html(doc, name=None, print_format=None, meta=None,
# fallback
template = "standard"
else:
template = "standard"
if template == "standard":
template = jenv.get_template("templates/print_formats/standard.html")
template = jenv.get_template(standard_format)
args = {
"doc": doc,
@ -119,6 +132,7 @@ def get_html(doc, name=None, print_format=None, meta=None,
@frappe.whitelist()
def get_html_and_style(doc, name=None, print_format=None, meta=None,
no_letterhead=None, trigger_print=False):
print_format = get_print_format_doc(print_format)
return {
"html": get_html(doc, name=name, print_format=print_format, meta=meta,
no_letterhead=no_letterhead, trigger_print=trigger_print),
@ -200,6 +214,10 @@ def make_layout(doc, meta, format_data=None):
df.print_hide = 0
if df.fieldtype=="Section Break" or page==[]:
if len(page) > 1 and not any(page[-1]):
# truncate prev section if empty
del page[-1]
page.append([])
if df.fieldtype=="Column Break" or (page[-1]==[] and df.fieldtype!="Section Break"):
@ -232,9 +250,6 @@ def make_layout(doc, meta, format_data=None):
df.end = None
page[-1][-1].append(df)
# filter empty sections
layout = [filter(lambda s: any(filter(lambda c: any(c), s)), pg) for pg in layout]
return layout
def is_visible(df, doc):
@ -256,6 +271,9 @@ def has_value(df, doc):
elif isinstance(value, basestring) and not strip_html(value).strip():
return False
elif isinstance(value, list) and not len(value):
return False
return True
def get_print_style(style=None, print_format=None, for_legacy=False):
@ -284,6 +302,9 @@ def get_print_style(style=None, print_format=None, for_legacy=False):
# prepend css with at_import
css = at_import + css
if print_format and print_format.css:
css += "\n\n" + print_format.css
return css
def get_font(print_settings, print_format=None, for_legacy=False):
@ -292,8 +313,7 @@ def get_font(print_settings, print_format=None, for_legacy=False):
return default
font = None
if print_format and print_format not in ("Standard", standard_format):
print_format = frappe.get_doc("Print Format", print_format)
if print_format:
if print_format.font and print_format.font!="Default":
font = '{0}, sans-serif'.format(print_format.font)

View file

@ -5,9 +5,9 @@
<div class="page-break">
{{ add_header(loop.index, layout|len, doc, letter_head, no_letterhead) }}
{% for section in page %}
<div class="row">
<div class="row section-break">
{% for column in section %}
<div class="col-xs-{{ (12 / section|len)|int }}">
<div class="col-xs-{{ (12 / section|len)|int }} column-break">
{% for df in column %}
{{ render_field(df, doc) }}
{% endfor %}

View file

@ -20,13 +20,13 @@
{%- if data -%}
{%- set visible_columns = get_visible_columns(doc.get(df.fieldname),
table_meta, df) -%}
<div>
<div {{ fieldmeta(df) }}>
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th style="width: 40px">Sr</th>
<th style="width: 40px" class="table-sr">Sr</th>
{% for tdf in visible_columns %}
<th style="width: {{ get_width(tdf) }}px;" class="{{ get_align_class(tdf.fieldtype) }}">
<th style="width: {{ get_width(tdf) }}px;" class="{{ get_align_class(tdf.fieldtype) }}" {{ fieldmeta(df) }}>
{{ _(tdf.label) }}</th>
{% endfor %}
</tr>
@ -34,10 +34,10 @@
<tbody>
{% for d in data %}
<tr>
<td>{{ d.idx }}</td>
<td class="table-sr">{{ d.idx }}</td>
{% for tdf in visible_columns %}
<td class="{{ get_align_class(tdf.fieldtype) }}">
<div>{{ print_value(tdf, d, doc) }}</div></td>
<td class="{{ get_align_class(tdf.fieldtype) }}" {{ fieldmeta(df) }}>
<div class="value">{{ print_value(tdf, d, doc) }}</div></td>
{% endfor %}
</tr>
{% endfor %}
@ -48,15 +48,19 @@
{%- endif -%}
{%- endmacro -%}
{% macro fieldmeta(df) -%}
data-fieldname="{{ df.fieldname }}" data-fieldtype="{{ df.fieldtype }}"
{%- endmacro %}
{%- macro render_field_with_label(df, doc) -%}
<div class="row">
<div class="row" {{ fieldmeta(df) }}>
<div class="col-xs-5 text-right">
{% if df.fieldtype not in ("Image","HTML") and
doc.get(df.fieldname) != None %}
<label>{{ _(df.label) }}</label>
{% endif %}
</div>
<div class="col-xs-7 {{ get_align_class(df.fieldtype) }}">
<div class="col-xs-7 {{ get_align_class(df.fieldtype) }} value">
{% if doc.get(df.fieldname) != None -%}
{{ print_value(df, doc) }}{% endif %}
</div>
@ -65,10 +69,10 @@
{%- macro render_text_field(df, doc) -%}
{%- if doc.get(df.fieldname) != None -%}
<div style="padding: 10px 0px">
<div style="padding: 10px 0px" {{ fieldmeta(df) }}>
{%- if df.fieldtype in ("Text", "Code") %}<label>{{ _(df.label) }}</label>{%- endif %}
{%- if df.fieldtype=="Code" %}
<pre>{{ doc.get(df.fieldname) }}</pre>
<pre class="value">{{ doc.get(df.fieldname) }}</pre>
{% else -%}
{{ doc.get_formatted(df.fieldname, parent_doc or doc) }}
{% endif -%}
@ -98,7 +102,7 @@
{%- endmacro %}
{% macro get_align_class(fieldtype) %}
{%- if fieldtype in ("Int", "Check", "Float", "Currency") -%}{{ "text-right" }}
{%- if fieldtype in ("Int", "Float", "Currency") -%}{{ "text-right" }}
{%- else -%}{{ "" }}
{%- endif -%}
{% endmacro %}