From 94bd74319ba8f5641c1ff5dc5e192fe296bd0113 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 1 Oct 2024 16:06:12 +0200 Subject: [PATCH] fix(edi-import): support link & library files, too --- erpnext/edi/doctype/code_list/code_list.py | 5 +- .../edi/doctype/code_list/code_list_import.js | 3 +- .../edi/doctype/code_list/code_list_import.py | 56 +++++++++++++------ .../edi/doctype/common_code/common_code.py | 3 +- 4 files changed, 47 insertions(+), 20 deletions(-) diff --git a/erpnext/edi/doctype/code_list/code_list.py b/erpnext/edi/doctype/code_list/code_list.py index 98a93fc73e59..52e39d1f2ff8 100644 --- a/erpnext/edi/doctype/code_list/code_list.py +++ b/erpnext/edi/doctype/code_list/code_list.py @@ -60,9 +60,10 @@ def get_code_for(self, doctype: str, name: str): return code[0][0] if code else None def import_genericode( - self, file_path, code_column, title_column=None, description_column=None, filters=None + self, file_name, code_column, title_column=None, description_column=None, filters=None ): """Import genericode file and create Common Code entries""" + file_path = frappe.utils.file_manager.get_file_path(file_name) parser = etree.XMLParser(remove_blank_text=True) tree = etree.parse(file_path, parser=parser) root = tree.getroot() @@ -82,7 +83,7 @@ def import_genericode( self.save() common_codes = CommonCode.import_genericode( - file_path, self.name, code_column, title_column, description_column, filters + file_name, self.name, code_column, title_column, description_column, filters ) # Bulk insert common codes diff --git a/erpnext/edi/doctype/code_list/code_list_import.js b/erpnext/edi/doctype/code_list/code_list_import.js index ef14dc2cf77c..5e4c190494ee 100644 --- a/erpnext/edi/doctype/code_list/code_list_import.js +++ b/erpnext/edi/doctype/code_list/code_list_import.js @@ -11,6 +11,7 @@ erpnext.edi.import_genericode = function (listview_or_form) { doctype: doctype, docname: docname, allow_toggle_private: false, + allow_take_photo: false, on_success: function (_file_doc, r) { listview_or_form.refresh(); show_column_selection_dialog(r.message); @@ -109,7 +110,7 @@ function show_column_selection_dialog(context) { method: "erpnext.edi.doctype.code_list.code_list_import.process_genericode_import", args: { code_list_name: context.code_list, - file_path: context.file_path, + file: context.file, code_column: values.code_column, title_column: values.title_column, description_column: values.description_column, diff --git a/erpnext/edi/doctype/code_list/code_list_import.py b/erpnext/edi/doctype/code_list/code_list_import.py index 14760a84c8ed..9136f3bae3ac 100644 --- a/erpnext/edi/doctype/code_list/code_list_import.py +++ b/erpnext/edi/doctype/code_list/code_list_import.py @@ -1,20 +1,41 @@ import json import frappe +import requests from frappe import _ from frappe.utils.file_manager import save_file from lxml import etree +URL_PREFIXES = ("http://", "https://") + @frappe.whitelist() def import_genericode(): doctype = frappe.form_dict.doctype docname = frappe.form_dict.docname - - # Parse the uploaded file content + content = frappe.local.uploaded_file + + # recover the content, if it's a link + if (file_url := frappe.local.uploaded_file_url) and file_url.startswith(URL_PREFIXES): + try: + # If it's a URL, fetch the content and make it a local file (for durable audit) + response = requests.get(frappe.local.uploaded_file_url) + response.raise_for_status() + frappe.local.uploaded_file = content = response.content + frappe.local.uploaded_filename = frappe.local.uploaded_file_url.split("/")[-1] + frappe.local.uploaded_file_url = None + except Exception as e: + frappe.throw(f"
{e!s}
", title=_("Fetching Error")) + + if file_url := frappe.local.uploaded_file_url: + file_path = frappe.utils.file_manager.get_file_path(file_url) + with open(file_path.encode(), mode="rb") as f: + content = f.read() + + # Parse the xml content parser = etree.XMLParser(remove_blank_text=True) try: - root = etree.fromstring(frappe.local.uploaded_file, parser=parser) + root = etree.fromstring(content, parser=parser) except Exception as e: frappe.throw(f"
{e!s}
", title=_("Parsing Error")) @@ -31,23 +52,26 @@ def import_genericode(): code_list.name = name code_list.insert(ignore_permissions=True) - # Save the file using save_file utility - file_doc = save_file( - fname=frappe.local.uploaded_filename, - content=frappe.local.uploaded_file, - dt="Code List", - dn=code_list.name, - folder="Home/Attachments", - is_private=1, - ) - file_path = file_doc.get_full_path() + # Attach the file and provide a recoverable identifier + file_doc = frappe.get_doc( + { + "doctype": "File", + "attached_to_doctype": "Code List", + "attached_to_name": code_list.name, + "folder": "Home/Attachments", + "file_name": frappe.local.uploaded_filename, + "file_url": frappe.local.uploaded_file_url, + "is_private": 1, + "content": content, + } + ).save(ignore_permissions=True) # Get available columns and example values columns, example_values, filterable_columns = get_genericode_columns_and_examples(root) return { "code_list": code_list.name, - "file_path": file_path, + "file": file_doc.name, "columns": columns, "example_values": example_values, "filterable_columns": filterable_columns, @@ -56,11 +80,11 @@ def import_genericode(): @frappe.whitelist() def process_genericode_import( - code_list_name, file_path, code_column, title_column=None, description_column=None, filters=None + code_list_name, file, code_column, title_column=None, description_column=None, filters=None ): code_list = frappe.get_doc("Code List", code_list_name) return code_list.import_genericode( - file_path, code_column, title_column, description_column, filters and json.loads(filters) + file, code_column, title_column, description_column, filters and json.loads(filters) ) diff --git a/erpnext/edi/doctype/common_code/common_code.py b/erpnext/edi/doctype/common_code/common_code.py index 56f2c5535dd4..dfbf39a66aed 100644 --- a/erpnext/edi/doctype/common_code/common_code.py +++ b/erpnext/edi/doctype/common_code/common_code.py @@ -62,8 +62,9 @@ def validate_distinct_references(self): @staticmethod def import_genericode( - file_path, list_name, code_column, title_column=None, description_column=None, filters=None + file_name, list_name, code_column, title_column=None, description_column=None, filters=None ): + file_path = frappe.utils.file_manager.get_file_path(file_name) parser = etree.XMLParser(remove_blank_text=True) tree = etree.parse(file_path, parser=parser) root = tree.getroot()