def map(item): if item.deleted: return try: exploration = exp_services.get_exploration_from_model(item) except Exception as e: yield ('ERROR get_exp_from_model: exp_id %s' % item.id, str(e)) return # Commit commands which are required to generate state id mapping for # given version of exploration from previous version of exploration. RELEVANT_COMMIT_CMDS = [ exp_domain.CMD_ADD_STATE, exp_domain.CMD_RENAME_STATE, exp_domain.CMD_DELETE_STATE, exp_models.ExplorationModel.CMD_REVERT_COMMIT ] explorations = [] # Fetch all versions of the exploration, if they exist. if exploration.version > 1: # Ignore latest version since we already have corresponding # exploration. versions = range(1, exploration.version) # Get all exploration versions for current exploration id. try: explorations = ( exp_services.get_multiple_explorations_by_version( exploration.id, versions)) except Exception as e: yield ( 'ERROR get_multiple_exp_by_version exp_id %s' % item.id, str(e)) return # Append latest exploration to the list of explorations. explorations.append(exploration) # Retrieve list of snapshot models representing each version of the # exploration. versions = range(1, exploration.version + 1) snapshots_by_version = ( exp_models.ExplorationModel.get_snapshots_metadata( exploration.id, versions)) # Create and save state id mapping model for all exploration versions. for exploration, snapshot in zip(explorations, snapshots_by_version): if snapshot is None: yield ( 'ERROR with exp_id %s' % item.id, 'Error: No exploration snapshot metadata model instance ' 'found for exploration %s, version %d' % ( exploration.id, exploration.version)) return change_list = [ exp_domain.ExplorationChange(change_cmd) for change_cmd in snapshot['commit_cmds'] if change_cmd['cmd'] in RELEVANT_COMMIT_CMDS ] try: # Check if commit is to revert the exploration. if change_list and change_list[0].cmd == ( exp_models.ExplorationModel.CMD_REVERT_COMMIT): reverted_version = change_list[0].version_number # pylint: disable=line-too-long state_id_mapping = exp_services.generate_state_id_mapping_model_for_reverted_exploration( exploration.id, exploration.version - 1, reverted_version) # pylint: enable=line-too-long else: state_id_mapping = ( exp_services.generate_state_id_mapping_model( exploration, change_list)) state_id_mapping_model = exp_models.StateIdMappingModel.create( state_id_mapping.exploration_id, state_id_mapping.exploration_version, state_id_mapping.state_names_to_ids, state_id_mapping.largest_state_id_used, overwrite=True) state_id_mapping_model.put() except Exception as e: yield ( 'ERROR with exp_id %s version %s' % ( item.id, exploration.version), traceback.format_exc()) return yield (exploration.id, exploration.version)
def map(item): if item.deleted: return try: exploration = exp_services.get_exploration_from_model(item) except Exception as e: yield ('ERROR get_exp_from_model: exp_id %s' % item.id, str(e)) return explorations = [] # Fetch all versions of the exploration, if they exist. if exploration.version > 1: # Ignore latest version since we already have corresponding # exploration. versions = range(1, exploration.version) # Get all exploration versions for current exploration id. try: explorations = ( exp_services.get_multiple_explorations_by_version( exploration.id, versions)) except Exception as e: yield ('ERROR get_multiple_exp_by_version exp_id %s' % item.id, str(e)) return # Append latest exploration to the list of explorations. explorations.append(exploration) # Retrieve list of snapshot models representing each version of the # exploration. versions = range(1, exploration.version + 1) snapshots_by_version = ( exp_models.ExplorationModel.get_snapshots_metadata( exploration.id, versions)) # Create and save state id mapping model for all exploration versions. for exploration, snapshot in zip(explorations, snapshots_by_version): if snapshot is None: yield ( 'ERROR with exp_id %s' % item.id, 'Error: No exploration snapshot metadata model instance ' 'found for exploration %s, version %d' % (exploration.id, exploration.version)) return change_list = snapshot['commit_cmds'] try: # Check if commit is to revert the exploration. if change_list and change_list[0]['cmd'].endswith( 'revert_version_number'): reverted_version = change_list[0]['version_number'] # pylint: disable=line-too-long state_id_mapping = exp_services.generate_state_id_mapping_model_for_reverted_exploration( exploration.id, exploration.version - 1, reverted_version) # pylint: enable=line-too-long else: state_id_mapping = ( exp_services.generate_state_id_mapping_model( exploration, change_list)) state_id_mapping_model = exp_models.StateIdMappingModel.create( state_id_mapping.exploration_id, state_id_mapping.exploration_version, state_id_mapping.state_names_to_ids, state_id_mapping.largest_state_id_used, overwrite=True) state_id_mapping_model.put() except Exception as e: yield ('ERROR with exp_id %s version %s' % (item.id, exploration.version), str(e)) return yield (exploration.id, exploration.version)