<s> from dimagi . utils . couch . undo import DELETED_SUFFIX <EOL> from dimagi . utils . modules import to_function <EOL> from corehq . apps . reports . models import ( <EOL> FormExportSchema , <EOL> CaseExportSchema , <EOL> ) <EOL> from . const import ( <EOL> CASE_EXPORT , <EOL> FORM_EXPORT , <EOL> DEID_TRANSFORM_FUNCTIONS , <EOL> TRANSFORM_FUNCTIONS , <EOL> ) <EOL> def is_occurrence_deleted ( last_occurrences , app_ids_and_versions ) : <EOL> is_deleted = True <EOL> for app_id , version in app_ids_and_versions . iteritems ( ) : <EOL> if last_occurrences . get ( app_id ) == version : <EOL> is_deleted = False <EOL> break <EOL> return is_deleted <EOL> def convert_saved_export_to_export_instance ( domain , saved_export ) : <EOL> from . models import ( <EOL> FormExportDataSchema , <EOL> FormExportInstance , <EOL> CaseExportDataSchema , <EOL> CaseExportInstance , <EOL> PathNode , <EOL> ) <EOL> schema = None <EOL> instance_cls = None <EOL> export_type = saved_export . type <EOL> if export_type == FORM_EXPORT : <EOL> instance_cls = FormExportInstance <EOL> schema = FormExportDataSchema . generate_schema_from_builds ( <EOL> domain , <EOL> saved_export . app_id , <EOL> _extract_xmlns_from_index ( saved_export . index ) , <EOL> ) <EOL> elif export_type == CASE_EXPORT : <EOL> instance_cls = CaseExportInstance <EOL> schema = CaseExportDataSchema . generate_schema_from_builds ( <EOL> domain , <EOL> _extract_casetype_from_index ( saved_export . index ) , <EOL> ) <EOL> instance = instance_cls . generate_instance_from_schema ( schema ) <EOL> instance . name = saved_export . name <EOL> instance . is_deidentified = saved_export . is_safe <EOL> instance . export_format = saved_export . default_format <EOL> instance . transform_dates = saved_export . transform_dates <EOL> instance . legacy_saved_export_schema_id = saved_export . _id <EOL> if saved_export . type == FORM_EXPORT : <EOL> instance . split_multiselects = saved_export . split_multiselects <EOL> instance . include_errors = saved_export . include_errors <EOL> for old_table in saved_export . tables : <EOL> table_path = _convert_index_to_path_nodes ( old_table . index ) <EOL> new_table = instance . get_table ( _convert_index_to_path_nodes ( old_table . index ) ) <EOL> if new_table : <EOL> new_table . label = old_table . display <EOL> new_table . selected = True <EOL> else : <EOL> continue <EOL> for new_column in new_table . columns : <EOL> new_column . selected = False <EOL> for column in old_table . columns : <EOL> index = column . index <EOL> transform = None <EOL> if column . doc_type == '<STR_LIT>' : <EOL> index , new_column = new_table . get_column ( <EOL> [ PathNode ( name = '<STR_LIT>' ) ] , <EOL> '<STR_LIT>' , <EOL> None , <EOL> ) <EOL> if new_column : <EOL> new_column . selected = True <EOL> new_column . label = column . display <EOL> continue <EOL> if column . transform : <EOL> transform = _convert_transform ( column . transform ) <EOL> if _is_repeat ( old_table . index ) : <EOL> index = '<STR_LIT>' . format ( <EOL> table_index = _strip_repeat_index ( old_table . index ) , <EOL> column_index = column . index , <EOL> ) <EOL> column_path = _convert_index_to_path_nodes ( index ) <EOL> column_path = table_path + column_path [ len ( table_path ) : ] <EOL> system_property = _get_system_property ( <EOL> column . index , <EOL> transform , <EOL> export_type , <EOL> new_table . path <EOL> ) <EOL> if system_property : <EOL> column_path , transform = system_property <EOL> guess_types = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> for guess_type in guess_types : <EOL> index , new_column = new_table . get_column ( <EOL> column_path , <EOL> guess_type , <EOL> _strip_deid_transform ( transform ) , <EOL> ) <EOL> if new_column : <EOL> break <EOL> if not new_column : <EOL> continue <EOL> new_column . label = column . display <EOL> new_column . selected = True <EOL> if transform and not _strip_deid_transform ( transform ) : <EOL> new_column . deid_transform = transform <EOL> instance . save ( ) <EOL> saved_export . doc_type += DELETED_SUFFIX <EOL> saved_export . converted_saved_export_id = instance . _id <EOL> saved_export . save ( ) <EOL> return instance <EOL> def _extract_xmlns_from_index ( index ) : <EOL> return index [ <NUM_LIT:1> ] <EOL> def _extract_casetype_from_index ( index ) : <EOL> return index [ <NUM_LIT:1> ] <EOL> def _is_repeat ( index ) : <EOL> return index . startswith ( '<STR_LIT:#>' ) and index . endswith ( '<STR_LIT:#>' ) and index != '<STR_LIT:#>' <EOL> def _strip_repeat_index ( index ) : <EOL> index = index . strip ( '<STR_LIT>' ) <EOL> index = index . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return index <EOL> def _strip_deid_transform ( transform ) : <EOL> return None if transform in DEID_TRANSFORM_FUNCTIONS . keys ( ) else transform <EOL> def _convert_transform ( serializable_transform ) : <EOL> transform_fn = to_function ( serializable_transform . dumps_simple ( ) ) <EOL> if not transform_fn : <EOL> return None <EOL> for slug , fn in list ( TRANSFORM_FUNCTIONS . iteritems ( ) ) + list ( DEID_TRANSFORM_FUNCTIONS . iteritems ( ) ) : <EOL> if fn == transform_fn : <EOL> return slug <EOL> return None <EOL> def _get_system_property ( index , transform , export_type , table_path ) : <EOL> from . models import ( <EOL> MAIN_TABLE , <EOL> CASE_HISTORY_TABLE , <EOL> PARENT_CASE_TABLE , <EOL> ) <EOL> from . conversion_mappings import ( <EOL> FORM_PROPERTY_MAPPING , <EOL> CASE_PROPERTY_MAPPING , <EOL> CASE_HISTORY_PROPERTY_MAPPING , <EOL> PARENT_CASE_PROPERTY_MAPPING , <EOL> REPEAT_GROUP_PROPERTY_MAPPING , <EOL> ) <EOL> system_property = None <EOL> transform = _strip_deid_transform ( transform ) <EOL> if export_type == FORM_EXPORT : <EOL> if table_path == MAIN_TABLE : <EOL> system_property = FORM_PROPERTY_MAPPING . get ( ( index , transform ) ) <EOL> elif table_path [ - <NUM_LIT:1> ] . is_repeat : <EOL> system_property = REPEAT_GROUP_PROPERTY_MAPPING . get ( ( index , transform ) ) <EOL> elif export_type == CASE_EXPORT : <EOL> if table_path == MAIN_TABLE : <EOL> system_property = CASE_PROPERTY_MAPPING . get ( ( index , transform ) ) <EOL> elif table_path == CASE_HISTORY_TABLE : <EOL> system_property = CASE_HISTORY_PROPERTY_MAPPING . get ( ( index , transform ) ) <EOL> elif table_path == PARENT_CASE_TABLE : <EOL> system_property = PARENT_CASE_PROPERTY_MAPPING . get ( ( index , transform ) ) <EOL> return system_property <EOL> def _convert_index_to_path_nodes ( index ) : <EOL> from corehq . apps . export . models . new import MAIN_TABLE <EOL> from corehq . apps . export . models . new import PathNode <EOL> if index == '<STR_LIT:#>' : <EOL> return MAIN_TABLE <EOL> elif _is_repeat ( index ) : <EOL> split_index = index . split ( '<STR_LIT:.>' ) [ <NUM_LIT:1> : ] <EOL> path = [ ] <EOL> for part in split_index : <EOL> if part == '<STR_LIT:#>' : <EOL> path [ - <NUM_LIT:1> ] . is_repeat = True <EOL> else : <EOL> path . append ( PathNode ( name = part , is_repeat = False ) ) <EOL> return path <EOL> else : <EOL> return [ PathNode ( name = n ) for n in index . split ( '<STR_LIT:.>' ) ] <EOL> def revert_new_exports ( new_exports ) : <EOL> """<STR_LIT>""" <EOL> reverted_exports = [ ] <EOL> for new_export in new_exports : <EOL> if new_export . legacy_saved_export_schema_id : <EOL> schema_cls = FormExportSchema if new_export . type == FORM_EXPORT else CaseExportSchema <EOL> old_export = schema_cls . get ( new_export . legacy_saved_export_schema_id ) <EOL> old_export . doc_type = old_export . doc_type . rstrip ( DELETED_SUFFIX ) <EOL> old_export . save ( ) <EOL> reverted_exports . append ( old_export ) <EOL> new_export . doc_type += DELETED_SUFFIX <EOL> new_export . save ( ) <EOL> return reverted_exports </s>
