separated macros

This commit is contained in:
Rushabh Mehta 2014-07-21 12:01:56 +05:30 committed by Anand Doshi
parent 70f84020d2
commit 68cab5070a
4 changed files with 127 additions and 120 deletions

View file

@ -610,6 +610,8 @@ def get_jenv():
jenv.globals.update({
"frappe": sys.modules[__name__],
"frappe.utils": frappe.utils,
"get_visible_columns": \
frappe.get_attr("frappe.templates.pages.print.get_visible_columns"),
"_": _
})
@ -675,3 +677,7 @@ def get_test_records(doctype):
return json.loads(f.read())
else:
return []
def format_value(value, df, doc=None):
import frappe.utils.formatters
return frappe.utils.formatters.format_value(value, df, doc)

View file

@ -8,7 +8,6 @@ from frappe import _
from frappe.modules import get_doc_path
from jinja2 import TemplateNotFound
from frappe.utils.formatters import format_value
from frappe.utils import cint
no_cache = 1
@ -50,7 +49,6 @@ def get_context(context):
@frappe.whitelist()
def get_html(doc, name=None, print_format=None, meta=None,
no_letterhead=False, trigger_print=False):
from jinja2 import Environment
if isinstance(doc, basestring) and isinstance(name, basestring):
doc = frappe.get_doc(doc, name)
@ -67,26 +65,28 @@ def get_html(doc, name=None, print_format=None, meta=None,
if not meta:
meta = frappe.get_meta(doc.doctype)
jenv = frappe.get_jenv()
if print_format in ("Standard", standard_format):
template = frappe.get_template("templates/print_formats/standard.html")
template = jenv.get_template("templates/print_formats/standard.html")
else:
template = Environment().from_string(get_print_format(doc.doctype,
template =jenv.from_string(get_print_format(doc.doctype,
print_format))
args = {
"doc": doc,
"meta": frappe.get_meta(doc.doctype),
"layout": make_layout(doc, meta),
"frappe": frappe,
"utils": frappe.utils,
"get_visible_columns": get_visible_columns,
"format": format_value,
"no_letterhead": no_letterhead,
"trigger_print": cint(trigger_print),
"letter_head": frappe.db.get_value("Letter Head",
doc.letter_head, "content") if doc.get("letter_head") else ""
}
html = template.render(args, filters={"len": len})
return html
def get_print_format(doctype, format_name):

View file

@ -1,123 +1,16 @@
{% set section_made = False %}
{% macro render_field(df) -%}
{%- if df.fieldtype=="Table" -%}
{{ render_table(df) }}
{%- elif df.fieldtype in ("Text", "Text Editor", "Code") -%}
{{ render_text_field(df) }}
{%- else -%}
{{ render_field_with_label(df) }}
{%- endif -%}
{%- endmacro -%}
{%- macro render_table(df) -%}
{%- set table_meta = frappe.get_meta(df.options) -%}
{%- set data = doc.get(df.fieldname)[df.start:df.end] -%}
{%- if doc.table_print_templates and
doc.table_print_templates.get(df.fieldname) -%}
{% include doc.table_print_templates[df.fieldname] %}
{%- else -%}
{%- if data -%}
{%- set visible_columns = get_visible_columns(doc.get(df.fieldname),
table_meta) -%}
<div>
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th style="width: 40px">Sr</th>
{% for tdf in visible_columns %}
<th style="width: {{ get_width(tdf.fieldtype) }}px">
{{ tdf.label }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for d in data %}
<tr>
<td>{{ d.idx }}</td>
{% for tdf in visible_columns %}
<td>{{ print_value(tdf, d, table_meta) }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
</div>
{%- endif -%}
{%- endif -%}
{%- endmacro -%}
{%- macro render_field_with_label(df) -%}
<div class="row">
<div class="col-sm-4 text-right">
{% if df.fieldtype not in ("Image","HTML") and
doc.get(df.fieldname) %}
<label>{{ df.label }}</label>
{% endif %}
</div>
<div class="col-sm-8">
{% if doc.get(df.fieldname) != None -%}
{{ print_value(df, doc, meta) }}{% endif %}
</div>
</div>
{%- endmacro -%}
{%- macro render_text_field(df) -%}
{%- if doc.get(df.fieldname) != None -%}
<div style="padding: 10px 0px">
{%- if df.fieldtype=="Code" %}<pre>{% endif %}
{{ doc.get(df.fieldname) }}
{% if df.fieldtype=="Code" %}</pre>{% endif -%}
</div>
{%- endif -%}
{%- endmacro -%}
{%- macro print_value(df, d, meta) -%}
{% if df.fieldtype=="Check" %}
<i class="{{ 'icon-check' if d[df.fieldname] else 'icon-check-empty' }}"></i>
{% elif df.fieldtype=="Image" %}
<img src="{{ doc[meta.get_field(df.fieldname).options] }}" class="img-responsive">
{% else %}
{{ format(d[df.fieldname], df, doc) if df.fieldname else "" }}
{% endif %}
{%- endmacro %}
{% macro get_width(fieldtype) -%}
{%- if fieldtype in ("Int", "Check", "Float", "Currency") -%}{{ 80 }}
{%- else -%}{{ 150 }}{% endif -%}
{%- endmacro %}
{%- macro add_header(page_num, max_pages) -%}
{% if letter_head and not no_letterhead|int %}
<div class="letter-head">{{ letter_head }}</div>
{% endif %}
<div class="print-heading">
<h2>{{ doc.print_heading if doc.print_heading != None
else doc.doctype }}</h2>
<h4 class="text-muted">{{ doc.sub_heading if doc.sub_heading != None else ("#" + doc[meta.title_field or "name"]) }}</h4>
</div>
{%- if meta.is_submittable and doc.docstatus==0-%}
<div class="alert alert-info text-center">
<h4 style="margin: 0px;">DRAFT</h4></div>
{%- endif -%}
{%- if meta.is_submittable and doc.docstatus==2-%}
<div class="alert alert-danger text-center">
<h4 style="margin: 0px;">CANCELLED</h4></div>
{%- endif -%}
{% if max_pages > 1 %}
<p class="text-right">{{ _("Page #{0} of {1}").format(page_num, max_pages) }}</p>
{% endif %}
{%- endmacro -%}
{%- set section_made = False -%}
{%- from "templates/print_formats/standard_macros.html" import add_header,
render_field -%}
{% for page in layout %}
<div class="page-break">
{{ add_header(loop.index, layout|len) }}
{{ add_header(loop.index, layout|len, doc) }}
{% for section in page %}
<div class="row">
{% for column in section %}
<div class="col-sm-{{ (12 / section|len)|int }}">
{% for df in column %}
{{ render_field(df) }}
{{ render_field(df, doc) }}
{% endfor %}
</div>
{% endfor %}

View file

@ -0,0 +1,108 @@
{% macro render_field(df, doc) -%}
{%- if df.fieldtype=="Table" -%}
{{ render_table(df, doc) }}
{%- elif df.fieldtype in ("Text", "Text Editor", "Code") -%}
{{ render_text_field(df, doc) }}
{%- else -%}
{{ render_field_with_label(df, doc) }}
{%- endif -%}
{%- endmacro -%}
{%- macro render_table(df, doc) -%}
{%- set table_meta = frappe.get_meta(df.options) -%}
{%- set data = doc.get(df.fieldname)[df.start:df.end] -%}
{%- if doc.table_print_templates and
doc.table_print_templates.get(df.fieldname) -%}
{% include doc.table_print_templates[df.fieldname] %}
{%- else -%}
{%- if data -%}
{%- set visible_columns = get_visible_columns(doc.get(df.fieldname),
table_meta) -%}
<div>
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th style="width: 40px">Sr</th>
{% for tdf in visible_columns %}
<th style="width: {{ get_width(tdf.fieldtype) }}px">
{{ tdf.label }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for d in data %}
<tr>
<td>{{ d.idx }}</td>
{% for tdf in visible_columns %}
<td>{{ print_value(tdf, d, doc) }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
</div>
{%- endif -%}
{%- endif -%}
{%- endmacro -%}
{%- macro render_field_with_label(df, doc) -%}
<div class="row">
<div class="col-sm-4 text-right">
{% if df.fieldtype not in ("Image","HTML") and
doc.get(df.fieldname) %}
<label>{{ df.label }}</label>
{% endif %}
</div>
<div class="col-sm-8">
{% if doc.get(df.fieldname) != None -%}
{{ print_value(df, doc) }}{% endif %}
</div>
</div>
{%- endmacro -%}
{%- macro render_text_field(df, doc) -%}
{%- if doc.get(df.fieldname) != None -%}
<div style="padding: 10px 0px">
{%- if df.fieldtype=="Code" %}<pre>{% endif %}
{{ doc.get(df.fieldname) }}
{% if df.fieldtype=="Code" %}</pre>{% endif -%}
</div>
{%- endif -%}
{%- endmacro -%}
{%- macro print_value(df, doc, parent_doc=None) -%}
{% if df.fieldtype=="Check" %}
<i class="{{ 'icon-check' if doc[df.fieldname] else 'icon-check-empty' }}"></i>
{% elif df.fieldtype=="Image" %}
<img src="{{ doc[doc.meta.get_field(df.fieldname).options] }}" class="img-responsive">
{% else %}
{{ frappe.format_value(doc[df.fieldname], df, parent_doc or doc) if df.fieldname else "" }}
{% endif %}
{%- endmacro %}
{% macro get_width(fieldtype) -%}
{%- if fieldtype in ("Int", "Check", "Float", "Currency") -%}{{ 80 }}
{%- else -%}{{ 150 }}{% endif -%}
{%- endmacro %}
{%- macro add_header(page_num, max_pages, doc) -%}
{% if letter_head and not no_letterhead|int %}
<div class="letter-head">{{ letter_head }}</div>
{% endif %}
<div class="print-heading">
<h2>{{ doc.print_heading if doc.print_heading != None
else doc.doctype }}</h2>
<h4 class="text-muted">{{ doc.sub_heading if doc.sub_heading != None else ("#" + doc[doc.meta.title_field or "name"]) }}</h4>
</div>
{%- if doc.meta.is_submittable and doc.docstatus==0-%}
<div class="alert alert-info text-center">
<h4 style="margin: 0px;">DRAFT</h4></div>
{%- endif -%}
{%- if doc.meta.is_submittable and doc.docstatus==2-%}
<div class="alert alert-danger text-center">
<h4 style="margin: 0px;">CANCELLED</h4></div>
{%- endif -%}
{% if max_pages > 1 %}
<p class="text-right">{{ _("Page #{0} of {1}").format(page_num, max_pages) }}</p>
{% endif %}
{%- endmacro -%}