Example #1
0
 def _get_datastreams(self, pid, asOfDateTime=None):
     """
     Retrieve the list of datastreams.
     """
     with get_connection() as conn, conn.cursor() as cursor:
         object_info = object_reader.object_info_from_raw(
             pid, cursor=cursor).fetchone()
         try:
             object_id = object_info['id']
         except TypeError as e:
             raise ObjectDoesNotExistError(pid) from e
         raw_datastreams = ds_reader.datastreams(object_id,
                                                 cursor=cursor).fetchall()
         datastreams = []
         for datastream in raw_datastreams:
             mime = ds_reader.mime_from_resource(datastream['resource'],
                                                 cursor=cursor).fetchone()
             datastreams.append({
                 'dsid':
                 datastream['dsid'],
                 'label':
                 datastream['label'],
                 'mimeType':
                 mime['mime'] if mime is not None else '',
             })
         return datastreams
Example #2
0
def populate_foxml_etree(foxml,
                         pid,
                         base_url='http://localhost:8080/fedora',
                         archival=False,
                         inline_to_managed=False,
                         cursor=None):
    """
    Add FOXML from a PID into an lxml etree.

    Raises:
        ObjectDoesNotExistError: The object doesn't exist.
    """
    attributes = {
        'VERSION': '1.1',
        'PID': pid,
        '{{{}}}schemaLocation'.format(SCHEMA_NAMESPACE): SCHEMA_LOCATION
    }

    with foxml.element('{{{0}}}digitalObject'.format(FOXML_NAMESPACE),
                       **attributes):
        cursor = object_info_from_raw(pid, cursor=cursor)
        object_info = cursor.fetchone()
        if object_info is None:
            raise ObjectDoesNotExistError(pid)
        populate_foxml_properties(foxml, object_info, cursor=cursor)
        populate_foxml_datastreams(foxml, pid, object_info, base_url, archival,
                                   inline_to_managed, cursor)
Example #3
0
    def _update_object(self, req, pid):
        """
        Commit the object modification.
        """
        with get_connection() as conn, conn.cursor() as cursor:
            # Get current object info.
            object_info = object_reader.object_info_from_raw(
                pid, cursor=cursor).fetchone()
            if not object_info:
                raise ObjectDoesNotExistError(pid)

            object_info = dict(object_info)

            # Check modified date param, exiting if needed.
            modified_date = req.get_param('lastModifiedDate')
            if modified_date is not None:
                modified_date = utils.iso8601_to_datetime(modified_date)
                if object_info['modified'] > modified_date:
                    raise ObjectConflictsError(pid, object_info['modified'],
                                               modified_date)

            # Create old version of object.
            if object_info['versioned']:
                old_object_info = object_info
                old_object_info['committed'] = object_info['modified']
                old_object_info['object'] = object_info['id']
                del old_object_info['id']
                object_writer.upsert_old_object(old_object_info, cursor=cursor)
                cursor.fetchone()

            # Update object info.
            new_object_info = object_info
            new_object_info['label'] = req.get_param(
                'label', default=object_info['label'])
            new_object_info['state'] = req.get_param(
                'state', default=object_info['state'])
            if req.get_param('ownerId') is not None:
                new_object_info['owner'] = self._resolve_owner(req, cursor)
            if req.get_param('logMessage') is not None:
                new_object_info['log'] = resolve_log(req, cursor)
            del new_object_info['modified']
            object_id = object_writer.upsert_object(
                new_object_info, cursor=cursor).fetchone()['id']
            return object_reader.object_info(
                object_id, cursor=cursor).fetchone()['modified']
Example #4
0
    def _purge_object(self, req, pid):
        """
        Purge the object.

        @TODO: handle logMessage when audit is dealt with.
        """
        with get_connection() as conn, conn.cursor() as cursor:
            object_info = object_reader.object_info_from_raw(
                pid, cursor).fetchone()

            if object_info is None:
                raise ObjectDoesNotExistError(pid)
            if object_relation_reader.is_object_referenced(
                    object_info['id'], cursor):
                raise ValueError(
                    'Not purging {} as it is referenced.'.format(pid))

            object_purger.delete_object(object_info['id'], cursor)
Example #5
0
    def _get_object(self, req, pid):
        """
        Generate the object profile XML.

        This does not respect asOfDateTime from Fedora.
        """
        with get_connection() as conn, conn.cursor() as cursor:
            # Get object info.
            object_info = object_reader.object_info_from_raw(
                pid, cursor=cursor).fetchone()
            if object_info is None:
                raise ObjectDoesNotExistError(pid)
            object_relation_reader.read_relationship(
                relations.FEDORA_MODEL_NAMESPACE,
                relations.HAS_MODEL_PREDICATE,
                object_info['id'],
                cursor=cursor)
            model_info = cursor.fetchall()
            models = set()
            for rdf_object_info in model_info:
                cursor = object_reader.object_info(
                    rdf_object_info['rdf_object'], cursor=cursor)
                model_object_info = cursor.fetchone()

                object_reader.namespace_info(model_object_info['namespace'],
                                             cursor=cursor)
                namespace = cursor.fetchone()['namespace']

                model_pid = utils.make_pid(namespace,
                                           model_object_info['pid_id'])
                models.add('info:fedora/{}'.format(model_pid))
            source_reader.user(object_info['owner'], cursor=cursor)
            owner = cursor.fetchone()['name']

            return (pid, object_info['label'], models, object_info['created'],
                    object_info['modified'], object_info['state'], owner)
def _rdf_object_from_element(predicate, relation, source, cursor):
    """
    Pull out an RDF object form an RDF XML element.

    Returns:
        A tuple of:
        - the resolved RDF object
        - the type; one of:
            - OBJECT_RDF_OBJECT
            - DATASTREAM_RDF_OBJECT
            - USER_RDF_OBJECT
            - ROLE_RDF_OBJECT

    Raises:
        ReferencedObjectDoesNotExistError: If the value appeared to
            reference a repo object, but it could not be found.
        ValueError: If the value could not be resolved in general.
    """
    user_tags = frozenset([
        (relations.ISLANDORA_RELS_EXT_NAMESPACE,
         relations.IS_VIEWABLE_BY_USER_PREDICATE),
        (relations.ISLANDORA_RELS_INT_NAMESPACE,
         relations.IS_VIEWABLE_BY_USER_PREDICATE),
        (relations.ISLANDORA_RELS_EXT_NAMESPACE,
         relations.IS_MANAGEABLE_BY_USER_PREDICATE),
        (relations.ISLANDORA_RELS_INT_NAMESPACE,
         relations.IS_MANAGEABLE_BY_USER_PREDICATE),
    ])
    role_tags = frozenset([
        (relations.ISLANDORA_RELS_EXT_NAMESPACE,
         relations.IS_VIEWABLE_BY_ROLE_PREDICATE),
        (relations.ISLANDORA_RELS_INT_NAMESPACE,
         relations.IS_VIEWABLE_BY_ROLE_PREDICATE),
        (relations.ISLANDORA_RELS_EXT_NAMESPACE,
         relations.IS_MANAGEABLE_BY_ROLE_PREDICATE),
        (relations.ISLANDORA_RELS_INT_NAMESPACE,
         relations.IS_MANAGEABLE_BY_ROLE_PREDICATE),
    ])
    if relation.text:
        if predicate in user_tags:
            cursor = source_writer.upsert_user({'name': relation.text,
                                                'source': source},
                                               cursor=cursor)
            return (cursor.fetchone()['id'], USER_RDF_OBJECT)
        elif predicate in role_tags:
            cursor = source_writer.upsert_role({'role': relation.text,
                                                'source': source},
                                               cursor=cursor)
            return (cursor.fetchone()['id'], ROLE_RDF_OBJECT)
        else:
            logger.debug(('No dereferencing performed for relationship %s for '
                          'value %s.'), predicate, relation.text)
            return (relation.text, RAW_RDF_OBJECT)
    else:
        resource = relation.attrib['{{{}}}resource'.format(RDF_NAMESPACE)]

        pid = pid_from_fedora_uri(resource)
        dsid = dsid_from_fedora_uri(resource)
        if pid:
            cursor = object_reader.object_info_from_raw(pid, cursor=cursor)
            try:
                object_id = cursor.fetchone()['id']
            except TypeError as e:
                logger.error('Referenced object %s does not exist.', pid)
                raise ReferencedObjectDoesNotExistError(pid) from e
            else:
                if dsid:
                    try:
                        cursor = datastream_reader.datastream_id(
                            {'object_id': object_id, 'dsid': dsid},
                            cursor=cursor
                        )
                        return (cursor.fetchone()['id'], DATASTREAM_RDF_OBJECT)
                    except TypeError as e:
                        logger.error(
                            'Referenced datastream %s/%s does not exist.',
                            pid,
                            dsid
                        )
                        raise ReferencedDatastreamDoesNotExist(pid,
                                                               dsid) from e

                return (object_id, OBJECT_RDF_OBJECT)

        raise ValueError('Failed to resolve relationship %s with value %s.',
                         predicate, resource)