I need to be able to set some metadata in some custom data types. For now, I’m just trying to set the value of the ‘misc_info’ field. The client script is this:

put( sys.argv[1], sys.argv[2], { 'update_type' : 'metadata', 'misc_info' : 'meta data msg' } )

and my API method is as follows. It executes fine, but the values do not show on the next show() call.

Disclaimer: Python is still quite new to me, and it’s very likely I do not understand the metadata model and the way library dataset associates work.

 

Any clues will be greatly appreaciated!

@web.expose_api

    def update( self, trans, id,  library_id, payload, **kwd ):

        """

        PUT /api/libraries/{encoded_library_id}/contents/{encoded_content_type_and_id}

        Sets attributes (metadata) on a library item.

        """

        update_type = None

        if 'update_type' not in payload:

            trans.response.status = 400

            return "Missing required 'update_type' parameter.  Please consult the API documentation for help."

        else:

            update_type = payload.pop( 'update_type' )

        if update_type not in ( 'metadata' ):

            trans.response.status = 400

            return "Invalid value for 'update_type' parameter ( %s ) specified.  Please consult the API documentation for help." % update_type

        content_id = id

        decoded_type_and_id = trans.security.decode_string_id( content_id )

        content_type, decoded_content_id = decoded_type_and_id.split( '.' )

        if content_type not in ( 'file' ):

            trans.response.status = 400

            return "Updates allowed only on files, not directories"

        try:

            content = trans.sa_session.query( trans.app.model.LibraryDatasetDatasetAssociation ).get( decoded_content_id )

        except:

            content = None

        if not content or ( not trans.user_is_admin() and not trans.app.security_agent.can_modify_library_item( trans.get_current_user_roles(), content, trans.user ) ):

            trans.response.status = 400

            return "Invalid %s id ( %s ) specified." % ( content_type, str( content_id ) )

 

        metadata = content.get_metadata()

        content.datatype.before_setting_metadata(content)

       

        if not metadata:

           metadata = {}

       

        for name in payload:

            if name not in [ 'name', 'info', 'dbkey' ]:

                setattr( metadata, name, payload[name])

       

        content.set_metadata(metadata)

        content.datatype.after_setting_metadata( content )

        trans.sa_session.flush()

 

        return "OK"

   

 

John Duddy
Sr. Staff Software Engineer
Illumina, Inc.
9885 Towne Centre Drive
San Diego, CA 92121
Tel: 858-736-3584
E-mail:
jduddy@illumina.com