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
Example #2
0
def delete_datastream_versions(pid, dsid, start=None, end=None, cursor=None):
    """
    Delete versions of a datastream
    """
    cursor = check_cursor(cursor)

    datastream_reader.datastream_from_raw(pid, dsid, cursor=cursor)
    ds_info = cursor.fetchone()
    if ds_info is None:
        return cursor

    # Handle base datastream.
    if end is None and start is None:
        return delete_datastream(ds_info['id'], cursor=cursor)
    elif end is None or end >= ds_info['modified']:
        # Find youngest surviving version and make it current.
        ds_replacement = datastream_reader.datastream_as_of_time(
            ds_info['id'], start, cursor, False)
        if ds_replacement is not None:
            datastream_writer.upsert_datastream(dict(ds_replacement),
                                                cursor=cursor)
            cursor.execute(
                '''
                DELETE FROM old_datastreams
                WHERE datastream = %s AND committed = %s
            ''', (ds_info['id'], ds_replacement['modified']))

    # Handle old datastreams.
    if start is None:
        # Remove from dawn of time to specified end.
        cursor.execute(
            '''
            DELETE FROM old_datastreams
            WHERE datastream = %s AND committed <= %s
        ''', (ds_info['id'], end))
    elif end is None:
        # Remove from specified start to end of time.
        cursor.execute(
            '''
            DELETE FROM old_datastreams
            WHERE datastream = %s AND committed >= %s
        ''', (ds_info['id'], start))
    else:
        # Remove items between specified start and end times.
        cursor.execute(
            '''
            DELETE FROM old_datastreams
            WHERE datastream = %s AND committed <= %s AND
                committed >= %s
        ''', (ds_info['id'], end, start))

    logger.debug('Deleted datastream versions for %s on %s between %s and %s.',
                 dsid, pid, start, end)

    return cursor
Example #3
0
    def _get_datastream_versions(self, pid, dsid):
        """
        Get an iterable of datastream versions.
        """
        with get_connection() as conn, conn.cursor() as cursor:
            ds_info = ds_reader.datastream_from_raw(pid, dsid,
                                                    cursor=cursor).fetchone()
            if ds_info is None:
                raise DatastreamDoesNotExistError(pid, dsid)

            datastream_versions = []
            old_dss = ds_reader.old_datastreams(ds_info['id'],
                                                cursor=cursor).fetchall()
            version = 0
            temp_ds = ds_info.copy()
            # Not using enumerate as we use the version var outside the loop.
            for old_ds in old_dss:
                temp_ds.update(old_ds)
                temp_ds['modified'] = old_ds['committed']
                datastream_versions.append(
                    fedora_utils.datastream_to_profile(temp_ds,
                                                       cursor,
                                                       version=version))
                version += 1
            datastream_versions.append(
                fedora_utils.datastream_to_profile(ds_info,
                                                   cursor,
                                                   version=version))

            return reversed(datastream_versions)
 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)
    def _create_datastream(self, req, pid, dsid):
        """
        Persist the new datastream.

        Raises:
            DatastreamExistsError: The object doesn't exist.
        """
        conn = get_connection(ISOLATION_LEVEL_READ_COMMITTED)
        with conn, conn.cursor() as cursor:
            ds_info = ds_reader.datastream_from_raw(pid, dsid,
                                                    cursor=cursor).fetchone()
            if ds_info:
                raise DatastreamExistsError(pid, dsid)
            self._upsert_ds(req, pid, dsid, cursor)
Example #6
0
    def _get_info(self, pid, dsid):
        """
        Get the MIME-type and URI of the given datastream.

        Returns:
            A three-tuple comprising:
            - the datastream control group
            - the URI of the resource the datastream represents
            - the MIME type of the datastream's resource
        Raises:
            DatastreamDoesNotExistError: The datastream doesn't exist.
        """
        with get_connection() as conn, conn.cursor() as cursor:
            datastream_info = ds_reader.datastream_from_raw(
                pid, dsid, cursor=cursor).fetchone()
            if datastream_info is None:
                raise DatastreamDoesNotExistError(pid, dsid)
            resource_info = ds_reader.resource(datastream_info['resource'],
                                               cursor=cursor).fetchone()
            mime_info = ds_reader.mime(resource_info['mime'],
                                       cursor=cursor).fetchone()

            return (datastream_info['control_group'], resource_info['uri'],
                    mime_info['mime'])