<s> """<STR_LIT>""" <EOL> import sys <EOL> import logging <EOL> from modularodm import Q <EOL> from modularodm . storage . base import KeyExistsException <EOL> from website . app import init_app <EOL> from scripts import utils as script_utils <EOL> from framework . transactions . context import TokuTransaction <EOL> from framework . mongo import database <EOL> from website . addons . box . model import BoxUserSettings <EOL> from website . addons . box . model import BoxNodeSettings <EOL> from website . oauth . models import ExternalAccount <EOL> logger = logging . getLogger ( __name__ ) <EOL> def do_migration ( records ) : <EOL> database [ '<STR_LIT>' ] . update ( { '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:2> } } , { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } } , multi = True ) <EOL> for user_addon in records : <EOL> user = user_addon . owner <EOL> old_account = user_addon . oauth_settings <EOL> logger . info ( '<STR_LIT>' . format ( user . _id ) ) <EOL> try : <EOL> account = ExternalAccount ( <EOL> provider = '<STR_LIT>' , <EOL> provider_name = '<STR_LIT>' , <EOL> display_name = old_account . username , <EOL> oauth_key = old_account . access_token , <EOL> refresh_token = old_account . refresh_token , <EOL> provider_id = old_account . user_id , <EOL> expires_at = old_account . expires_at , <EOL> ) <EOL> account . save ( ) <EOL> except KeyExistsException : <EOL> account = ExternalAccount . find_one ( <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) & <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , old_account . user_id ) <EOL> ) <EOL> assert account is not None <EOL> user . external_accounts . append ( account ) <EOL> user . save ( ) <EOL> user_addon . oauth_settings = None <EOL> user_addon . save ( ) <EOL> logger . info ( '<STR_LIT>' . format ( <EOL> account . _id , user . _id , <EOL> ) ) <EOL> for node in BoxNodeSettings . find ( ) : <EOL> if node . foreign_user_settings is None : <EOL> continue <EOL> logger . info ( '<STR_LIT>' . format ( node . _id ) ) <EOL> node . user_settings = node . foreign_user_settings <EOL> node . save ( ) <EOL> def get_targets ( ) : <EOL> return BoxUserSettings . find ( <EOL> Q ( '<STR_LIT>' , '<STR_LIT>' , None ) <EOL> ) <EOL> def main ( dry = True ) : <EOL> init_app ( set_backends = True , routes = False ) <EOL> with TokuTransaction ( ) : <EOL> do_migration ( get_targets ( ) ) <EOL> if dry : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> dry = '<STR_LIT>' in sys . argv <EOL> if not dry : <EOL> script_utils . add_file_logger ( logger , __file__ ) <EOL> main ( dry = dry ) </s>
