예제 #1
0
def test_records_type_helpers(app, test_records_data):
    """Test record util functions retrieving the record type."""
    with app.app_context():
        creator = create_user('creator')
        deposit = create_deposit(test_records_data[0], creator)
        deposit.submit()
        deposit.publish()
        _, record = deposit.fetch_published()
        assert is_deposit(deposit.model)
        assert not is_deposit(record.model)
        assert is_publication(record.model)
        assert not is_publication(deposit.model)
예제 #2
0
def test_records_type_helpers(app, test_records_data):
    """Test record util functions retrieving the record type."""
    with app.app_context():
        creator = create_user('creator')
        deposit = create_deposit(test_records_data[0], creator)
        deposit.submit()
        deposit.publish()
        _, record = deposit.fetch_published()
        assert is_deposit(deposit.model)
        assert not is_deposit(record.model)
        assert is_publication(record.model)
        assert not is_publication(deposit.model)
예제 #3
0
def skip_deposit(doc):
    """Check if event is coming from deposit file and skip."""
    rb = RecordsBuckets.query.filter_by(bucket_id=doc['bucket_id']).first()
    record = RecordMetadata.query.filter_by(id=rb.record_id).first()
    if is_deposit(record):
        return None
    return doc
예제 #4
0
def files_permission_factory(obj, action=None):
    """Permission for files are always based on the type of record.

    Record bucket: Read access only with open access.
    Deposit bucket: Read/update with restricted access.
    """
    # Extract bucket id
    bucket_id = None
    if isinstance(obj, Bucket):
        bucket_id = str(obj.id)
    elif isinstance(obj, ObjectVersion):
        bucket_id = str(obj.bucket_id)
    elif isinstance(obj, MultipartObject):
        bucket_id = str(obj.bucket_id)
    elif isinstance(obj, FileObject):
        bucket_id = str(obj.bucket_id)

    # Retrieve record
    if bucket_id is not None:
        # Record or deposit bucket
        rb = RecordsBuckets.query.filter_by(bucket_id=bucket_id).one_or_none()
        if rb is not None:
            record = Record.get_record(rb.record_id)
            if is_publication(record.model):
                return PublicationFilesPermission(record, action)
            elif is_deposit(record.model):
                return DepositFilesPermission(record, action)

    return DynamicPermission(superuser_access)
예제 #5
0
def files_permission_factory(obj, action=None):
    """Permission for files are always based on the type of record.

    Record bucket: Read access only with open access.
    Deposit bucket: Read/update with restricted access.
    """
    # Extract bucket id
    bucket_id = None
    if isinstance(obj, Bucket):
        bucket_id = str(obj.id)
    elif isinstance(obj, ObjectVersion):
        bucket_id = str(obj.bucket_id)
    elif isinstance(obj, MultipartObject):
        bucket_id = str(obj.bucket_id)
    elif isinstance(obj, FileObject):
        bucket_id = str(obj.bucket_id)

    # Retrieve record
    if bucket_id is not None:
        # Record or deposit bucket
        rb = RecordsBuckets.query.filter_by(bucket_id=bucket_id).one_or_none()
        if rb is not None:
            record = Record.get_record(rb.record_id)
            if is_publication(record.model):
                return PublicationFilesPermission(record, action)
            elif is_deposit(record.model):
                return DepositFilesPermission(record, action)

    return DynamicPermission(superuser_access)
예제 #6
0
def skip_deposit(doc):
    """Check if event is coming from deposit file and skip."""
    rb = RecordsBuckets.query.filter_by(bucket_id=doc['bucket_id']).first()
    record = RecordMetadata.query.filter_by(id=rb.record_id).first()
    if is_deposit(record):
        return None
    return doc
예제 #7
0
    def filter_internal(self, data):
        """Remove internal fields from the record metadata."""
        external_pids = []
        bucket = None
        record = None
        # differentiating between search results and
        # single record requests
        if hasattr(g, 'record'):
            record = g.record
            if record.files:
                bucket = record.files.bucket
            if is_deposit(record.model):
                external_pids = generate_external_pids(record)
            # if it is a published record don't generate external pids
            # as they are immutable and stored in _deposit
            else:
                external_pids = record.model.json[
                    '_deposit'].get('external_pids')
            user_has_permission = \
                allow_public_file_metadata(data['metadata']) if bucket \
                is None else files_permission_factory(
                    bucket, 'bucket-read').can()
        elif hasattr(g, 'record_hit'):
            user_has_permission = allow_public_file_metadata(
                g.record_hit['_source'])

        if '_deposit' in data['metadata']:
            data['metadata']['owners'] = data['metadata']['_deposit']['owners']

            # Add the external_pids only if the
            # user is allowed to read the bucket
            if external_pids and bucket and user_has_permission:
                data['metadata']['external_pids'] = external_pids
            del data['metadata']['_deposit']
        if '_files' in data['metadata']:
            # Also add the files field only if the user is allowed
            if user_has_permission:
                data['files'] = data['metadata']['_files']
                if external_pids and bucket:
                    external_dict = {x['key']: x['ePIC_PID']
                                     for x in external_pids}
                    for _file in data['files']:
                        if _file['key'] in external_dict:
                            _file['b2safe'] = True
                            _file['ePIC_PID'] = external_dict[_file['key']]
            del data['metadata']['_files']
        if '_pid' in data['metadata']:
            # move PIDs to metadata top level
            epic_pids = [p for p in data['metadata']['_pid']
                         if p.get('type') == 'ePIC_PID']
            dois = [p for p in data['metadata']['_pid']
                    if p.get('type') == 'DOI']
            if len(epic_pids) > 0:
                data['metadata']['ePIC_PID'] = epic_pids[0].get('value')
            if len(dois) > 0:
                data['metadata']['DOI'] = DOI_URL_PREFIX + dois[0].get('value')

            # add parent version pid
            # data['metadata']['parent_id'] = next(
            #     pid['value'] for pid in data['metadata']['_pid']
            #     if pid['type'] == RecordUUIDProvider.parent_pid_type
            # )
            del data['metadata']['_pid']
        if '_oai' in data['metadata']:
            del data['metadata']['_oai']
        if '_internal' in data['metadata']:
            del data['metadata']['_internal']
        return data
예제 #8
0
파일: json.py 프로젝트: jupste/b2share
    def filter_internal(self, data):
        """Remove internal fields from the record metadata."""
        external_pids = []
        bucket = None
        record = None
        # differentiating between search results and
        # single record requests
        if hasattr(g, 'record'):
            record = g.record
            if record.files:
                bucket = record.files.bucket
            if is_deposit(record.model):
                external_pids = generate_external_pids(record)
            # if it is a published record don't generate external pids
            # as they are immutable and stored in _deposit
            else:
                external_pids = record.model.json[
                    '_deposit'].get('external_pids')
            user_has_permission = \
                allow_public_file_metadata(data['metadata']) if bucket \
                is None else files_permission_factory(
                    bucket, 'bucket-read').can()
        elif hasattr(g, 'record_hit'):
            user_has_permission = allow_public_file_metadata(
                g.record_hit['_source'])

        if '_deposit' in data['metadata']:
            if hasattr(g, 'record') and is_deposit(record.model) and current_app.config.get('AUTOMATICALLY_ASSIGN_DOI', False):
                # add future DOI string
                data['metadata'].update({'$future_doi': generate_doi(data['metadata']['_deposit']['id']) })

            data['metadata']['owners'] = data['metadata']['_deposit']['owners']

            # Add the external_pids only if the
            # user is allowed to read the bucket
            if external_pids and bucket and user_has_permission:
                data['metadata']['external_pids'] = external_pids
            del data['metadata']['_deposit']
        if '_files' in data['metadata']:
            # Also add the files field only if the user is allowed
            if user_has_permission:
                data['files'] = data['metadata']['_files']
                if external_pids and bucket:
                    external_dict = {x['key']: x['ePIC_PID']
                                     for x in external_pids}
                    for _file in data['files']:
                        if _file['key'] in external_dict:
                            _file['b2safe'] = True
                            _file['ePIC_PID'] = external_dict[_file['key']]
            del data['metadata']['_files']
        if '_pid' in data['metadata']:
            # move PIDs to metadata top level
            epic_pids = [p for p in data['metadata']['_pid']
                         if p.get('type') == 'ePIC_PID']
            dois = [p for p in data['metadata']['_pid']
                    if p.get('type') == 'DOI']
            if len(epic_pids) > 0:
                data['metadata']['ePIC_PID'] = epic_pids[0].get('value')
            if len(dois) > 0:
                data['metadata']['DOI'] = DOI_URL_PREFIX + dois[0].get('value')

            # add parent version pid
            # data['metadata']['parent_id'] = next(
            #     pid['value'] for pid in data['metadata']['_pid']
            #     if pid['type'] == RecordUUIDProvider.parent_pid_type
            # )
            del data['metadata']['_pid']
        if '_oai' in data['metadata']:
            del data['metadata']['_oai']
        if '_internal' in data['metadata']:
            del data['metadata']['_internal']
        return data