fix: (bulk transaction) key error and better error logging (#32445)

* fix: (bulk transaction) key error and better error logging

* chore: pre-commit

* chore: linter - missing comma
This commit is contained in:
Dany Robert 2022-10-03 10:59:53 +05:30 committed by GitHub
parent 73e5a7d671
commit 91055151ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,7 +9,6 @@ from frappe import _
def transaction_processing(data, from_doctype, to_doctype): def transaction_processing(data, from_doctype, to_doctype):
if isinstance(data, str): if isinstance(data, str):
deserialized_data = json.loads(data) deserialized_data = json.loads(data)
else: else:
deserialized_data = data deserialized_data = data
@ -30,30 +29,29 @@ def transaction_processing(data, from_doctype, to_doctype):
def job(deserialized_data, from_doctype, to_doctype): def job(deserialized_data, from_doctype, to_doctype):
failed_history = [] fail_count = 0
i = 0
for d in deserialized_data: for d in deserialized_data:
failed = []
try: try:
i += 1
doc_name = d.get("name") doc_name = d.get("name")
frappe.db.savepoint("before_creation_state") frappe.db.savepoint("before_creation_state")
task(doc_name, from_doctype, to_doctype) task(doc_name, from_doctype, to_doctype)
except Exception as e: except Exception as e:
frappe.db.rollback(save_point="before_creation_state") frappe.db.rollback(save_point="before_creation_state")
failed_history.append(e) fail_count += 1
failed.append(e)
update_logger( update_logger(
doc_name, e, from_doctype, to_doctype, status="Failed", log_date=str(date.today()) doc_name,
str(frappe.get_traceback()),
from_doctype,
to_doctype,
status="Failed",
log_date=str(date.today()),
) )
if not failed: else:
update_logger( update_logger(
doc_name, None, from_doctype, to_doctype, status="Success", log_date=str(date.today()) doc_name, None, from_doctype, to_doctype, status="Success", log_date=str(date.today())
) )
show_job_status(failed_history, deserialized_data, to_doctype) show_job_status(fail_count, len(deserialized_data), to_doctype)
def task(doc_name, from_doctype, to_doctype): def task(doc_name, from_doctype, to_doctype):
@ -94,7 +92,7 @@ def task(doc_name, from_doctype, to_doctype):
"Purchase Invoice": purchase_order.make_purchase_invoice, "Purchase Invoice": purchase_order.make_purchase_invoice,
"Purchase Receipt": purchase_order.make_purchase_receipt, "Purchase Receipt": purchase_order.make_purchase_receipt,
}, },
"Purhcase Invoice": { "Purchase Invoice": {
"Purchase Receipt": purchase_invoice.make_purchase_receipt, "Purchase Receipt": purchase_invoice.make_purchase_receipt,
"Payment": payment_entry.get_payment_entry, "Payment": payment_entry.get_payment_entry,
}, },
@ -150,15 +148,14 @@ def update_logger(doc_name, e, from_doctype, to_doctype, status, log_date=None,
log_doc.save() log_doc.save()
def show_job_status(failed_history, deserialized_data, to_doctype): def show_job_status(fail_count, deserialized_data_count, to_doctype):
if not failed_history: if not fail_count:
frappe.msgprint( frappe.msgprint(
_("Creation of {0} successful").format(to_doctype), _("Creation of {0} successful").format(to_doctype),
title="Successful", title="Successful",
indicator="green", indicator="green",
) )
elif fail_count != 0 and fail_count < deserialized_data_count:
if len(failed_history) != 0 and len(failed_history) < len(deserialized_data):
frappe.msgprint( frappe.msgprint(
_( _(
"""Creation of {0} partially successful. """Creation of {0} partially successful.
@ -167,8 +164,7 @@ def show_job_status(failed_history, deserialized_data, to_doctype):
title="Partially successful", title="Partially successful",
indicator="orange", indicator="orange",
) )
else:
if len(failed_history) == len(deserialized_data):
frappe.msgprint( frappe.msgprint(
_( _(
"""Creation of {0} failed. """Creation of {0} failed.
@ -180,9 +176,7 @@ def show_job_status(failed_history, deserialized_data, to_doctype):
def record_exists(log_doc, doc_name, status): def record_exists(log_doc, doc_name, status):
record = mark_retrired_transaction(log_doc, doc_name) record = mark_retrired_transaction(log_doc, doc_name)
if record and status == "Failed": if record and status == "Failed":
return False return False
elif record and status == "Success": elif record and status == "Success":