Пример #1
0
    def test_export_by_versions(self):
        """Test export_to_zip_file() for different versions."""
        exploration = self.save_new_default_exploration(self.EXP_ID)
        self.assertEqual(exploration.version, 1)

        exploration.add_states(['New state'])
        with open(os.path.join(feconf.TESTS_DATA_DIR, 'img.png')) as f:
            raw_image = f.read()
        fs = fs_domain.AbstractFileSystem(
            fs_domain.ExplorationFileSystem(self.EXP_ID))
        fs.commit(self.OWNER_ID, 'abc.png', raw_image)
        exp_services._save_exploration(self.OWNER_ID, exploration, '', [])
        self.assertEqual(exploration.version, 2)

        exploration.rename_state('New state', 'Renamed state')
        exp_services._save_exploration(self.OWNER_ID, exploration, '', [])
        self.assertEqual(exploration.version, 3)

        # Download version 2
        zip_file_output = exp_services.export_to_zip_file(self.EXP_ID, 2)
        zf = zipfile.ZipFile(StringIO.StringIO(zip_file_output))
        self.assertEqual(
            zf.open('A title.yaml').read(), self.SAMPLE_YAML_CONTENT)

        # Download version 3
        zip_file_output = exp_services.export_to_zip_file(self.EXP_ID, 3)
        zf = zipfile.ZipFile(StringIO.StringIO(zip_file_output))
        self.assertEqual(
            zf.open('A title.yaml').read(), self.UPDATED_YAML_CONTENT)
Пример #2
0
    def get(self, exploration_id):
        """Handles GET requests."""
        try:
            exploration = exp_services.get_exploration_by_id(exploration_id)
        except:
            raise self.PageNotFoundException

        version = self.request.get('v', default_value=exploration.version)
        output_format = self.request.get('output_format', default_value='zip')
        width = int(self.request.get('width', default_value=80))

        # If the title of the exploration has changed, we use the new title.
        filename = 'oppia-%s-v%s' % (utils.to_ascii(
            exploration.title.replace(' ', '')), version)

        if output_format == feconf.OUTPUT_FORMAT_ZIP:
            self.response.headers['Content-Type'] = 'text/plain'
            self.response.headers['Content-Disposition'] = (
                'attachment; filename=%s.zip' % str(filename))
            self.response.write(
                exp_services.export_to_zip_file(exploration_id, version))
        elif output_format == feconf.OUTPUT_FORMAT_JSON:
            self.render_json(
                exp_services.export_states_to_yaml(exploration_id,
                                                   version=version,
                                                   width=width))
        else:
            raise self.InvalidInputException('Unrecognized output format %s' %
                                             output_format)
Пример #3
0
    def get(self, exploration_id):
        """Handles GET requests."""
        exploration = exp_fetchers.get_exploration_by_id(exploration_id)
        version = self.normalized_request.get('v')
        output_format = self.normalized_request.get('output_format')

        if version is None:
            version = exploration.version

        # If the title of the exploration has changed, we use the new title.
        if not exploration.title:
            init_filename = 'oppia-unpublished_exploration-v%s.zip' % version
        else:
            init_filename = 'oppia-%s-v%s.zip' % (exploration.title.replace(
                ' ', ''), version)
        filename = utils.to_ascii(init_filename)

        if output_format == feconf.OUTPUT_FORMAT_ZIP:
            self.render_downloadable_file(
                exp_services.export_to_zip_file(exploration_id,
                                                version=version), filename,
                'text/plain')
        elif output_format == feconf.OUTPUT_FORMAT_JSON:
            self.render_json(
                exp_services.export_states_to_yaml(exploration_id,
                                                   version=version))
Пример #4
0
    def get(self, exploration_id):
        """Handles GET requests."""
        exploration = exp_fetchers.get_exploration_by_id(exploration_id)

        version_str = self.request.get('v', default_value=exploration.version)
        output_format = self.request.get('output_format', default_value='zip')

        try:
            version = int(version_str)
        except ValueError:
            version = exploration.version

        # If the title of the exploration has changed, we use the new title.
        if not exploration.title:
            init_filename = 'oppia-unpublished_exploration-v%s.zip' % version
        else:
            init_filename = 'oppia-%s-v%s.zip' % (
                exploration.title.replace(' ', ''), version)
        filename = utils.to_ascii(init_filename).decode('utf-8')

        if output_format == feconf.OUTPUT_FORMAT_ZIP:
            self.render_downloadable_file(
                exp_services.export_to_zip_file(
                    exploration_id, version=version),
                filename, 'text/plain')
        elif output_format == feconf.OUTPUT_FORMAT_JSON:
            self.render_json(exp_services.export_states_to_yaml(
                exploration_id, version=version))
        else:
            raise self.InvalidInputException(
                'Unrecognized output format %s' % output_format)
Пример #5
0
    def get(self, exploration_id):
        """Handles GET requests."""
        try:
            exploration = exp_services.get_exploration_by_id(exploration_id)
        except:
            raise self.PageNotFoundException

        if not rights_manager.Actor(self.user_id).can_view(
                rights_manager.ACTIVITY_TYPE_EXPLORATION, exploration_id):
            raise self.PageNotFoundException

        version = self.request.get('v', default_value=exploration.version)
        output_format = self.request.get('output_format', default_value='zip')
        width = int(self.request.get('width', default_value=80))

        # If the title of the exploration has changed, we use the new title
        filename = 'oppia-%s-v%s' % (
            utils.to_ascii(exploration.title.replace(' ', '')), version)

        if output_format == feconf.OUTPUT_FORMAT_ZIP:
            self.response.headers['Content-Type'] = 'text/plain'
            self.response.headers['Content-Disposition'] = (
                'attachment; filename=%s.zip' % str(filename))
            self.response.write(
                exp_services.export_to_zip_file(exploration_id, version))
        elif output_format == feconf.OUTPUT_FORMAT_JSON:
            self.render_json(exp_services.export_states_to_yaml(
                exploration_id, version=version, width=width))
        else:
            raise self.InvalidInputException(
                'Unrecognized output format %s' % output_format)
Пример #6
0
    def get(self, exploration_id):
        """Handles GET requests."""
        exploration = exp_services.get_exploration_by_id(exploration_id)
        filename = 'oppia-%s-v%s' % (
            utils.to_ascii(exploration.title), exploration.version)

        self.response.headers['Content-Type'] = 'text/plain'
        self.response.headers['Content-Disposition'] = (
            'attachment; filename=%s.zip' % filename)

        self.response.write(exp_services.export_to_zip_file(exploration_id))
Пример #7
0
    def test_export_to_zip_file(self):
        """Test the export_to_zip_file() method."""
        exploration = self.save_new_default_exploration(self.EXP_ID)
        exploration.add_states(['New state'])
        exp_services._save_exploration(self.OWNER_ID, exploration, '', [])

        zip_file_output = exp_services.export_to_zip_file(self.EXP_ID)
        zf = zipfile.ZipFile(StringIO.StringIO(zip_file_output))

        self.assertEqual(zf.namelist(), ['A title.yaml'])
        self.assertEqual(
            zf.open('A title.yaml').read(), self.SAMPLE_YAML_CONTENT)
Пример #8
0
    def get(self, exploration_id):
        """Handles GET requests."""

        exploration = exp_services.get_exploration_by_id(exploration_id)
        version = self.request.get('v', default_value=exploration.version)

        # If the title of the exploration has changed, we use the new title
        filename = 'oppia-%s-v%s' % (
            utils.to_ascii(exploration.title.replace(' ', '')), version)

        self.response.headers['Content-Type'] = 'text/plain'
        self.response.headers['Content-Disposition'] = (
            'attachment; filename=%s.zip' % str(filename))

        self.response.write(
            exp_services.export_to_zip_file(exploration_id, version))
Пример #9
0
    def get(self, exploration_id):
        """Handles GET requests."""

        exploration = exp_services.get_exploration_by_id(exploration_id)
        version = self.request.get('v', default_value=exploration.version)

        # If the title of the exploration has changed, we use the new title
        filename = 'oppia-%s-v%s' % (utils.to_ascii(
            exploration.title.replace(' ', '')), version)

        self.response.headers['Content-Type'] = 'text/plain'
        self.response.headers['Content-Disposition'] = (
            'attachment; filename=%s.zip' % str(filename))

        self.response.write(
            exp_services.export_to_zip_file(exploration_id, version))
Пример #10
0
    def test_export_to_zip_file_with_assets(self):
        """Test exporting an exploration with assets to a zip file."""
        exploration = self.save_new_default_exploration(self.EXP_ID)
        exploration.add_states(['New state'])
        exp_services._save_exploration(self.OWNER_ID, exploration, '', [])

        with open(os.path.join(feconf.TESTS_DATA_DIR, 'img.png')) as f:
            raw_image = f.read()
        fs = fs_domain.AbstractFileSystem(
            fs_domain.ExplorationFileSystem(self.EXP_ID))
        fs.commit(self.OWNER_ID, 'abc.png', raw_image)

        zip_file_output = exp_services.export_to_zip_file(self.EXP_ID)
        zf = zipfile.ZipFile(StringIO.StringIO(zip_file_output))

        self.assertEqual(zf.namelist(), ['A title.yaml', 'assets/abc.png'])
        self.assertEqual(
            zf.open('A title.yaml').read(), self.SAMPLE_YAML_CONTENT)
        self.assertEqual(zf.open('assets/abc.png').read(), raw_image)