<s> from celery . task import task <EOL> from couchdbkit . exceptions import ResourceNotFound <EOL> from casexml . apps . case . xml import V1 <EOL> from casexml . apps . phone . restore import RestoreParams , RestoreCacheSettings , RestoreConfig <EOL> from corehq . apps . users . models import CommCareUser <EOL> from soil import DownloadBase <EOL> @ task <EOL> def prime_restore ( domain , usernames_or_ids , version = V1 , cache_timeout_hours = None , <EOL> overwrite_cache = False , check_cache_only = False ) : <EOL> """<STR_LIT>""" <EOL> total = len ( usernames_or_ids ) <EOL> DownloadBase . set_progress ( prime_restore , <NUM_LIT:0> , total ) <EOL> ret = { '<STR_LIT>' : [ ] } <EOL> for i , username_or_id in enumerate ( usernames_or_ids ) : <EOL> couch_user = get_user ( username_or_id , domain ) <EOL> if not couch_user : <EOL> ret [ '<STR_LIT>' ] . append ( '<STR_LIT>' . format ( username_or_id ) ) <EOL> continue <EOL> elif couch_user . domain != domain : <EOL> ret [ '<STR_LIT>' ] . append ( "<STR_LIT>" . format ( <EOL> username_or_id , <EOL> domain <EOL> ) ) <EOL> continue <EOL> try : <EOL> project = couch_user . project <EOL> restore_config = RestoreConfig ( <EOL> project = project , <EOL> user = couch_user . to_casexml_user ( ) , <EOL> params = RestoreParams ( <EOL> version = version , <EOL> include_item_count = True , <EOL> ) , <EOL> cache_settings = RestoreCacheSettings ( <EOL> force_cache = True , <EOL> cache_timeout = cache_timeout_hours * <NUM_LIT> * <NUM_LIT> , <EOL> overwrite_cache = overwrite_cache <EOL> ) <EOL> ) <EOL> if check_cache_only : <EOL> cached_payload = _get_cached_payload ( restore_config ) <EOL> ret [ '<STR_LIT>' ] . append ( u'<STR_LIT>' . format ( <EOL> '<STR_LIT>' if cached_payload else '<STR_LIT>' , <EOL> couch_user . human_friendly_name , <EOL> ) ) <EOL> else : <EOL> restore_config . get_payload ( ) <EOL> cached_payload = _get_cached_payload ( restore_config ) <EOL> if cached_payload : <EOL> ret [ '<STR_LIT>' ] . append ( '<STR_LIT>' . format ( <EOL> couch_user . human_friendly_name , <EOL> ) ) <EOL> else : <EOL> ret [ '<STR_LIT>' ] . append ( '<STR_LIT>' . format ( <EOL> couch_user . human_friendly_name , <EOL> ) ) <EOL> except Exception as e : <EOL> ret [ '<STR_LIT>' ] . append ( '<STR_LIT>' . format ( str ( e ) ) ) <EOL> DownloadBase . set_progress ( prime_restore , i + <NUM_LIT:1> , total ) <EOL> return ret <EOL> def _get_cached_payload ( restore_config ) : <EOL> original = restore_config . overwrite_cache <EOL> try : <EOL> restore_config . overwrite_cache = False <EOL> payload = restore_config . get_cached_payload ( ) <EOL> finally : <EOL> restore_config . overwrite_cache = original <EOL> return payload <EOL> def get_user ( username_or_id , domain ) : <EOL> if '<STR_LIT:@>' in username_or_id : <EOL> return get_user_from_username ( username_or_id ) <EOL> else : <EOL> try : <EOL> couch_user = CommCareUser . get ( username_or_id ) <EOL> return couch_user <EOL> except ResourceNotFound : <EOL> username_or_id = '<STR_LIT>' . format ( username_or_id , domain ) <EOL> return get_user_from_username ( username_or_id ) <EOL> def get_user_from_username ( username ) : <EOL> try : <EOL> return CommCareUser . get_by_username ( username ) <EOL> except ResourceNotFound : <EOL> return None </s>
