From 5a6d7ee191149291dc2b84391ca8fddeb9543388 Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Fri, 26 Nov 2021 17:29:44 +0530 Subject: [PATCH] refactor: get_mapping_module doesn't need to access to instance --- .../data_migration_plan.py | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/frappe/data_migration/doctype/data_migration_plan/data_migration_plan.py b/frappe/data_migration/doctype/data_migration_plan/data_migration_plan.py index 94ed77e2ec..d13912b431 100644 --- a/frappe/data_migration/doctype/data_migration_plan/data_migration_plan.py +++ b/frappe/data_migration/doctype/data_migration_plan/data_migration_plan.py @@ -1,5 +1,4 @@ -# -*- coding: utf-8 -*- -# Copyright (c) 2017, Frappe Technologies and contributors +# Copyright (c) 2021, Frappe Technologies and contributors # License: MIT. See LICENSE import frappe @@ -8,6 +7,20 @@ from frappe.modules.export_file import export_to_files, create_init_py from frappe.custom.doctype.custom_field.custom_field import create_custom_field from frappe.model.document import Document + +def get_mapping_module(module, mapping_name): + app_name = frappe.db.get_value("Module Def", module, "app_name") + mapping_name = frappe.scrub(mapping_name) + module = frappe.scrub(module) + + try: + return frappe.get_module( + f"{app_name}.{module}.data_migration_mapping.{mapping_name}" + ) + except ImportError: + return None + + class DataMigrationPlan(Document): def on_update(self): # update custom fields in mappings @@ -54,26 +67,14 @@ class DataMigrationPlan(Document): frappe.flags.ignore_in_install = False def pre_process_doc(self, mapping_name, doc): - module = self.get_mapping_module(mapping_name) + module = get_mapping_module(self.module, mapping_name) if module and hasattr(module, 'pre_process'): return module.pre_process(doc) return doc def post_process_doc(self, mapping_name, local_doc=None, remote_doc=None): - module = self.get_mapping_module(mapping_name) + module = get_mapping_module(self.module, mapping_name) if module and hasattr(module, 'post_process'): return module.post_process(local_doc=local_doc, remote_doc=remote_doc) - - def get_mapping_module(self, mapping_name): - try: - module_def = frappe.get_doc("Module Def", self.module) - module = frappe.get_module('{app}.{module}.data_migration_mapping.{mapping_name}'.format( - app= module_def.app_name, - module=frappe.scrub(self.module), - mapping_name=frappe.scrub(mapping_name) - )) - return module - except ImportError: - return None