Esempio n. 1
0
    def PUT(self, repository_id, asset_id):
        try:
            main_input_file = web.input(inputFile={})
            vtt_input_file = web.input(vttFile={})
            transcript_input_file = web.input(transcriptFile={})

            asset_id = utilities.clean_id(asset_id)

            rm = rutils.get_repository_manager()
            repo = rm.get_repository(utilities.clean_id(repository_id))
            params = self.data()

            locale = 'en'
            if 'locale' in params:
                locale = params['locale']

            # update the asset contents here, for convenience
            if 'clearAltTexts' in params and params['clearAltTexts']:
                rutils.clear_alt_texts(repo, asset_id)
            if 'altText' in params:
                # find the right asset content by genus type. Grab its ID
                # then get the asset content update form
                # call form.add_display_name(new alt text)
                # update form
                rutils.add_alt_text_to_asset(
                    repo, asset_id, utilities.clean_json(params['altText']))
            if 'removeAltTextLanguage' in params:
                rutils.remove_alt_text_language(
                    repo, asset_id, params['removeAltTextLanguage'])

            if 'clearMediaDescriptions' in params and params[
                    'clearMediaDescriptions']:
                rutils.clear_media_descriptions(repo, asset_id)
            if 'mediaDescription' in params:
                rutils.add_media_description_to_asset(
                    repo, asset_id,
                    utilities.clean_json(params['mediaDescription']))
            if 'removeMediaDescriptionLanguage' in params:
                rutils.remove_media_description_language(
                    repo, asset_id, params['removeMediaDescriptionLanguage'])

            # Now handle the vtt and transcript uploaded files
            if 'clearVTTFiles' in params and params['clearVTTFiles']:
                rutils.clear_vtt_files(repo, asset_id)
            try:
                vtt_file = vtt_input_file['vttFile'].file
            except AttributeError:
                pass
            else:
                file_name = vtt_input_file['vttFile'].filename
                rutils.add_vtt_file_to_asset(repo, asset_id, file_name,
                                             vtt_file, locale)
            if 'removeVTTFileLanguage' in params:
                rutils.remove_vtt_file_language(
                    repo, asset_id, params['removeVTTFileLanguage'])

            if 'clearTranscriptFiles' in params and params[
                    'clearTranscriptFiles']:
                rutils.clear_transcript_files(repo, asset_id)
            try:
                transcript_file = transcript_input_file['transcriptFile'].file
            except AttributeError:
                pass
            else:
                file_name = transcript_input_file['transcriptFile'].filename
                rutils.add_transcript_file_to_asset(repo, asset_id, file_name,
                                                    transcript_file, locale)
            if 'removeTranscriptFileLanguage' in params:
                rutils.remove_transcript_file_language(
                    repo, asset_id, params['removeTranscriptFileLanguage'])

            # also handle updating the main asset content (image or video or audio)
            try:
                main_file = main_input_file['inputFile'].file
            except AttributeError:
                pass
            else:
                file_name = main_input_file['inputFile'].filename
                rutils.replace_asset_main_content(repo, asset_id, file_name,
                                                  main_file)

            form = repo.get_asset_form_for_update(asset_id)
            form = utilities.set_form_basics(form, params)

            if 'license' in params.keys():
                form.set_license(params['license'])

            if 'copyright' in params.keys():
                form.set_copyright(params['copyright'])

            if 'source' in params.keys():
                resource_id = resource_utils.get_or_create_resource_id(
                    repo, params['source'])
                form.set_source(resource_id)

            if 'provider' in params.keys():
                resource_id = resource_utils.get_or_create_resource_id(
                    repo, params['provider'])
                form.set_provider(resource_id)

            repo.update_asset(form)

            if 'createdDate' in params.keys():
                rutils.set_asset_created_date(
                    repo, asset_id, DateTime(**params['createdDate']))

            data = utilities.convert_dl_object(repo.get_asset(asset_id))

            # Update the source field with the displayName.text of the actual resource
            data = resource_utils.update_asset_map_with_resource(data)

            return data
        except Exception as ex:
            utilities.handle_exceptions(ex)
Esempio n. 2
0
    def POST(self, repository_id):
        try:
            main_input_file = web.input(inputFile={})
            vtt_input_file = web.input(vttFile={})
            transcript_input_file = web.input(transcriptFile={})

            rm = rutils.get_repository_manager()
            repository = rm.get_repository(utilities.clean_id(repository_id))
            repository.use_isolated_repository_view()
            # get each set of files individually, because
            # we are doing this in memory, so the file pointer changes
            # once we read in a file
            # https://docs.python.org/2/library/zipfile.html
            params = self.data()
            locale = 'en'
            if 'locale' in params:
                locale = params['locale']

            if locale not in ['en', 'hi', 'te']:
                raise TypeError(
                    "The 'locale' parameter must be one of: 'en', 'hi', or 'te'"
                )

            try:
                input_file = main_input_file['inputFile'].file
            except AttributeError:
                form = repository.get_asset_form_for_create([])
                form = utilities.set_form_basics(form, params)
                asset = repository.create_asset(form)
            else:
                # first, let's search the database to see if an asset exists with
                # the file name -- file name will be everything minus the
                # extension and language indicator.
                # for example, for one video there might be four files:
                #   * ee_u1l01a01v01.mov
                #   * ee_u1l01a01v01_en.vtt
                #   * ee_u1l01a01v01_hi.vtt
                #   * ee_u1l01a01v01_te.vtt
                # In this case, the "filename" / asset displayName.text is `ee_u1l01a01v01`.
                # If that already exists, add the input_file as an asset content.
                # If that asset does not exist, create it.
                file_name = main_input_file['inputFile'].filename

                if 'createNew' in params.keys() and params['createNew']:
                    asset = rutils.create_asset(repository, file_name)
                else:
                    try:
                        querier = repository.get_asset_query()
                    except (AttributeError, Unimplemented, PermissionDenied):
                        assets = None
                    else:
                        querier.match_display_name(
                            rutils.get_singular_filename(file_name),
                            match=True)
                        assets = repository.get_assets_by_query(querier)

                    if assets is not None and assets.available() > 0:
                        asset = assets.next()
                    else:
                        asset = rutils.create_asset(repository, file_name)

                # now let's create an asset content for this asset, with the
                # right genus type and file data
                rutils.append_file_as_asset_content(repository, asset.ident,
                                                    file_name, input_file)

            # Check if transcripts or VTT files are included
            try:
                vtt_file = vtt_input_file['vttFile'].file
            except AttributeError:
                pass
            else:
                file_name = vtt_input_file['vttFile'].filename
                rutils.append_vtt_file_as_asset_content(
                    repository, asset.ident, file_name, vtt_file, locale)
            try:
                transcript_file = transcript_input_file['transcriptFile'].file
            except AttributeError:
                pass
            else:
                file_name = transcript_input_file['transcriptFile'].filename
                rutils.append_transcript_file_as_asset_content(
                    repository, asset.ident, file_name, transcript_file,
                    locale)

            if any(term in params.keys()
                   for term in ['license', 'copyright', 'source', 'provider']):
                form = repository.get_asset_form_for_update(asset.ident)
                if 'license' in params.keys():
                    form.set_license(params['license'])
                if 'copyright' in params.keys():
                    form.set_copyright(params['copyright'])
                if 'source' in params.keys():
                    resource_id = resource_utils.get_or_create_resource_id(
                        repository, params['source'])
                    form.set_source(resource_id)
                if 'provider' in params.keys():
                    resource_id = resource_utils.get_or_create_resource_id(
                        repository, params['provider'])
                    form.set_provider(resource_id)
                asset = repository.update_asset(form)

            # Handle the alt-text for images
            if 'altText' in params.keys():
                rutils.append_text_as_asset_content(
                    repository, asset.ident,
                    utilities.clean_json(params['altText']), 'Alt text',
                    'alt-text')
            if 'mediaDescription' in params.keys():
                rutils.append_text_as_asset_content(
                    repository, asset.ident,
                    utilities.clean_json(params['mediaDescription']),
                    'Description', 'mediaDescription')

            if 'createdDate' in params.keys():
                # set the createdDate
                rutils.set_asset_created_date(
                    repository, asset.ident,
                    DateTime(**utilities.clean_json(params['createdDate'])))

            # need to get the updated asset with Contents
            asset = repository.get_asset(asset.ident)
            asset_map = json.loads(utilities.convert_dl_object(asset))
            if 'returnUrl' in web.input().keys():
                asset_map = rutils.update_asset_map_with_content_url(
                    rm, asset_map)

            # Update the source field with the displayName.text of the actual resource
            asset_map = resource_utils.update_asset_map_with_resource(
                asset_map)

            return json.dumps(asset_map)
        except Exception as ex:
            utilities.handle_exceptions(ex)