'Migrate records from another doctype'

def migrate_doctype(self, doctype, filters=None, update=None, verbose=1, exclude=None, preprocess=None):

    meta = frappe.get_meta(doctype)
    tables = {}
    for df in meta.get_table_fields():
        if verbose:
            print(('getting    ' + df.options))
        tables[df.fieldname] = self.get_list(df.options, limit_page_length=999999)
    if verbose:
        print(('getting    ' + doctype))
    docs = self.get_list(doctype, limit_page_length=999999, filters=filters)
    if tables:
        docs = [frappe._dict(doc) for doc in docs]
        docs_map = dict(((doc.name, doc) for doc in docs))
        for fieldname in tables:
            for child in tables[fieldname]:
                child = frappe._dict(child)
                if (child.parent in docs_map):
                    docs_map[child.parent].setdefault(fieldname, []).append(child)
    if verbose:
        print(('inserting    ' + doctype))
    for doc in docs:
        if (exclude and (doc['name'] in exclude)):
            continue
        if preprocess:
            preprocess(doc)
        if (not doc.get('owner')):
            doc['owner'] = 'Administrator'
        if ((doctype != 'User') and (not frappe.db.exists('User', doc.get('owner')))):
            frappe.get_doc({'doctype': 'User', 'email': doc.get('owner'), 'first_name': doc.get('owner').split('@')[0]}).insert()
        if update:
            doc.update(update)
        doc['doctype'] = doctype
        new_doc = frappe.get_doc(doc)
        new_doc.insert()
        if (not meta.istable):
            if (doctype != 'Communication'):
                self.migrate_doctype('Communication', {'reference_doctype': doctype, 'reference_name': doc['name']}, update={'reference_name': new_doc.name}, verbose=0)
            if (doctype != 'File'):
                self.migrate_doctype('File', {'attached_to_doctype': doctype, 'attached_to_name': doc['name']}, update={'attached_to_name': new_doc.name}, verbose=0)
