def _validate_data(cls, data): if not isinstance(data, dict) or data == {}: raise DepositValidationError('Empty deposit data.') try: schema_fullpath = data['$schema'] except KeyError: raise DepositValidationError('Schema not specified.') try: Schema.get_by_fullpath(schema_fullpath) except (AttributeError, JSONSchemaNotFound): raise DepositValidationError( 'Schema {} is not a valid option.'.format(schema_fullpath))
def _validate_data(cls, data): if not isinstance(data, dict) or data == {}: raise DepositValidationError('Empty deposit data.') try: schema_fullpath = data['$schema'] except KeyError: raise DepositValidationError('Schema not specified.') try: Schema.get_by_fullpath(schema_fullpath) except (AttributeError, JSONSchemaNotFound): raise DepositValidationError('Schema {} is not a valid option.' .format(schema_fullpath))
def test_add_or_update_schema_when_schema_already_exist_updates_json_for_schema(db): # noqa db.session.add(Schema(**{ 'name': 'records/ana1', 'major': 1, 'minor': 0, 'patch': 2, 'experiment': 'CMS', 'fullname': 'Old Schema', 'json': {'field': 'initial'} })) db.session.commit() updated_data = { 'experiment': 'Updated', 'fullname': 'Updated Schema', 'jsonschema': { 'updated': 'string' } } add_or_update_schema(fullpath='records/ana1-v1.0.2.json', data=updated_data) schema = Schema.get_by_fullpath('records/ana1-v1.0.2.json') assert schema.name == 'records/ana1' assert schema.fullname == updated_data['fullname'] assert schema.experiment == updated_data['experiment'] assert schema.json == updated_data['jsonschema'] assert schema.major == 1 assert schema.minor == 0 assert schema.patch == 2
def test_add_or_update_schema_when_schema_already_exist_updates_json_for_schema( db): # noqa db.session.add( Schema( **{ 'name': 'records/ana1', 'major': 1, 'minor': 0, 'patch': 2, 'experiment': 'CMS', 'fullname': 'Old Schema', 'json': { 'field': 'initial' } })) db.session.commit() updated_data = { 'experiment': 'Updated', 'fullname': 'Updated Schema', 'jsonschema': { 'updated': 'string' } } add_or_update_schema(fullpath='records/ana1-v1.0.2.json', data=updated_data) schema = Schema.get_by_fullpath('records/ana1-v1.0.2.json') assert schema.name == 'records/ana1' assert schema.fullname == updated_data['fullname'] assert schema.experiment == updated_data['experiment'] assert schema.json == updated_data['jsonschema'] assert schema.major == 1 assert schema.minor == 0 assert schema.patch == 2
def _add_schema(schema, is_deposit=True, experiment=None, json=None, roles=None): """ Add new schema into db """ default_json = { 'title': { 'type': 'string' } } try: schema = Schema.get_by_fullpath(schema) except SchemaDoesNotExist: schema = Schema( fullpath=schema, experiment=experiment, is_deposit=is_deposit, json=json or default_json ) db.session.add(schema) db.session.commit() if roles: roles = Role.query.filter(Role.name.in_(roles)).all() for role in roles: schema.add_read_access(role) return schema
def resolve_cap_schemas(path): """Resolve CAP JSON schemas.""" schema = Schema.get_by_fullpath(path) if not has_request_context() or ReadSchemaPermission(schema).can(): return schema.json else: abort(403)
def test_add_or_update_schema_when_schema_does_not_exist_create_new_one(db): # noqa data = { 'experiment': 'CMS', 'fullname': 'Test Schema', 'jsonschema': {'field': 'string'} } add_or_update_schema(fullpath='records/ana1-v1.0.2.json', data=data) schema = Schema.get_by_fullpath('records/ana1-v1.0.2.json') assert schema.name == 'records/ana1' assert schema.fullname == data['fullname'] assert schema.experiment == data['experiment'] assert schema.json == data['jsonschema'] assert schema.major == 1 assert schema.minor == 0 assert schema.patch == 2
def test_add_or_update_schema_when_schema_does_not_exist_create_new_one( db): # noqa data = { 'experiment': 'CMS', 'fullname': 'Test Schema', 'jsonschema': { 'field': 'string' } } add_or_update_schema(fullpath='records/ana1-v1.0.2.json', data=data) schema = Schema.get_by_fullpath('records/ana1-v1.0.2.json') assert schema.name == 'records/ana1' assert schema.fullname == data['fullname'] assert schema.experiment == data['experiment'] assert schema.json == data['jsonschema'] assert schema.major == 1 assert schema.minor == 0 assert schema.patch == 2
def _add_schema(schema, is_deposit=True, experiment=None, json=None): """ Add new schema into db """ default_json = { 'title': { 'type': 'string' } } try: schema = Schema.get_by_fullpath(schema) except JSONSchemaNotFound: schema = Schema( fullpath=schema, experiment=experiment, is_deposit=is_deposit, json=json or default_json ) db.session.add(schema) db.session.commit() return schema
def _get_schema_needs(self, deposit): """Create deposit permissions are based on schema's permissions.""" if '$schema' in deposit: try: schema = Schema.get_by_fullpath(deposit['$schema']) except JSONSchemaNotFound: raise WrongJSONSchemaError('Schema {} doesnt exist.'. format(deposit['$schema'])) elif '$ana_type' in deposit: try: schema = Schema.get_latest( 'deposits/records/{}'.format(deposit['$ana_type']) ) except JSONSchemaNotFound: raise WrongJSONSchemaError('Schema with name {} doesnt exist.'. format(deposit['$ana_type'])) else: raise WrongJSONSchemaError( 'You have to specify either $schema or $ana_type') return ReadSchemaPermission(schema).needs
def _get_schema_needs(self, deposit): """Create deposit permissions are based on schema's permissions.""" if '$schema' in deposit: try: schema = Schema.get_by_fullpath(deposit['$schema']) except SchemaDoesNotExist: raise WrongJSONSchemaError('Schema {} doesnt exist.'. format(deposit['$schema'])) elif '$ana_type' in deposit: try: schema = Schema.get_latest( 'deposits/records/{}'.format(deposit['$ana_type']) ) except SchemaDoesNotExist: raise WrongJSONSchemaError('Schema with name {} doesnt exist.'. format(deposit['$ana_type'])) else: raise WrongJSONSchemaError( 'You have to specify either $schema or $ana_type') return ReadSchemaPermission(schema).needs
def test_get_by_fullpath(db): schema = Schema(**{'name': 'records/ana1', 'major': 1, 'minor': 0, 'patch': 1}) schema2 = Schema(**{'name': 'deposits/records/ana2', 'major': 2, 'minor': 1, 'patch': 1}) db.session.add(schema) db.session.add(schema2) db.session.commit() assert Schema.get_by_fullpath('https://some-host.com/schemas/records/ana1-v1.0.1.json') == schema assert Schema.get_by_fullpath('records/ana1-v1.0.1.json') == schema assert Schema.get_by_fullpath('https://some-host.com/schemas/deposits/records/ana2-v2.1.1') == schema2 assert Schema.get_by_fullpath('deposits/records/ana2-v2.1.1.json') == schema2 assert Schema.get_by_fullpath('/deposits/records/ana2-v2.1.1.json') == schema2 assert Schema.get_by_fullpath('/deposits/records/ana2-v2.1.1') == schema2
def test_get_by_fullpath(db): schema = Schema(**{'name': 'records/ana1', 'major': 1, 'minor': 0, 'patch': 1}) schema2 = Schema(**{'name': 'deposits/records/ana2', 'major': 2, 'minor': 1, 'patch': 1}) db.session.add(schema) db.session.add(schema2) db.session.commit() assert Schema.get_by_fullpath( 'https://some-host.com/schemas/records/ana1-v1.0.1.json') == schema assert Schema.get_by_fullpath('records/ana1-v1.0.1.json') == schema assert Schema.get_by_fullpath( 'https://some-host.com/schemas/deposits/records/ana2-v2.1.1') == schema2 assert Schema.get_by_fullpath( 'deposits/records/ana2-v2.1.1.json') == schema2 assert Schema.get_by_fullpath( '/deposits/records/ana2-v2.1.1.json') == schema2 assert Schema.get_by_fullpath('/deposits/records/ana2-v2.1.1') == schema2
def resolve_cap_schemas(path): """Resolve CAP JSON schemas.""" return Schema.get_by_fullpath(path).json
def test_get_by_fullpath_when_non_existing_raise_JSONSchemaNotFound(db): with raises(JSONSchemaNotFound): Schema.get_by_fullpath('/non-existing/schema/ana2-v2.1.1')
def test_get_by_fullpath_when_non_existing_raise_SchemaDoesNotExist(db): with raises(SchemaDoesNotExist): Schema.get_by_fullpath('/non-existing/schema/ana2-v2.1.1')
def _set_experiment(self): schema = Schema.get_by_fullpath(self['$schema']) self['_experiment'] = schema.experiment
def schema(self): """Schema property.""" return Schema.get_by_fullpath(self['$schema'])