def _delete_datastream(self, req, pid, dsid):
        """
        Purge the datastream (or range of versions).

        @TODO: handle logMessage when audit is dealt with.
        """
        start = utils.iso8601_to_datetime(req.get_param('startDT'))
        end = utils.iso8601_to_datetime(req.get_param('endDT'))
        with get_connection() as conn, conn.cursor() as cursor:
            ds_purger.delete_datastream_versions(pid,
                                                 dsid,
                                                 start=start,
                                                 end=end,
                                                 cursor=cursor)
            if not cursor.rowcount:
                object_info = object_reader.object_id_from_raw(
                    pid, cursor=cursor).fetchone()
                if object_info is None:
                    # Only raise if the object is missing because Fedora.
                    raise ObjectDoesNotExistError(pid)

            foxml.internalize_rels(pid,
                                   dsid,
                                   req.env['wsgi.identity'].source_id,
                                   cursor=cursor)
        return (start, end)
    def _update_datastream(self, req, pid, dsid):
        """
        Commit the modification to the datastream.
        """
        conn = get_connection(ISOLATION_LEVEL_READ_COMMITTED)
        with conn, conn.cursor() as cursor:
            ds_reader.datastream_from_raw(pid, dsid, cursor=cursor)
            ds_info = cursor.fetchone()
            if ds_info is None:
                raise DatastreamDoesNotExistError(pid, dsid)
            ds = dict(ds_info)
            ds['committed'] = ds['modified']
            ds['datastream'] = ds['id']
            del ds['id']
            # 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 ds['committed'] > modified_date:
                    raise DatastreamConflictsError(pid, dsid, ds['committed'],
                                                   modified_date)
            if ds_info['versioned']:
                ds_writer.upsert_old_datastream(ds, cursor=cursor)

            if ds['resource'] is not None:
                ds['mimetype'] = ds_reader.mime_from_resource(
                    ds['resource'], cursor=cursor).fetchone()['mime']
            self._upsert_ds(req, pid, dsid, cursor, ds=ds)
        return
 def _get_datastream_info(self, pid, dsid, asOfDateTime=None, **kwargs):
     """
     Get the ds* values in a dict, to build the datastream profile.
     """
     with get_connection() as conn, conn.cursor() as cursor:
         ds_reader.datastream_from_raw(pid, dsid, cursor=cursor)
         ds_info = cursor.fetchone()
         if ds_info is None:
             raise DatastreamDoesNotExistError(pid, dsid)
         if asOfDateTime is not None:
             time = utils.iso8601_to_datetime(asOfDateTime)
             ds_info = ds_reader.datastream_as_of_time(ds_info['id'],
                                                       time,
                                                       cursor=cursor)
             if ds_info is None:
                 raise DatastreamDoesNotExistError(pid, dsid, time)
         return fedora_utils.datastream_to_profile(ds_info, cursor)
示例#4
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']
示例#5
0
    def _get_ds_dissemination(self, req, pid, dsid):
        """
        Provide datastream content.
        """
        with get_connection() as conn, conn.cursor() as cursor:
            object_info = object_reader.object_id_from_raw(
                pid, cursor=cursor).fetchone()
            if object_info is None:
                raise ObjectDoesNotExistError(pid)

            time = utils.iso8601_to_datetime(req.get_param('asOfDateTime'))
            ds_info = ds_reader.datastream(
                {
                    'object': object_info['id'],
                    'dsid': dsid
                }, cursor=cursor).fetchone()
            if ds_info is None:
                raise DatastreamDoesNotExistError(pid, dsid)
            if time is not None:
                ds_info = ds_reader.datastream_as_of_time(
                    ds_info['id'], time, cursor)
                if ds_info is None:
                    raise DatastreamDoesNotExistError(pid, dsid, time)

            resource_info = ds_reader.resource(ds_info['resource']).fetchone()
            if resource_info is None:
                return None

            info = {}
            mime_info = ds_reader.mime_from_resource(resource_info['id'],
                                                     cursor=cursor).fetchone()
            if mime_info:
                info['mime'] = mime_info['mime']
            # Redirect if we are a redirect DS.
            if ds_info['control_group'] == 'R':
                info['location'] = resource_info['uri']
            else:
                # Send data if we are not a redirect DS.
                file_path = filestore.resolve_uri(resource_info['uri'])
                info['stream'] = open(file_path, 'rb')

            return info