-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feat-columns-in-dynamic-containers
- Loading branch information
Showing
14 changed files
with
582 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import os | ||
import shutil | ||
from pathlib import Path | ||
|
||
import frappe | ||
from frappe.modules.import_file import import_file_by_path | ||
from frappe.utils import get_files_path | ||
|
||
""" | ||
features: | ||
- Print Designer App can have default formats for all installed apps. | ||
- Any Custom/Standard App can have default formats for any installed apps | ||
( This will only install formats if print_designer is installed ). | ||
- This will be useful when we have standalone formats that can be used without print designer app. | ||
when print_designer app is installed | ||
- get hooks from all installed apps including pd and load default formats from defined folders. | ||
when any new app is installed | ||
- if exists in print_designer/default_templates, load default formats for newly installed app. | ||
- get hooks from new app and load default formats for all installed apps from app's format dir. | ||
""" | ||
|
||
# TODO: handle override of default formats from different apps or even Custom Formats with same name. | ||
|
||
# add default formats for all installed apps. | ||
def on_print_designer_install(): | ||
for app in frappe.get_installed_apps(): | ||
install_default_formats(app=app, load_pd_formats=False) | ||
|
||
|
||
def get_preview_image_folder_path(print_format): | ||
app = frappe.scrub(frappe.get_doctype_app(print_format.doc_type)) | ||
pd_folder = frappe.get_hooks( | ||
"pd_standard_format_folder", app_name=print_format.print_designer_template_app | ||
) | ||
if len(pd_folder) == 0: | ||
pd_folder = ["default_templates"] | ||
return os.path.join( | ||
frappe.get_app_path(print_format.print_designer_template_app), os.path.join(pd_folder[0], app) | ||
) | ||
|
||
|
||
def update_preview_img(file): | ||
print_format = frappe.get_doc(file.attached_to_doctype, file.attached_to_name) | ||
folder = get_preview_image_folder_path(print_format) | ||
file_name = print_format.print_designer_preview_img.split("/")[-1] | ||
org_path = os.path.join(folder, file_name) | ||
target_path = get_files_path(file_name, is_private=1) | ||
shutil.copy2(org_path, target_path) | ||
|
||
|
||
# called after install of any new app. | ||
def install_default_formats(app, filter_by="", load_pd_formats=True): | ||
if load_pd_formats: | ||
# load formats from print_designer app if some new app is installed and have default formats | ||
install_default_formats(app="print_designer", filter_by=app, load_pd_formats=False) | ||
|
||
# get dir path and load formats from installed app | ||
pd_folder = frappe.get_hooks("pd_standard_format_folder", app_name=app) | ||
if len(pd_folder) == 0: | ||
return | ||
|
||
print_formats = get_filtered_formats_by_app( | ||
app=app, templates_folder=pd_folder[0], filter_by=filter_by | ||
) | ||
|
||
# preview_files = [f for f in print_formats if f.name.endswith("-preview.json")] | ||
print_formats = [f for f in print_formats if not f.name.endswith("-preview.json")] | ||
|
||
for json_file_path in print_formats: | ||
import_file_by_path(path=json_file_path) | ||
frappe.db.commit() | ||
# TODO: enable this after this is released in v15 https://github.com/frappe/frappe/pull/25779 | ||
# for json_file_path in preview_files: | ||
# import_file_by_path(path=json_file_path, pre_process=update_preview_img) | ||
# frappe.db.commit() | ||
|
||
# for pf in frappe.db.get_all("Print Format", filters={"standard": "Yes", "print_designer": 1}): | ||
# updated_url = frappe.db.get_value( | ||
# "File", | ||
# { | ||
# "attached_to_doctype": "Print Format", | ||
# "attached_to_name": pf.name, | ||
# "attached_to_field": "print_designer_preview_img", | ||
# }, | ||
# "file_url", | ||
# ) | ||
# if updated_url: | ||
# frappe.set_value("Print Format", pf.name, "print_designer_preview_img", updated_url) | ||
|
||
|
||
def get_filtered_formats_by_app(app, templates_folder, filter_by=""): | ||
app_path = frappe.get_app_path(app) | ||
if filter_by == "": | ||
folders = Path(os.path.join(app_path, templates_folder)) | ||
return get_formats_from_folders(folders=folders) | ||
else: | ||
folder = Path(os.path.join(app_path, templates_folder, filter_by)) | ||
return get_json_files(folder) | ||
|
||
|
||
def get_formats_from_folders(folders): | ||
formats = set() | ||
if not folders.exists(): | ||
return formats | ||
for folder in folders.iterdir(): | ||
if folder.is_dir() and folder.name in frappe.get_installed_apps(): | ||
formats.update(get_json_files(folder)) | ||
return formats | ||
|
||
|
||
def get_json_files(folder): | ||
formats = set() | ||
for json_file in folder.glob("*.json"): | ||
formats.add(json_file) | ||
return formats |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from frappe.custom.doctype.custom_field.custom_field import create_custom_fields | ||
|
||
from print_designer.custom_fields import CUSTOM_FIELDS | ||
|
||
|
||
def custom_field_patch(): | ||
create_custom_fields(CUSTOM_FIELDS, ignore_validate=True) |
10 changes: 10 additions & 0 deletions
10
print_designer/print_designer/client_scripts/print_format.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import os | ||
import shutil | ||
|
||
import frappe | ||
from frappe.modules.utils import scrub | ||
from frappe.printing.doctype.print_format.print_format import PrintFormat | ||
|
||
|
||
class PDPrintFormat(PrintFormat): | ||
def export_doc(self): | ||
if ( | ||
not self.standard == "Yes" | ||
or not frappe.conf.developer_mode | ||
or frappe.flags.in_patch | ||
or frappe.flags.in_install | ||
or frappe.flags.in_migrate | ||
or frappe.flags.in_import | ||
or frappe.flags.in_setup_wizard | ||
): | ||
return | ||
|
||
if not self.print_designer: | ||
return super().export_doc() | ||
|
||
self.write_document_file() | ||
|
||
def write_document_file(self): | ||
doc = self | ||
doc_export = doc.as_dict(no_nulls=True) | ||
doc.run_method("before_export", doc_export) | ||
|
||
# create folder | ||
folder = self.create_folder(doc.doc_type, doc.name) | ||
|
||
fname = scrub(doc.name) | ||
|
||
# write the data file | ||
path = os.path.join(folder, f"{fname}.json") | ||
with open(path, "w+") as json_file: | ||
json_file.write(frappe.as_json(doc_export)) | ||
print(f"Wrote document file for {doc.doctype} {doc.name} at {path}") | ||
self.export_preview(folder=folder, fname=fname) | ||
|
||
def create_folder(self, dt, dn): | ||
app = scrub(frappe.get_doctype_app(dt)) | ||
dn = scrub(dn) | ||
pd_folder = frappe.get_hooks( | ||
"pd_standard_format_folder", app_name=self.print_designer_template_app | ||
) | ||
if len(pd_folder) == 0: | ||
pd_folder = ["default_templates"] | ||
folder = os.path.join( | ||
frappe.get_app_path(self.print_designer_template_app), os.path.join(pd_folder[0], app) | ||
) | ||
frappe.create_folder(folder) | ||
return folder | ||
|
||
def export_preview(self, folder, fname): | ||
if self.print_designer_preview_img: | ||
try: | ||
file = frappe.get_doc( | ||
"File", | ||
{ | ||
"file_url": self.print_designer_preview_img, | ||
"attached_to_doctype": self.doctype, | ||
"attached_to_name": self.name, | ||
"attached_to_field": "print_designer_preview_img", | ||
}, | ||
) | ||
except frappe.DoesNotExistError: | ||
file = None | ||
if not file: | ||
return | ||
file_export = file.as_dict(no_nulls=True) | ||
file.run_method("before_export", file_export) | ||
org_path = file.get_full_path() | ||
target_path = os.path.join(folder, org_path.split("/")[-1]) | ||
shutil.copy2(org_path, target_path) | ||
print(f"Wrote preview file for {self.doctype} {self.name} at {target_path}") | ||
# write the data file | ||
path = os.path.join(folder, f"print_designer-{fname}-preview.json") | ||
with open(path, "w+") as json_file: | ||
json_file.write(frappe.as_json(file_export)) | ||
print(f"Wrote document file for {file.doctype} {file.name} at {path}") |
18 changes: 15 additions & 3 deletions
18
print_designer/print_designer/page/print_designer/jinja/macros/image.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,26 @@ | ||
{% macro image(element) -%} | ||
{%- if element.image.file_url -%} | ||
{%- set value = element.image.file_url -%} | ||
{%- elif element.image.fieldname -%} | ||
{%- if element.image.parent == doc.doctype -%} | ||
{%- set value = doc.get(element.image.fieldname) -%} | ||
{%- else -%} | ||
{%- set value = frappe.db.get_value(element.image.doctype, doc[element.image.parentField], element.image.fieldname) -%} | ||
{%- endif -%} | ||
{%- else -%} | ||
{%- set value = "" -%} | ||
{%- endif -%} | ||
|
||
{%- if value -%} | ||
<div | ||
style="position: absolute; top:{{ element.startY }}px; left:{{ element.startX }}px;width:{{ element.width }}px;height:{{ element.height }}px; | ||
{{convert_css(element.style)}}" | ||
class="image {{ element.classes | join(' ') }}" | ||
> | ||
<div | ||
style="width:100%;height:100%; | ||
background-image: url('{{frappe.get_url()}}{%if element.isDynamic %}{{element.image.value}}{% elif not element.isDynamic%}{{element.image.file_url}}{% endif %}'); | ||
" | ||
style="width:100%; height:100%; background-image: url('{{frappe.get_url()}}{{value}}');" | ||
class="image {{ element.classes | join(' ') }}" | ||
></div> | ||
</div> | ||
{%- endif -%} | ||
{%- endmacro %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.