From 73c1bf972efa27229aae70c37ae5cf44c22444f1 Mon Sep 17 00:00:00 2001 From: Rohit Waghchaure Date: Thu, 30 Jan 2025 11:59:40 +0530 Subject: [PATCH] fix: reposting issue with s3 backup (cherry picked from commit 6b454ca9a7fa3fd50f9d2d5a2e9a9c321c0a18e8) --- erpnext/stock/stock_ledger.py | 38 +++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/erpnext/stock/stock_ledger.py b/erpnext/stock/stock_ledger.py index 91d10b3a9ac..3dea6010867 100644 --- a/erpnext/stock/stock_ledger.py +++ b/erpnext/stock/stock_ledger.py @@ -405,23 +405,13 @@ def create_json_gz_file(data, doc, file_name=None) -> str: compressed_content = gzip.compress(encoded_content) if not file_name: - json_filename = f"{scrub(doc.doctype)}-{scrub(doc.name)}.json.gz" - _file = frappe.get_doc( - { - "doctype": "File", - "file_name": json_filename, - "attached_to_doctype": doc.doctype, - "attached_to_name": doc.name, - "attached_to_field": "reposting_data_file", - "content": compressed_content, - "is_private": 1, - } - ) - _file.save(ignore_permissions=True) - - return _file.file_url + return create_file(doc, compressed_content) else: file_doc = frappe.get_doc("File", file_name) + if "/frappe_s3_attachment." in file_doc.file_url: + file_doc.delete() + return create_file(doc, compressed_content) + path = file_doc.get_full_path() with open(path, "wb") as f: @@ -430,6 +420,24 @@ def create_json_gz_file(data, doc, file_name=None) -> str: return doc.reposting_data_file +def create_file(doc, compressed_content): + json_filename = f"{scrub(doc.doctype)}-{scrub(doc.name)}.json.gz" + _file = frappe.get_doc( + { + "doctype": "File", + "file_name": json_filename, + "attached_to_doctype": doc.doctype, + "attached_to_name": doc.name, + "attached_to_field": "reposting_data_file", + "content": compressed_content, + "is_private": 1, + } + ) + _file.save(ignore_permissions=True) + + return _file.file_url + + def get_items_to_be_repost(voucher_type=None, voucher_no=None, doc=None, reposting_data=None): if not reposting_data and doc and doc.reposting_data_file: reposting_data = get_reposting_data(doc.reposting_data_file)