

def do_migration(records):
    host = 'dataverse.harvard.edu'
    for user_addon in records:
        user = user_addon.owner
        api_token = user_addon.api_token
        logger.info('Record found for user {}'.format(user._id))
        try:
            account = ExternalAccount(provider='dataverse', provider_name='Dataverse', display_name=host, oauth_key=host, oauth_secret=api_token, provider_id=api_token)
            account.save()
        except KeyExistsException:
            account = ExternalAccount.find_one((Q('provider', 'eq', 'dataverse') & Q('provider_id', 'eq', api_token)))
            assert (account is not None)
        user.external_accounts.append(account)
        user.save()
        user_addon.api_token = None
        user_addon.save()
        logger.info('Added external account {0} to user {1}'.format(account._id, user._id))
        for node_addon in get_authorized_node_settings(user_addon):
            node_addon.set_auth(account, user)
            logger.info('Added external account {0} to node {1}'.format(account._id, node_addon.owner._id))

dataset/ETHPy150Open CenterForOpenScience/osf.io/scripts/dataverse/migrate_to_external_account.py do_migration/original