def get_mapped_record(self, doc): '''Build a mapped record using information from the fields table''' mapped = frappe._dict() key_fieldname = 'remote_fieldname' value_fieldname = 'local_fieldname' if self.mapping_type == 'Pull': key_fieldname, value_fieldname = value_fieldname, key_fieldname for field_map in self.fields: key = get_source_value(field_map, key_fieldname) if not field_map.is_child_table: # field to field mapping value = get_value_from_fieldname(field_map, value_fieldname, doc) else: # child table mapping mapping_name = field_map.child_table_mapping value = get_mapped_child_records( mapping_name, doc.get(get_source_value(field_map, value_fieldname))) mapped[key] = value return mapped
def get_mapped_record(self, doc): '''Build a mapped record using information from the fields table''' mapped = frappe._dict() key_fieldname = 'remote_fieldname' value_fieldname = 'local_fieldname' if self.mapping_type == 'Pull': key_fieldname, value_fieldname = value_fieldname, key_fieldname for field_map in self.fields: key = get_source_value(field_map, key_fieldname) if not field_map.is_child_table: # field to field mapping value = get_value_from_fieldname(field_map, value_fieldname, doc) else: # child table mapping mapping_name = field_map.child_table_mapping value = get_mapped_child_records(mapping_name, doc.get(get_source_value(field_map, value_fieldname))) mapped[key] = value return mapped
def get_value_from_fieldname(field_map, fieldname_field, doc): field_name = get_source_value(field_map, fieldname_field) if field_name.startswith('eval:'): value = frappe.safe_eval(field_name[5:], dict(frappe=frappe)) elif field_name[0] in ('"', "'"): value = field_name[1:-1] else: value = get_source_value(doc, field_name) return value
def get_value_from_fieldname(field_map, fieldname_field, doc): field_name = get_source_value(field_map, fieldname_field) if field_name.startswith('eval:'): value = frappe.safe_eval(field_name[5:], dict(frappe=frappe)) elif field_name[0] in ('"', "'"): value = field_name[1:-1] else: value = get_source_value(doc, field_name) return value
def pull(self): self.db_set('current_mapping_type', 'Pull') connection = self.get_connection() mapping = self.get_mapping(self.current_mapping) data = self.get_remote_data() for d in data: migration_id_value = get_source_value(d, connection.name_field) doc = self.pre_process_doc(d) doc = mapping.get_mapped_record(doc) if migration_id_value: try: if not local_doc_exists(mapping, migration_id_value): # insert new local doc local_doc = insert_local_doc(mapping, doc) self.update_log('pull_insert', 1) # set migration id frappe.db.set_value(mapping.local_doctype, local_doc.name, mapping.migration_id_field, migration_id_value, update_modified=False) frappe.db.commit() else: # update doc local_doc = update_local_doc(mapping, doc, migration_id_value) self.update_log('pull_update', 1) # post process doc after success self.post_process_doc(remote_doc=d, local_doc=local_doc) except Exception: # failed, append to log self.update_log('pull_failed', {migration_id_value: cstr(e)}) if len(data) < mapping.page_length: # last page, done with pull return True
def pull(self): self.db_set('current_mapping_type', 'Pull') connection = self.get_connection() mapping = self.get_mapping(self.current_mapping) data = self.get_remote_data() for d in data: migration_id_value = get_source_value(d, connection.name_field) doc = self.pre_process_doc(d) doc = mapping.get_mapped_record(doc) if migration_id_value: try: if not local_doc_exists(mapping, migration_id_value): # insert new local doc local_doc = insert_local_doc(mapping, doc) self.update_log('pull_insert', 1) # set migration id frappe.db.set_value(mapping.local_doctype, local_doc.name, mapping.migration_id_field, migration_id_value, update_modified=False) frappe.db.commit() else: # update doc local_doc = update_local_doc(mapping, doc, migration_id_value) self.update_log('pull_update', 1) # post process doc after success self.post_process_doc(remote_doc=d, local_doc=local_doc) except Exception: # failed, append to log self.update_log('pull_failed', migration_id_value) if len(data) < mapping.page_length: # last page, done with pull return True