def _prepare_edit(self, record): """Prepare deposit for editing. Extend the deposit's communities metadata by the pending inclusion requests. """ data = super(ZenodoDeposit, self)._prepare_edit(record) data.setdefault('communities', []).extend( [c.id_community for c in InclusionRequest.get_by_record(record.id)]) data['communities'] = sorted(list(set(data['communities']))) # Remove the OpenAIRE subtype if the record is no longer pending, # nor in the relevant community oa_type = data['resource_type'].get('openaire_subtype') if oa_type and not is_valid_openaire_type(data['resource_type'], data['communities']): del data['resource_type']['openaire_subtype'] if not data['communities']: del data['communities'] # If non-Zenodo DOI unlock the bucket to allow file-editing if not is_doi_locally_managed(data['doi']): self.files.bucket.locked = False return data
def _prepare_edit(self, record): """Prepare deposit for editing. Extend the deposit's communities metadata by the pending inclusion requests. """ data = super(ZenodoDeposit, self)._prepare_edit(record) data.setdefault('communities', []).extend( [c.id_community for c in InclusionRequest.get_by_record(record.id)]) data['communities'] = sorted(list(set(data['communities']))) # Remove the OpenAIRE subtype if the record is no longer pending, # nor in the relevant community oa_type = data['resource_type'].get('openaire_subtype') if oa_type and not is_valid_openaire_type(data['resource_type'], data['communities']): del data['resource_type']['openaire_subtype'] if not data['communities']: del data['communities'] return data
def validate_data(self, obj): """Validate resource type.""" type_ = obj.get('resource_type', {}).get('type') if type_ in ['publication', 'image']: type_dict = { 'type': type_, 'subtype': obj.get('resource_type', {}).get('subtype') } field_names = ['{0}_type'.format(type_)] else: type_dict = {'type': type_} field_names = ['upload_type'] if ObjectType.get_by_dict(type_dict) is None: raise ValidationError( _('Invalid upload, publication or image type.'), field_names=field_names, ) if not is_valid_openaire_type(obj.get('resource_type', {}), obj.get('communities', [])): raise ValidationError( _('Invalid OpenAIRE subtype.'), field_names=['openaire_subtype'], )
def validate_data(self, obj): """Validate resource type.""" type_ = obj.get('resource_type', {}).get('type') if type_ in ['publication', 'image']: type_dict = { 'type': type_, 'subtype': obj.get('resource_type', {}).get('subtype') } field_names = ['{0}_type'.format(type_)] else: type_dict = {'type': type_} field_names = ['upload_type'] if ObjectType.get_by_dict(type_dict) is None: raise ValidationError( _('Invalid upload, publication or image type.'), field_names=field_names, ) if not is_valid_openaire_type(obj.get('resource_type', {}), obj.get('communities', [])): raise ValidationError( _('Invalid OpenAIRE subtype.'), field_names=['openaire_subtype'], )
def test_openaire_type_validation(app): """Test validation of OpenAIRE subtypes.""" assert is_valid_openaire_type({}, []) assert is_valid_openaire_type({'type': 'dataset'}, ['c1', 'b2']) # valid case assert is_valid_openaire_type( { 'openaire_subtype': 'foo:t4', 'type': 'other' }, ['c1']) # another valid case assert is_valid_openaire_type( { 'openaire_subtype': 'bar:t3', 'type': 'software' }, ['c3']) # valid case (mixed communities, but subtype from other/foo) assert is_valid_openaire_type( { 'openaire_subtype': 'foo:t4', 'type': 'other' }, ['c1', 'c3']) # valid case (mixed communities, but subtype from software/bar) assert is_valid_openaire_type( { 'openaire_subtype': 'bar:t3', 'type': 'software' }, ['c1', 'c3']) # invalid OA subtype assert not is_valid_openaire_type( { 'openaire_subtype': 'xxx', 'type': 'other' }, ['c1']) # community missing assert not is_valid_openaire_type( { 'openaire_subtype': 'foo:oth1', 'type': 'other' }, []) # wrong community assert not is_valid_openaire_type( { 'openaire_subtype': 'foo:oth1', 'type': 'other' }, ['c3']) # wrong general type (software has a definition) assert not is_valid_openaire_type( { 'openaire_subtype': 'foo:t4', 'type': 'software' }, ['c1']) # wrong general type (dataset has no definition) assert not is_valid_openaire_type( { 'openaire_subtype': 'foo:t4', 'type': 'dataset' }, ['c1']) # non-existing prefix assert not is_valid_openaire_type( { 'openaire_subtype': 'xxx:t1', 'type': 'software' }, ['c1'])
def test_openaire_type_validation(app): """Test validation of OpenAIRE subtypes.""" assert is_valid_openaire_type({}, []) assert is_valid_openaire_type({'type': 'dataset'}, ['c1', 'b2']) # valid case assert is_valid_openaire_type( {'openaire_subtype': 'foo:t4', 'type': 'other'}, ['c1']) # another valid case assert is_valid_openaire_type( {'openaire_subtype': 'bar:t3', 'type': 'software'}, ['c3']) # valid case (mixed communities, but subtype from other/foo) assert is_valid_openaire_type( {'openaire_subtype': 'foo:t4', 'type': 'other'}, ['c1', 'c3']) # valid case (mixed communities, but subtype from software/bar) assert is_valid_openaire_type( {'openaire_subtype': 'bar:t3', 'type': 'software'}, ['c1', 'c3']) # invalid OA subtype assert not is_valid_openaire_type( {'openaire_subtype': 'xxx', 'type': 'other'}, ['c1']) # community missing assert not is_valid_openaire_type( {'openaire_subtype': 'foo:oth1', 'type': 'other'}, []) # wrong community assert not is_valid_openaire_type( {'openaire_subtype': 'foo:oth1', 'type': 'other'}, ['c3']) # wrong general type (software has a definition) assert not is_valid_openaire_type( {'openaire_subtype': 'foo:t4', 'type': 'software'}, ['c1']) # wrong general type (dataset has no definition) assert not is_valid_openaire_type( {'openaire_subtype': 'foo:t4', 'type': 'dataset'}, ['c1']) # non-existing prefix assert not is_valid_openaire_type( {'openaire_subtype': 'xxx:t1', 'type': 'software'}, ['c1'])