def migrate_to_external_account(user_settings_document):
    if not user_settings_document.get('access_token'):
        return (None, None, None)
    new = False
    user = User.load(user_settings_document['owner'])
    try:
        external_account = ExternalAccount.find(Q('provider_id', 'eq', user_settings_document['dropbox_id']))[0]
        logger.info('Duplicate account use found: User {0} with dropbox_id {1}'.format(user.username, user_settings_document['dropbox_id']))
    except __HOLE__:
        new = True
        external_account = ExternalAccount(
            provider=PROVIDER,
            provider_name=PROVIDER_NAME,
            provider_id=user_settings_document['dropbox_id'],
            oauth_key=user_settings_document['access_token'],
            display_name=user_settings_document['dropbox_info'].get('display_name', None) if user_settings_document.get('dropbox_info', None) else None,
        )
        external_account.save()  # generate pk for external accountc

    user.external_accounts.append(external_account)
    user.save()
    return external_account, user, new
dataset/ETHPy150Open CenterForOpenScience/osf.io/scripts/dropbox/migrate_to_external_accounts.py/migrate_to_external_account