Skip to content

Commit

Permalink
chore: Merge develop to main (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
maharshivpatel authored Jun 11, 2024
2 parents 29a0707 + 4c263e0 commit fa05aaf
Show file tree
Hide file tree
Showing 43 changed files with 2,336 additions and 1,319 deletions.
10 changes: 8 additions & 2 deletions print_designer/patches.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[pre_model_sync]
print_designer.patches.introduce_dynamic_containers
execute:from print_designer.patches.create_custom_fields import custom_field_patch; custom_field_patch() #208

[post_model_sync]
print_designer.patches.update_white_space_property
print_designer.patches.introduce_barcode
print_designer.patches.introduce_jinja
Expand All @@ -6,7 +11,8 @@ print_designer.patches.rerun_introduce_jinja
print_designer.patches.introduce_table_alt_row_styles
print_designer.patches.introduce_column_style
print_designer.patches.introduce_suffix_dynamic_content
print_designer.patches.introduce_dynamic_containers
print_designer.patches.introduce_dynamic_height
print_designer.patches.remove_unused_rectangle_gs_properties
execute:from print_designer.patches.create_custom_fields import custom_field_patch; custom_field_patch()
print_designer.patches.change_dynamic_height_variable
print_designer.patches.introduce_z_index
print_designer.patches.move_header_footers_to_new_schema
24 changes: 24 additions & 0 deletions print_designer/patches/change_dynamic_height_variable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import frappe

from print_designer.patches.patch_utils import patch_formats


def execute():
"""changing isDynamicHeight property to heightType property in text and table elements"""

def element_callback(el):
if el.get("type") == "text" and not (el.get("isDynamic") or el.get("parseJinja")):
return

if not "isDynamicHeight" in el:
el["isDynamicHeight"] = False

if el["isDynamicHeight"]:
el["heightType"] = "auto"
else:
el["heightType"] = "fixed"

patch_formats(
{"element": element_callback},
types=["text", "table"],
)
14 changes: 14 additions & 0 deletions print_designer/patches/introduce_z_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import frappe

from print_designer.patches.patch_utils import patch_formats


def execute():
"""Updating all style objects to have zIndex 0 in print formats that uses print designer"""

def style(style):
style["zIndex"] = 0

patch_formats(
{"style": style},
)
96 changes: 96 additions & 0 deletions print_designer/patches/move_header_footers_to_new_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import frappe

from print_designer.pdf import is_older_schema


def patch_format():
print_formats = frappe.get_all(
"Print Format",
filters={"print_designer": 1},
fields=[
"name",
"print_designer_header",
"print_designer_body",
"print_designer_after_table",
"print_designer_footer",
"print_designer_print_format",
"print_designer_settings",
],
)
for pf in print_formats:
settings = frappe.json.loads(pf.print_designer_settings or "{}")

header_childrens = frappe.json.loads(pf.print_designer_header or "[]")
header_data = [
{
"type": "page",
"childrens": header_childrens,
"firstPage": True,
"oddPage": True,
"evenPage": True,
"lastPage": True,
}
]

footer_childrens = frappe.json.loads(pf.print_designer_footer or "[]")
footer_data = [
{
"type": "page",
"childrens": footer_childrens,
"firstPage": True,
"oddPage": True,
"evenPage": True,
"lastPage": True,
}
]
for child in footer_childrens:
child["startY"] -= (
settings["page"].get("height", 0)
- settings["page"].get("marginTop", 0)
- settings["page"].get("footerHeight", 0)
)

childrens = frappe.json.loads(pf.print_designer_body or "[]")
bodyPage = [
{
"index": 0,
"type": "page",
"childrens": childrens,
"isDropZone": True,
}
]
object_to_save = {
"print_designer_header": frappe.json.dumps(header_data),
"print_designer_body": frappe.json.dumps(bodyPage),
"print_designer_footer": frappe.json.dumps(footer_data),
"print_designer_settings": frappe.json.dumps(settings),
}
if not is_older_schema(settings=settings, current_version="1.1.0"):
pf_print_format = frappe.json.loads(pf.print_designer_print_format)
if "header" in pf_print_format:
pf_print_format["header"] = {
"firstPage": pf_print_format["header"],
"oddPage": pf_print_format["header"],
"evenPage": pf_print_format["header"],
"lastPage": pf_print_format["header"],
}
if "footer" in pf_print_format:
pf_print_format["footer"] = {
"firstPage": pf_print_format["footer"],
"oddPage": pf_print_format["footer"],
"evenPage": pf_print_format["footer"],
"lastPage": pf_print_format["footer"],
}
object_to_save["print_designer_print_format"] = frappe.json.dumps(pf_print_format)

frappe.set_value(
"Print Format",
pf.name,
object_to_save,
)
return print_formats


def execute():
"""Updating Table and Dynamic Text Elements to have property isDynamicHeight with default value as True"""
patch_format()
27 changes: 24 additions & 3 deletions print_designer/patches/patch_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import frappe

from print_designer.pdf import is_older_schema

"""
Example Callback Functions used to Demonstrate Data Structure.
Example functions are just printing the values of the object passed to them.
Expand Down Expand Up @@ -71,6 +73,7 @@ def dynamic_content_style(style):
def patch_formats(
callbacks: Union[Callable[[Dict], None], Dict[str, Callable[[Dict], None]]],
types: Optional[List[str]] = None,
update_print_json: bool = False,
save: bool = True,
) -> None:
"""
Expand All @@ -81,7 +84,7 @@ def patch_formats(
`element, dynamic_content, style, dynamic_content_style` each with a function as its value.
Each callback function should take a dictionary as an argument and can modify it and return nothing.
The dictionary passed to the callback function represents a element or style object.
:param update_print_json: If True, the function will update the generated print format json that is used by jinja template.
:param types: A list of print format types to which the callback function should be applied.
If not provided, it defaults to ["text", "image", "barcode", "rectangle", "table"].
"""
Expand All @@ -98,11 +101,29 @@ def patch_formats(
"print_designer_body",
"print_designer_after_table",
"print_designer_footer",
"print_designer_print_format",
"print_designer_settings",
],
as_list=1,
)
for pf in print_formats:
updated_doc = frappe.get_doc("Print Format", pf[0]).update(
print_json = pf[5] or "{}"
# print_designer_print_format was introduced in schema version 1.1.0 so running this on older version is not required
pf_doc = frappe.get_doc("Print Format", pf[0])
if update_print_json and not is_older_schema(
settings=frappe.json.loads(pf[6] or "{}"), current_version="1.1.0"
):
print_json = frappe.json.loads(print_json)
if print_json.get("header", None):
print_json["header"] = patch_elements(print_json["header"], callbacks, types)
if print_json.get("body", None):
print_json["body"] = patch_elements(print_json["body"], callbacks, types)
if print_json.get("footer", None):
print_json["footer"] = patch_elements(print_json["footer"], callbacks, types)
print_json = frappe.json.dumps(print_json)
pf_doc.update({"print_designer_print_format": print_json})

pf_doc.update(
{
"print_designer_header": frappe.json.dumps(
patch_elements(frappe.json.loads(pf[1] or "[]"), callbacks, types)
Expand All @@ -119,7 +140,7 @@ def patch_formats(
}
)
if save:
updated_doc.save(ignore_permissions=True, ignore_version=False)
pf_doc.save(ignore_permissions=True, ignore_version=False)


def patch_elements(
Expand Down
6 changes: 3 additions & 3 deletions print_designer/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from frappe.monitor import add_data_to_monitor
from frappe.utils.error import log_error
from frappe.utils.jinja_globals import is_rtl
from frappe.utils.pdf import pdf_body_html as fw_pdf_body_html


def pdf_header_footer_html(soup, head, content, styles, html_id, css):
Expand Down Expand Up @@ -82,12 +83,11 @@ def pdf_body_html(print_format, jenv, args, template):
return f"<h1><b>Something went wrong while rendering the print format.</b> <hr/> If you don't know what just happened, and wish to file a ticket or issue on Github <hr /> Please copy the error from <code>Error Log {error.name}</code> or ask Administrator.<hr /><h3>Error rendering print format: {error.reference_name}</h3><h4>{error.method}</h4><pre>{html.escape(error.error)}</pre>"
else:
return f"<h1><b>Something went wrong while rendering the print format.</b> <hr/> If you don't know what just happened, and wish to file a ticket or issue on Github <hr /> Please copy the error from <code>Error Log {error.name}</code> or ask Administrator.</h1>"

return template.render(args, filters={"len": len})
return fw_pdf_body_html(template, args)


def is_older_schema(settings, current_version):
format_version = settings.get("schema_version")
format_version = settings.get("schema_version", "1.0.0")
format_version = format_version.split(".")
current_version = current_version.split(".")
if int(format_version[0]) < int(current_version[0]):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,70 @@
y[j].textContent = vars[x[i]];
}
}
}

const headers = {
firstPage : document.getElementById("firstPageHeader"),
oddPage : document.getElementById("oddPageHeader"),
evenPage : document.getElementById("evenPageHeader"),
lastPage : document.getElementById("lastPageHeader"),
}
const footers = {
firstPage : document.getElementById("firstPageFooter"),
oddPage : document.getElementById("oddPageFooter"),
evenPage : document.getElementById("evenPageFooter"),
lastPage : document.getElementById("lastPageFooter"),
}
function displayHeaderFooter(elements, selectedElements) {
for (var key in elements) {
if (elements[key]) {
if (elements[key] === selectedElements) {
elements[key].style.display = "block";
} else {
elements[key].style.display = "none";
}
}
}
}
var page_no = parseInt(vars["page"]);
var total_page_no = parseInt(vars["topage"]);
if (page_no == 1) {
if (headers.firstPage) {
displayHeaderFooter(headers, headers.firstPage);
} else {
displayHeaderFooter(headers, headers.oddPage);
}
if (footers.firstPage) {
displayHeaderFooter(footers, footers.firstPage);
} else {
displayHeaderFooter(footers, footers.oddPage);
}
} else if (page_no == total_page_no) {
if (headers.lastPage) {
displayHeaderFooter(headers, headers.lastPage);
} else {
if (total_page_no % 2 == 0) {
displayHeaderFooter(headers, headers.evenPage);
} else {
displayHeaderFooter(headers, headers.oddPage);
}
}
if (footers.lastPage) {
displayHeaderFooter(footers, footers.lastPage);
} else {
if (total_page_no % 2 == 0) {
displayHeaderFooter(footers, footers.evenPage);
} else {
displayHeaderFooter(footers, footers.oddPage);
}
}
} else if (page_no % 2 == 0) {
displayHeaderFooter(headers, headers.evenPage);
displayHeaderFooter(footers, footers.evenPage);
} else {
displayHeaderFooter(headers, headers.oddPage);
displayHeaderFooter(footers, footers.oddPage);
}
}
</script>

{% for tag in styles -%}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{% from 'print_designer/page/print_designer/jinja/macros/spantag.html' import span_tag with context %}

{% macro dynamictext(element, send_to_jinja, is_parent_dynamic_height) -%}
<div style="position:{%- if is_parent_dynamic_height -%}relative{% else %}absolute{%- endif -%}; {%- if not is_parent_dynamic_height -%}top: {%- else -%}margin-top: {%- endif -%}{{ element.startY }}px; left:{{ element.startX }}px;{% if element.isFixedSize %}width:{{ element.width }}px; {%- if not is_parent_dynamic_height -%}height:{{ element.height }}px; {%- endif -%} {% else %} width:fit-content; height:fit-content; white-space:nowrap; max-width: {{ (settings.page.width - settings.page.marginLeft - settings.page.marginRight - element.startX) + 2 }}px;{%endif%}" class="
{% macro dynamictext(element, send_to_jinja, heightType) -%}
<div style="position:{%- if heightType != 'fixed' -%}relative{% else %}absolute{%- endif -%}; {%- if heightType == 'fixed' -%}top: {%- else -%}margin-top: {%- endif -%}{{ element.startY }}px; left:{{ element.startX }}px;{% if element.isFixedSize %}width:{{ element.width }}px; {%- if heightType == 'fixed' -%}height:{{ element.height }}px; {%- endif -%} {% else %} width:fit-content; height:fit-content; white-space:nowrap; max-width: {{ (settings.page.width - settings.page.marginLeft - settings.page.marginRight - element.startX) + 2 }}px;{%endif%}" class="
{{ element.classes | join(' ') }}">
<div style="{% if element.isFixedSize %}width:{{ element.width }}px; {%- if not is_parent_dynamic_height -%}height:{{ element.height }}px; {%- endif -%}{% else %}width:fit-content; height:fit-content; white-space:nowrap; max-width: {{ (settings.page.width - settings.page.marginLeft - settings.page.marginRight - element.startX) + 2 }}px;{%endif%} {{convert_css(element.style)}}"
<div style="{% if element.isFixedSize %}width:{{ element.width }}px; {%- if heightType == 'fixed' -%}height:{{ element.height }}px; {%- endif -%}{% else %}width:fit-content; height:fit-content; white-space:nowrap; max-width: {{ (settings.page.width - settings.page.marginLeft - settings.page.marginRight - element.startX) + 2 }}px;{%endif%} {{convert_css(element.style)}}"
class="dynamicText {{ element.classes | join(' ') }}">
{% for field in element.dynamicContent %}
<!-- third Arg is row which is not sent outside table -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
{% macro rectangle(element, render_element, send_to_jinja) -%}
<div id="{{ element.id }}" style="position:absolute; top:{{ element.startY }}px; left:{{ element.startX }}px; width:{{ element.width }}px; height:{{ element.height }}px; {{convert_css(element.style)}}"
{% macro rectangle(element, render_element, send_to_jinja, heightType) -%}
{%- set heightType = element.get("heightType") -%}
{%- if settings.get("schema_version") == "1.1.0" or heightType == None -%}
{%- set heightType = "auto" if element.get("isDynamicHeight", False) else "fixed" -%}
{%- endif -%}
<div id="{{ element.id }}" style="position:{%- if heightType and heightType != 'fixed' -%}relative{% else %}absolute{%- endif -%}; {%- if heightType == 'fixed' -%}top: {%- else -%}margin-top: {%- endif -%}{{ element.startY }}px; left:{{ element.startX }}px; width:{{ element.width }}px; {%- if heightType != 'auto' -%} {%- if heightType == 'auto-min-height' -%}min-{%- endif -%}height:{{ element.height }}px; {%- endif -%} {{convert_css(element.style)}}"
class="rectangle {{ element.classes | join(' ') }}">
{% if element.childrens %}
{% for object in element.childrens %}
{{ render_element(object, send_to_jinja) }}
{{ render_element(object, send_to_jinja, heightType) }}
{% endfor %}
{% endif %}
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
{% from 'print_designer/page/print_designer/jinja/macros/render_element.html' import render_element with context %}

{% macro relative_columns(element, send_to_jinja) -%}
<div style="position:relative; top: 0px; width:{{ element.width }}px; {%- if not element.isDynamicHeight -%} height:{{ element.height }}px; {%- endif -%} {{convert_css(element.style)}}"
{%- set heightType = element.get("heightType") -%}
{%- if settings.get("schema_version") == "1.1.0" -%}
{%- set heightType = "auto" if element.get("isDynamicHeight", False) else "fixed" -%}
{%- endif -%}
<div style="position:relative; top: 0px; {%- if element.rectangleContainer -%}margin-top:{{element.startY}}px; margin-left:{{element.startX}}px;{%- endif -%} width:{{ element.width }}px; {%- if heightType != 'auto' -%}{%- if heightType == 'auto-min-height' -%}min-{%- endif -%}height:{{ element.height }}px; {%- endif -%} {{convert_css(element.style)}}"
class="rectangle {{ element.classes | join(' ') }}">
{% if element.childrens %}
{% for object in element.childrens %}
{{ render_element(object, send_to_jinja, element.get("isDynamicHeight", false)) }}
{%- if object.layoutType == "row" -%}
{{ relative_containers(object, send_to_jinja) }}
{%- elif object.layoutType == "column" -%}
{{ relative_columns(object, send_to_jinja) }}
{%- else -%}
{{ render_element(object, send_to_jinja, heightType) }}
{%- endif -%}
{% endfor %}
{% endif %}
</div>
{%- endmacro %}

{% macro relative_containers(element, send_to_jinja) -%}
<div style="position:relative; left:{{ element.startX }}px; {%- if not element.isDynamicHeight -%} height:{{ element.height }}px; {%- endif -%} {{convert_css(element.style)}}"
{%- set heightType = element.get("heightType") -%}
{%- if settings.get("schema_version") == "1.1.0" -%}
{%- set heightType = "auto" if element.get("isDynamicHeight", False) else "fixed" -%}
{%- endif -%}
<div style="position:relative; left:{{ element.startX }}px; {%- if heightType != 'auto' -%}{%- if heightType == 'auto-min-height' -%}min-{%- endif -%}height:{{ element.height }}px; {%- endif -%} {{convert_css(element.style)}}"
class="rectangle relative-row {{ element.classes | join(' ') }}">
{% if element.childrens %}
{% for object in element.childrens %}
{{ relative_columns(object, send_to_jinja) }}
{%- if object.layoutType == "column" -%}
{{ relative_columns(object, send_to_jinja) }}
{%- elif object.layoutType == "row" -%}
{{ relative_containers(object, send_to_jinja) }}
{%- else -%}
{{ render_element(object, send_to_jinja, heightType) }}
{%- endif -%}
{% endfor %}
{% endif %}
</div>
Expand Down
Loading

0 comments on commit fa05aaf

Please sign in to comment.