Ejemplo n.º 1
0
    def map(item):
        try:
            exploration = exp_services.get_exploration_by_id(item.id)
        # Handle case where the exploration is deleted.
        except Exception as e:
            return

        latest_exp_version = exploration.version
        version_numbers = range(1, latest_exp_version + 1)

        try:
            exp_services.get_multiple_explorations_by_version(
                item.id, version_numbers)
        except Exception as e:
            yield (item.id, e)
            return
Ejemplo n.º 2
0
    def map(item):
        if item.deleted:
            return

        exploration = exp_services.get_exploration_from_model(item)
        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 with 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']
            # 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']
                exp_services.create_and_save_state_id_mapping_model_for_reverted_exploration(  # pylint: disable=line-too-long
                    exploration.id, exploration.version - 1, reverted_version)
            else:
                exp_services.create_and_save_state_id_mapping_model(
                    exploration, change_list)
        yield (exploration.id, exploration.version)
Ejemplo n.º 3
0
    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)