def _create_deposit(user, schema_name, metadata=None, experiment=None, publish=False):
            """
            Create a new deposit for given user and schema name
            e.g cms-analysis-v0.0.1,
            with minimal metadata defined for this schema type.
            """
            with app.test_request_context():
                # create schema for record
                create_schema('records/{}'.format(schema_name), is_deposit=False,
                              experiment='CMS')

                # create schema for deposit
                schema = create_schema('deposits/records/{}'.format(schema_name), 
                                       experiment=experiment)
                metadata = metadata or minimal_metadata(jsonschemas_host,
                                                        'deposits/records/{}'.format(schema_name))
                login_user(user)
                id_ = uuid4()
                deposit_minter(id_, metadata)
                deposit = Deposit.create(metadata, id_=id_)

            if publish:
                deposit.publish()
                _, record = deposit.fetch_published()
                RecordIndexer().index(record)

                current_search.flush_and_refresh('records')

            current_search.flush_and_refresh(schema.index_name)

            return Deposit.get_record(deposit.id)
Exemple #2
0
def test_jsonschemas_with_additional_properties_fails(app, db, location):
    with app.test_request_context():
        metadata = minimal_deposits_metadata('test-schema-v0.0.1')
        id_ = uuid4()
        deposit_minter(id_, metadata)
        with pytest.raises(ValidationError):
            Deposit.create(metadata, id_=id_)
def synchronize_cadi_entries(limit=None):
    """Add/update all CADI entries connecting with CADI database."""

    entries = get_entries_from_cadi_db()
    for entry in entries[0:limit]:
        # remove artefact from code names
        cadi_id = re.sub('^d', '', entry.get('code', None))

        try:  # update if already exists
            parser = HTMLParser()
            uuid = get_entry_uuid_by_unique_field('deposits-records-cms-analysis-v0.0.1',
                                                  {'basic_info__cadi_id': cadi_id})

            deposit = CAPDeposit.get_record(uuid)

            if 'cadi_info' not in deposit:
                deposit['cadi_info'] = {}
            for cadi_key, cap_key in CADI_FIELD_TO_CAP_MAP.items():
                # sometimes they store data in HTML format.. need to escape chars
                deposit['cadi_info'][cap_key] = parser.unescape(entry.get(cadi_key, '')) or ''
            deposit.commit()

            print('Cadi entry {} updated.'.format(cadi_id))

        except DepositDoesNotExist:  # or create new cadi entry
            data = construct_cadi_entry(cadi_id, {
                'cadi_info': {v: parser.unescape(entry.get(k, '')) or ''
                              for k, v in CADI_FIELD_TO_CAP_MAP.items()}
            })

            deposit = CAPDeposit.create(data=data)
            add_read_permission_for_egroup(deposit, '*****@*****.**')

            print('Cadi entry {} added.'.format(cadi_id))
Exemple #4
0
def test_create_deposit_with_non_object_data_returns_wrong_schema_error(
        app, users, location, jsonschemas_host):
    with app.test_request_context():
        metadata = 5
        login_user(users['superuser'])
        id_ = uuid4()
        with raises(EmptyDepositError):
            Deposit.create(metadata, id_=id_)
Exemple #5
0
def test_create_deposit_with_empty_schema_returns_wrong_schema_error(
        app, users, location, jsonschemas_host):
    with app.test_request_context():
        metadata = {'$schema': ''}
        login_user(users['superuser'])
        id_ = uuid4()
        with raises(WrongJSONSchemaError):
            Deposit.create(metadata, id_=id_)
def test_create_deposit_with_empty_schema_raises_DepositValidationError(
        app, users, location, jsonschemas_host):
    metadata = {'$schema': ''}

    with app.test_request_context():
        login_user(users['superuser'])
        id_ = uuid4()

        with raises(DepositValidationError):
            Deposit.create(metadata, id_=id_)
Exemple #7
0
def test_create_deposit_with_wrong_schema_returns_wrong_schema_error(
        app, users, location, jsonschemas_host):
    with app.test_request_context():
        metadata = {
            '$schema':
            'https://{}/schemas/deposits/records/lhcb-wrong.json'.format(
                jsonschemas_host)
        }
        login_user(users['superuser'])
        id_ = uuid4()
        with raises(WrongJSONSchemaError):
            Deposit.create(metadata, id_=id_)
Exemple #8
0
def test_create_deposit_with_wrong_schema_raises_DepositValidationError(
    app, users, location):
    metadata = {
        '$schema': 'https://analysispreservation.cern.ch/schemas/deposits/records/non-existing-schema.json'  # noqa
    }

    with app.test_request_context():
        login_user(users['superuser'])
        id_ = uuid4()

        with raises(DepositValidationError):
            Deposit.create(metadata, id_=id_)
def test_create_deposit_with_empty_schema_raises_DepositValidationError(app,
                                                                        users,
                                                                        location,
                                                                        jsonschemas_host):
    metadata = {'$schema': ''}

    with app.test_request_context():
        login_user(users['superuser'])
        id_ = uuid4()

        with raises(DepositValidationError):
            Deposit.create(metadata, id_=id_)
Exemple #10
0
def test_create_deposit_atlas_analysis_index(db, es, location, deposit_index):
    """Test if deposit atlas analysis index is created."""
    deposit_index_name = deposit_index + '-' + 'atlas-analysis-v0.0.1'

    Deposit.create({
        '$schema':
        'https://analysispreservation.cern.ch/schemas/deposits/records/atlas-analysis-v0.0.1.json'
    })
    db.session.commit()
    current_search.flush_and_refresh(deposit_index_name)
    res = current_search.client.search(index=deposit_index_name)
    assert 'id' in res['hits']['hits'][0]['_source']['_deposit']
Exemple #11
0
def test_create_deposit_with_wrong_data_returns_validation_error(
        app, users, location, jsonschemas_host):
    with app.test_request_context():
        metadata = {
            '$schema':
            'https://{}/schemas/deposits/records/lhcb-v0.0.1.json'.format(
                jsonschemas_host),
            'general_title': ['I am an array, not a string']
        }
        login_user(users['superuser'])
        id_ = uuid4()
        with raises(ValidationError):
            Deposit.create(metadata, id_=id_)
def test_create_deposit_with_wrong_schema_raises_DepositValidationError(
        app, users, location, jsonschemas_host):
    metadata = {
        '$schema':
        'https://{}/schemas/deposits/records/non-existing-schema.json'.format(
            jsonschemas_host)
    }

    with app.test_request_context():
        login_user(users['superuser'])
        id_ = uuid4()

        with raises(DepositValidationError):
            Deposit.create(metadata, id_=id_)
def test_create_deposit_with_wrong_schema_raises_DepositValidationError(app,
                                                                        users,
                                                                        location,
                                                                        jsonschemas_host):
    metadata = {
        '$schema': 'https://{}/schemas/deposits/records/non-existing-schema.json'.format(
            jsonschemas_host)
    }

    with app.test_request_context():
        login_user(users['superuser'])
        id_ = uuid4()

        with raises(DepositValidationError):
            Deposit.create(metadata, id_=id_)
def add_drafts_from_file(file_path, schema,
                         egroup=None, user=None, limit=None):
    """Adds drafts from a specified file.

    Drafts with specified pid will be registered under those.
    For drafts without pid, new pids will be minted.
    """
    with open(file_path, 'r') as fp:
        entries = json.load(fp)

        for entry in entries[0:limit]:
            data = construct_draft_obj(schema, entry)
            pid = cap_deposit_fetcher(None, data)
            pid_value = pid.pid_value if pid else None

            try:
                PersistentIdentifier.get('depid', pid_value)

                print('Draft with id {} already exist!'.format(pid_value))

            except PIDDoesNotExistError:
                record_uuid = uuid.uuid4()
                pid = cap_deposit_minter(record_uuid, data)
                if user:
                    user = User.query.filter_by(email=user).one()
                if egroup:
                    role = Role.query.filter_by(name=egroup).one()
                deposit = CAPDeposit.create(data, record_uuid, user)
                deposit.commit()

                if egroup:
                    add_read_permission_for_egroup(deposit, egroup)

                print('Draft {} added.'.format(pid.pid_value))
Exemple #15
0
def download_repo(record_id,
                  filename,
                  download_url,
                  auth_headers=None,
                  from_snapshot_id=None):
    """Download a repository as a .tar file under record files."""
    response = requests.get(download_url, stream=True, headers=auth_headers)

    # retrieve the data with the correct content length
    if 'Content-Length' not in response.headers:
        response = ensure_content_length(response)

    size = int(response.headers.get('Content-Length'))
    response = response.raw

    from cap.modules.deposit.api import CAPDeposit
    record = CAPDeposit.get_record(record_id)

    # temporary workaround, we save empty file, when downloading failed
    # so we can show it in UI (with tag FAILED)
    failed = True if response.status != 200 else False
    if failed:
        print(f'Downloading content from {download_url}'
              f'failed ({response.status}).')

    obj_ver = record.save_file(response, filename, size, failed)

    if from_snapshot_id:
        obj_ver.snapshot_id = from_snapshot_id
        db.session.commit()
Exemple #16
0
def download_repo(record_id, host, owner, repo, branch, source_url,
                  download_url):
    """Download a repository as a .tar file under record files."""
    response = requests.get(download_url, stream=True)
    filename = 'repositories/{}/{}/{}/{}.tar.gz'.format(
        host, owner, repo, branch)

    # retrieve the data with the correct content length
    if 'Content-Length' not in response.headers:
        response = ensure_content_length(response)

    size = int(response.headers.get('Content-Length'))
    response = response.raw

    from cap.modules.deposit.api import CAPDeposit
    record = CAPDeposit.get_record(record_id)

    # temporary workaround, we save empty file, when downloading failed
    # so we can show it in UI (with tag FAILED)
    failed = True if response.status != 200 else False
    if failed:
        print('Downloading content from {} failed ({}).'.format(
            download_url, response.status))

    record.save_file(response, filename, size, source_url, failed)
def questionnaires(file):
    """Load and save CMS questionnaire data."""
    answers = extract_questionnaires_from_excel(file)
    click.secho(f"Total Answers: {len(answers)}", fg='green')

    for answer in answers:
        deposit = None
        with db.session.begin_nested():
            try:
                title = f"Statistics Questionnaire for " \
                        f"{answer['analysis_context']['cadi_id']}" \
                            if answer['analysis_context']['cadi_id'] \
                            else "--"
                extracted = remove_none_keys(
                    _questionnaire_data(answer, title=title))
                if title:
                    extracted["general_title"] = title
                deposit = CAPDeposit.create(data=extracted, owner=None)

                # give read access to members of CMS experiment
                deposit._add_experiment_permissions(
                    'CMS',
                    ['deposit-read'],
                )

                # give permission to stat committee
                admin_egroups = ["*****@*****.**"]
                for role in admin_egroups:
                    _role = get_existing_or_register_role(role)
                    deposit._add_egroup_permissions(
                        _role,
                        [
                            'deposit-read',
                            'deposit-update',
                            'deposit-admin',
                        ],
                        db.session,
                    )

                deposit.commit()
                click.secho(
                    f"Success: {answer['_general_info']['serial']} - "
                    f"{answer['_general_info']['user']}",
                    fg='green')
                click.secho(f"{title}", fg='yellow')

            except DepositValidationError as e:
                click.secho("---------------")
                click.secho(f"Validation Error", fg='red')
                for err in e.errors:
                    click.secho(f"{err.to_dict()}", fg='red')
                click.secho("---------------")
                pass
            except ValidationError:
                click.secho("---------------")
                click.secho(f"Validation Error", fg='red')
                click.secho("---------------")
                pass

        db.session.commit()
Exemple #18
0
def add_drafts_from_file(file_path, schema, egroup, limit=None):
    with open(file_path, 'r') as fp:
        entries = json.load(fp)
        for entry in entries[0:limit]:
            deposit = CAPDeposit.create(construct_draft_obj(schema, entry))
            add_read_permission_for_egroup(deposit, egroup)

            print('Draft {} added.'.format(deposit.id))
def download_url(record_id, url):
    """Create new file object and assign it to object version."""
    record = CAPDeposit.get_record(record_id)
    record.files[url].file.set_contents(
        requests.get(url, stream=True).raw,
        default_location=record.files.bucket.location.uri,
    )
    db.session.commit()
Exemple #20
0
    def get_record_schemas(self, obj):
        deposit = CAPDeposit.get_record(obj['pid'].object_uuid)

        schema = current_jsonschemas.get_schema(deposit.schema.record_path,
                                                with_refs=True,
                                                resolved=True)
        uiSchema = deposit.schema.record_options

        return dict(schema=copy.deepcopy(schema), uiSchema=uiSchema)
Exemple #21
0
    def _create_deposit(user,
                        schema_name,
                        metadata=None,
                        experiment=None,
                        files={},
                        publish=False,
                        mapping=None):
        """Create a new deposit for given user and schema name.

        e.g cms-analysis-v0.0.1,
        with minimal metadata defined for this schema type.
        """
        # create schema for record
        with app.test_request_context():
            schema = create_schema(schema_name,
                                   experiment=experiment,
                                   deposit_mapping=mapping)
            deposit_schema_url = current_jsonschemas.path_to_url(
                schema.deposit_path)

            metadata = metadata or minimal_metadata(deposit_schema_url)
            login_user(user)
            id_ = uuid4()
            deposit_minter(id_, metadata)
            deposit = Deposit.create(metadata, id_=id_)

            for k, v in files.items():
                deposit.files[k] = v
            if files:
                deposit.commit()

            db.session.commit()

        if publish:
            deposit.publish()
            _, record = deposit.fetch_published()
            RecordIndexer().index(record)

            current_search.flush_and_refresh(schema.record_index)

        current_search.flush_and_refresh(schema.deposit_index)

        return Deposit.get_record(deposit.id)
def synchronize_cadi_entries(limit=None):
    """Synchronize CMS analysis with CADI database.

    Updates cadi_info in existing analysis.

    If analysis with given CADI id doesn't exist yet,
    new deposit will be created.
    All members of CMS will get a r access and cms-admin egroup rw access.

    :params int limit: number of entries to update
    """
    def _cadi_deposit(cadi_id, cadi_info):
        return {
            '$ana_type': 'cms-analysis',
            'cadi_info': cadi_info,
            'general_title': cadi_info.get('name') or cadi_id,
            '_fetched_from': 'cadi',
            '_user_edited': False,
            'basic_info': {
                'cadi_id': cadi_id
            }
        }

    entries = get_all_from_cadi()
    cms_admin_group = get_existing_or_register_role('*****@*****.**')

    for entry in entries[:limit]:
        cadi_info = cadi_serializer.dump(entry).data
        cadi_id = cadi_info['cadi_id']

        try:  # update if cadi deposit already exists
            deposit = get_deposit_by_cadi_id(cadi_id)

            if deposit.get('cadi_info') == cadi_info:
                print('No changes in cadi entry {}.'.format(cadi_id))

            else:
                deposit['cadi_info'] = cadi_info
                deposit.commit()
                db.session.commit()

                print('Cadi entry {} updated.'.format(cadi_id))

        except DepositDoesNotExist:
            deposit = CAPDeposit.create(data=_cadi_deposit(cadi_id, cadi_info),
                                        owner=None)
            deposit._add_experiment_permissions('CMS', ['deposit-read'])
            deposit._add_egroup_permissions(
                cms_admin_group,
                ['deposit-read', 'deposit-update', 'deposit-admin'],
                db.session)  # noqa
            deposit.commit()
            db.session.commit()

            print('Cadi entry {} added.'.format(cadi_id))
def synchronize_cadi_entries(limit=None):
    """Add/update CADI info in all cms-analysis syncing with CADI db."""
    parser = HTMLParser()
    entries = get_all_entries_from_cadi()

    def parse_field(field):
        """Escape HTML characters."""
        if isinstance(field, unicode):
            return parser.unescape(parser.unescape(field))
        else:
            return ''

    for entry in entries[:limit]:
        # remove artefact from cadi names
        cadi_id = re.sub('^d', '', entry.get('code', None))

        cadi_info = {
            cap_key: parse_field(entry.get(cadi_key, None))
            for cadi_key, cap_key in CADI_FIELD_TO_CAP_MAP.items()
        }

        try:  # update if already exists
            uuid = get_entry_uuid_by_unique_field(
                'deposits-records-cms-analysis-v0.0.1',
                {'basic_info__cadi_id__keyword': cadi_id}
            )

            deposit = CAPDeposit.get_record(uuid)
            deposit['cadi_info'] = cadi_info
            deposit.commit()

            print('Cadi entry {} updated.'.format(cadi_id))

        except (DepositDoesNotExist, NoResultFound):
            data = construct_cadi_entry(cadi_id, {
                'cadi_info': cadi_info
            })

            deposit = CAPDeposit.create(data=data)
            add_read_permission_for_egroup(deposit, '*****@*****.**')

            print('Cadi entry {} added.'.format(cadi_id))
    def preprocess_search_hit(self, pid, record_hit, links_factory=None):
        """."""
        result = super(CAPSchemaSerializer, self).preprocess_search_hit(
            pid, record_hit, links_factory=links_factory
        )
        deposit = CAPDeposit.get_record(record_hit['_id'])

        result = self._get_user_rights_to_record(deposit, result)
        result = self._transform_record_owners(result)

        return result
Exemple #25
0
    def can_user_review(self, obj):
        deposit_pid = obj.get("metadata", {}).get("_deposit", {}).get("id")

        resolver = Resolver(pid_type='depid',
                            object_type='rec',
                            getter=lambda x: x)

        _, rec_uuid = resolver.resolve(deposit_pid)
        deposit = CAPDeposit.get_record(rec_uuid)

        return (deposit.schema_is_reviewable() and
                ReviewDepositPermission(deposit).can())
    def preprocess_search_hit(self, pid, record_hit, links_factory=None):
        """."""
        result = super(CAPSchemaSerializer,
                       self).preprocess_search_hit(pid,
                                                   record_hit,
                                                   links_factory=links_factory)
        deposit = CAPDeposit.get_record(record_hit['_id'])

        result = self._get_user_rights_to_record(deposit, result)
        result = self._transform_record_owners(result)

        return result
    def preprocess_record(self, pid, record, links_factory=None, **kwargs):
        """Preprocess record serializing for record retrievals from the db.

        Call base serializer with deposit_links_factory explicitly.
        (bug in invenio doesn't pass correct one on deposit actions
        (e.g. /actions/publish)
        """
        result = super().preprocess_record(pid,
                                           record,
                                           links_factory=deposit_links_factory)

        result['deposit'] = CAPDeposit.get_record(pid.object_uuid)
        return result
def synchronize_cadi_entries(limit=None):
    """Add/update CADI info in all cms-analysis syncing with CADI db."""
    parser = HTMLParser()
    entries = get_all_entries_from_cadi()

    def parse_field(field):
        """Escape HTML characters."""
        if isinstance(field, unicode):
            return parser.unescape(parser.unescape(field))
        else:
            return ''

    for entry in entries[:limit]:
        # remove artefact from cadi names
        cadi_id = re.sub('^d', '', entry.get('code', None))

        cadi_info = {
            cap_key: parse_field(entry.get(cadi_key, None))
            for cadi_key, cap_key in CADI_FIELD_TO_CAP_MAP.items()
        }

        try:  # update if already exists
            uuid = get_entry_uuid_by_unique_field(
                'deposits-records-cms-analysis-v0.0.1',
                {'basic_info__cadi_id__keyword': cadi_id})

            deposit = CAPDeposit.get_record(uuid)
            deposit['cadi_info'] = cadi_info
            deposit.commit()

            print('Cadi entry {} updated.'.format(cadi_id))

        except (DepositDoesNotExist, NoResultFound):
            data = construct_cadi_entry(cadi_id, {'cadi_info': cadi_info})

            deposit = CAPDeposit.create(data=data)
            add_read_permission_for_egroup(deposit, '*****@*****.**')

            print('Cadi entry {} added.'.format(cadi_id))
Exemple #29
0
    def get_review(self, obj):
        depid = obj.get("metadata", {}).get("_deposit", {}).get("id")
        resolver = Resolver(pid_type='depid',
                            object_type='rec',
                            getter=lambda x: x)

        _, rec_uuid = resolver.resolve(depid)
        deposit = CAPDeposit.get_record(rec_uuid)

        if deposit.schema_is_reviewable():
            _reviews = deposit.get('_review', [])
            return ReviewSchema(many=True).dump(_reviews).data
        else:
            return None
def synchronize_cadi_entries(limit=None):
    """Add/update all CADI entries connecting with CADI database."""
    entries = get_entries_from_cadi_db()
    for entry in entries[:limit]:
        # remove artefact from code names
        cadi_id = re.sub('^d', '', entry.get('code', None))

        try:  # update if already exists
            parser = HTMLParser()
            uuid = get_entry_uuid_by_unique_field(
                'deposits-records-cms-analysis-v0.0.1',
                {'basic_info__cadi_id__keyword': cadi_id}
            )

            deposit = CAPDeposit.get_record(uuid)

            if 'cadi_info' not in deposit:
                deposit['cadi_info'] = {}
            for cadi_key, cap_key in CADI_FIELD_TO_CAP_MAP.items():
                # sometimes they store data in HTML format.. need to escape
                # chars
                deposit['cadi_info'][cap_key] = parser.unescape(
                    entry.get(cadi_key, '') or '') or ''
            deposit.commit()

            print('Cadi entry {} updated.'.format(cadi_id))

        except (DepositDoesNotExist, NoResultFound):
            data = construct_cadi_entry(cadi_id, {
                'cadi_info': {v: parser.unescape(entry.get(k, '') or '') or ''
                              for k, v in CADI_FIELD_TO_CAP_MAP.items()}
            })

            deposit = CAPDeposit.create(data=data)
            add_read_permission_for_egroup(deposit, '*****@*****.**')

            print('Cadi entry {} added.'.format(cadi_id))
Exemple #31
0
 def _create_deposit(user, schema):
     """
     Create a new deposit for given user and schema name
     e.g cms-analysis-v0.0.1,
     with minimal metadata defined for this schema type.
     """
     db_.session.begin_nested()
     with app.test_request_context():
         metadata = minimal_deposits_metadata(schema)
         login_user(user)
         id_ = uuid4()
         deposit_minter(id_, metadata)
         deposit = Deposit.create(metadata, id_=id_)
     current_search.flush_and_refresh('deposits-records-{}'.format(schema))
     return deposit
Exemple #32
0
        def _create_deposit(user, schema_name, metadata=None, experiment=None, publish=False):
            """
            Create a new deposit for given user and schema name
            e.g cms-analysis-v0.0.1,
            with minimal metadata defined for this schema type.
            """
            with app.test_request_context():
                # create schema for record
                schema = create_schema('records/{}'.format(schema_name), is_deposit=False,
                                       experiment=experiment)
                if not experiment:
                    schema.add_read_access_to_all()

                # create schema for deposit
                schema = create_schema('deposits/records/{}'.format(schema_name),
                                       experiment=experiment)
                if not experiment:
                    schema.add_read_access_to_all()

                metadata = metadata or minimal_metadata(jsonschemas_host,
                                                        'deposits/records/{}'.format(schema_name))
                login_user(user)
                id_ = uuid4()
                deposit_minter(id_, metadata)
                deposit = Deposit.create(metadata, id_=id_)

            if publish:
                deposit.publish()
                _, record = deposit.fetch_published()
                RecordIndexer().index(record)

                current_search.flush_and_refresh('records')

            current_search.flush_and_refresh(schema.index_name)

            return Deposit.get_record(deposit.id)
def get_deposit_by_cadi_id(cadi_id):
    """Return deposit with given cadi id.

    :params str cadi_id: CADI identifier

    :rtype `cap.modules.deposits.api:CAPDeposit`
    """
    rs = RecordsSearch(index='deposits-records')

    res = rs.query(Q('match', basic_info__cadi_id__keyword=cadi_id)) \
        .execute().hits.hits

    if not res:
        raise DepositDoesNotExist
    else:
        uuid = res[0]['_id']
        deposit = CAPDeposit.get_record(uuid)

    return deposit
Exemple #34
0
def get_deposit_by_cadi_id(cadi_id):
    """Return deposit with given cadi id.

    :params str cadi_id: CADI identifier

    :rtype `cap.modules.deposits.api:CAPDeposit`
    """
    rs = RecordsSearch(index='deposits-records')

    res = rs.query(Q('match', basic_info__cadi_id__keyword=cadi_id)) \
        .execute().hits.hits

    if not res:
        raise DepositDoesNotExist
    else:
        uuid = res[0]['_id']
        deposit = CAPDeposit.get_record(uuid)

    return deposit
Exemple #35
0
    def get_links_with_review(self, obj):
        deposit_pid = obj.get("metadata", {}).get("_deposit", {}).get("id")

        resolver = Resolver(pid_type='depid',
                            object_type='rec',
                            getter=lambda x: x)

        _, rec_uuid = resolver.resolve(deposit_pid)
        deposit = CAPDeposit.get_record(rec_uuid)
        links = obj['links']

        if (deposit.schema_is_reviewable() and
                ReviewDepositPermission(deposit).can()):
            links['review'] = clean_api_url_for(
                'invenio_deposit_rest.depid_actions',
                deposit.pid,
                action="review")

        return links
def download_url(record_id, url):
    """Create new file object and assign it to object version."""
    record = CAPDeposit.get_record(record_id)
    if url.startswith("root://"):
        from xrootdpyfs.xrdfile import XRootDPyFile
        response = XRootDPyFile(url, mode='r-')
        total = response.size
    else:
        try:
            from cap.modules.repoimporter.repo_importer import RepoImporter
            link = RepoImporter.create(url).archive_repository()
            response = requests.get(link, stream=True).raw
            total = int(response.headers.get('Content-Length'))
        except TypeError as exc:
            download_url.retry(exc=exc)
    record.files[url].file.set_contents(
        response,
        default_location=record.files.bucket.location.uri,
        size=total)
    db.session.commit()
def test_add_user_permissions_set_access_object_properly(app, db, users, create_deposit):
    owner, other_user = users['cms_user'], users['cms_user2']
    deposit = create_deposit(owner, 'alice-analysis-v0.0.1')

    assert deposit['_access'] == {
        'deposit-read': {
            'users': [owner.id],
            'roles': []
        },
        'deposit-update': {
            'users': [owner.id],
            'roles': []
        },
        'deposit-admin': {
            'users': [owner.id],
            'roles': []
        }
    }

    deposit._add_user_permissions(other_user,
                                  ['deposit-read',
                                   'deposit-update'],
                                  db.session)

    deposit = Deposit.get_record(deposit.id)

    assert deposit['_access'] == {
        'deposit-read': {
            'users': [owner.id, other_user.id],
            'roles': []
        },
        'deposit-update': {
            'users': [owner.id, other_user.id],
            'roles': []
        },
        'deposit-admin': {
            'users': [owner.id],
            'roles': []
        }
    }
def test_add_user_permissions_set_access_object_properly(
        app, db, users, create_deposit):
    owner, other_user = users['cms_user'], users['cms_user2']
    deposit = create_deposit(owner, 'alice-analysis-v0.0.1')

    assert deposit['_access'] == {
        'deposit-read': {
            'users': [owner.id],
            'roles': []
        },
        'deposit-update': {
            'users': [owner.id],
            'roles': []
        },
        'deposit-admin': {
            'users': [owner.id],
            'roles': []
        }
    }

    deposit._add_user_permissions(other_user,
                                  ['deposit-read', 'deposit-update'],
                                  db.session)

    deposit = Deposit.get_record(deposit.id)

    assert deposit['_access'] == {
        'deposit-read': {
            'users': [owner.id, other_user.id],
            'roles': []
        },
        'deposit-update': {
            'users': [owner.id, other_user.id],
            'roles': []
        },
        'deposit-admin': {
            'users': [owner.id],
            'roles': []
        }
    }
Exemple #39
0
def add_drafts_from_file(file_path,
                         schema,
                         egroup=None,
                         usermail=None,
                         limit=None):
    """Adds drafts from a specified file.

    Drafts with specified pid will be registered under those.
    For drafts without pid, new pids will be minted.
    """
    if usermail:
        user = get_existing_or_register_user(usermail)
    else:
        user = None

    with open(file_path, 'r') as fp:
        entries = json.load(fp)

        for entry in entries[0:limit]:
            data = construct_draft_obj(schema, entry)
            pid = cap_deposit_fetcher(None, data)
            pid_value = pid.pid_value if pid else None

            try:
                PersistentIdentifier.get('depid', pid_value)

                print('Draft with id {} already exist!'.format(pid_value))

            except PIDDoesNotExistError:
                record_uuid = uuid.uuid4()
                pid = cap_deposit_minter(record_uuid, data)
                deposit = CAPDeposit.create(data, record_uuid, user)
                deposit.commit()

                if egroup:
                    add_read_permission_for_egroup(deposit, egroup)

                print('Draft {} added.'.format(pid.pid_value))

        db.session.commit()
def synchronize_cadi_entries(limit=None):
    """Synchronize CMS analysis with CADI database.

    Updates cadi_info in existing analysis.

    If analysis with given CADI id doesn't exist yet,
    new deposit will be created.
    All members of CMS will get a read access.

    :params int limit: number of entries to update
    """
    entries = get_all_from_cadi()

    for entry in entries[:limit]:
        cadi_id, cadi_info = parse_cadi_entry(entry)

        try:  # update if cadi deposit already exists
            deposit = get_deposit_by_cadi_id(cadi_id)

            if deposit.get('cadi_info') == cadi_info:
                print('No changes in cadi entry {}.'.format(cadi_id))

            else:
                deposit['cadi_info'] = cadi_info
                deposit.commit()
                db.session.commit()

                print('Cadi entry {} updated.'.format(cadi_id))

        except (DepositDoesNotExist):
            cadi_deposit = build_cadi_deposit(cadi_id, cadi_info)
            deposit = CAPDeposit.create(data=cadi_deposit, owner=None)
            deposit._add_experiment_permissions('CMS', ['deposit-read'])
            deposit.commit()
            db.session.commit()

            print('Cadi entry {} added.'.format(cadi_id))