def test_data_import_update(self): existing_doc = frappe.get_doc(doctype=doctype_name, title=frappe.generate_hash( doctype_name, 8), table_field_1=[{ 'child_title': 'child title to update' }]) existing_doc.save() frappe.db.commit() import_file = get_import_file('sample_import_file_for_update') data_import = self.get_importer(doctype_name, import_file, update=True) i = Importer(data_import.reference_doctype, data_import=data_import) # update child table id in template date i.import_file.raw_data[1][4] = existing_doc.table_field_1[0].name i.import_file.raw_data[1][0] = existing_doc.name i.import_file.parse_data_from_template() i.import_data() updated_doc = frappe.get_doc(doctype_name, existing_doc.name) self.assertEqual(updated_doc.description, 'test description') self.assertEqual(updated_doc.table_field_1[0].child_title, 'child title') self.assertEqual(updated_doc.table_field_1[0].name, existing_doc.table_field_1[0].name) self.assertEqual(updated_doc.table_field_1[0].child_description, 'child description') self.assertEqual(updated_doc.table_field_1_again[0].child_title, 'child title again')
def import_file(doctype, file_path, import_type, submit_after_import=False, console=False): """ Import documents in from CSV or XLSX using data import. :param doctype: DocType to import :param file_path: Path to .csv, .xls, or .xlsx file to import :param import_type: One of "Insert" or "Update" :param submit_after_import: Whether to submit documents after import :param console: Set to true if this is to be used from command line. Will print errors or progress to stdout. """ data_import = frappe.new_doc("Data Import") data_import.submit_after_import = submit_after_import data_import.import_type = ("Insert New Records" if import_type.lower() == "insert" else "Update Existing Records") i = Importer(doctype=doctype, file_path=file_path, data_import=data_import, console=console) i.import_data()
def start_import(data_import, bank_account, import_file_path, google_sheets_url, bank, template_options): """This method runs in background job""" update_mapping_db(bank, template_options) data_import = frappe.get_doc("Bank Statement Import", data_import) file = import_file_path if import_file_path else google_sheets_url import_file = ImportFile("Bank Transaction", file=file, import_type="Insert New Records") data = import_file.raw_data if import_file_path: add_bank_account(data, bank_account) write_files(import_file, data) try: i = Importer(data_import.reference_doctype, data_import=data_import) i.import_data() except Exception: frappe.db.rollback() data_import.db_set("status", "Error") frappe.log_error(title=data_import.name) finally: frappe.flags.in_import = False frappe.publish_realtime("data_import_refresh", {"data_import": data_import.name})
def start_import(data_import): """This method runs in background job""" data_import = frappe.get_doc("Data Import", data_import) try: i = Importer(data_import.reference_doctype, data_import=data_import) i.import_data() except Exception: frappe.db.rollback() data_import.db_set("status", "Error") frappe.log_error(title=data_import.name) finally: frappe.flags.in_import = False frappe.publish_realtime("data_import_refresh", {"data_import": data_import.name})
def test_data_import_update(self): existing_doc = frappe.get_doc( doctype=doctype_name, title=frappe.generate_hash(doctype_name, 8), table_field_1=[{ "child_title": "child title to update" }], ) existing_doc.save() frappe.db.commit() import_file = get_import_file("sample_import_file_for_update") data_import = self.get_importer(doctype_name, import_file, update=True) i = Importer(data_import.reference_doctype, data_import=data_import) # update child table id in template date i.import_file.raw_data[1][4] = existing_doc.table_field_1[0].name # uppercase to check if autoname field isn't replaced in mariadb if frappe.db.db_type == "mariadb": i.import_file.raw_data[1][0] = existing_doc.name.upper() else: i.import_file.raw_data[1][0] = existing_doc.name i.import_file.parse_data_from_template() i.import_data() updated_doc = frappe.get_doc(doctype_name, existing_doc.name) self.assertEqual(existing_doc.title, updated_doc.title) self.assertEqual(updated_doc.description, "test description") self.assertEqual(updated_doc.table_field_1[0].child_title, "child title") self.assertEqual(updated_doc.table_field_1[0].name, existing_doc.table_field_1[0].name) self.assertEqual(updated_doc.table_field_1[0].child_description, "child description") self.assertEqual(updated_doc.table_field_1_again[0].child_title, "child title again")
def get_importer(self): return Importer(self.reference_doctype, data_import=self)