def test_resolve_links_random_link(taxonomy_tree): """ Test if random user data with link resolve taxonomy and keep user data. """ random_user_data = { "created_at": "2014-08-11T05:26:03.869245", "email": "*****@*****.**", "name": "Ken", "links": { "self": "http://127.0.0.1:5000/2.0/taxonomies/test_taxonomy/a/b" } } schema = TaxonomyField() res = schema.load(random_user_data) assert res == { 'ancestors': [{ 'links': { 'self': 'http://127.0.0.1:5000/2.0/taxonomies/test_taxonomy/a' }, 'test': 'extra_data' }], 'created_at': '2014-08-11T05:26:03.869245', 'email': '*****@*****.**', 'links': { 'self': 'http://127.0.0.1:5000/2.0/taxonomies/test_taxonomy/a/b' }, 'name': 'Ken', 'test': 'extra_data' }
def test_resolve_links_random_string_3(app, db, taxonomy_tree): """ Test if random user data (string) are passed. """ random_user_data = "bla bla http://example.com/taxonomies/a/b/z" schema = TaxonomyField() with pytest.raises(NoResultFound): schema.load(random_user_data)
def test_resolve_links_random_string_5(app, db, taxonomy_tree): """ Test if random user data (string) are passed. """ random_user_data = ["wrong type"] schema = TaxonomyField() with pytest.raises(TypeError): schema.load(random_user_data)
def test_resolve_links_random_string_2(app, db, taxonomy_tree): """ Test if random user data (string) are passed. """ random_user_data = "bla bla http://example.com/" schema = TaxonomyField() with pytest.raises(ValueError): schema.load(random_user_data)
class NResultsMetadataSchemaV1(CommonMetadataSchemaV2): N_certifyingAuthority = TaxonomyField( mixins=[TitledMixin, NcertifyingAuthorityMixin]) N_dateCertified = DateString() N_economicalParameters = SanitizedUnicode(validate=Length(max=1024)) N_internalID = SanitizedUnicode() N_referenceNumber = SanitizedUnicode() N_resultUsage = TaxonomyField(mixins=[TitledMixin]) N_technicalParameters = SanitizedUnicode(validate=Length(max=3000)) N_type = TaxonomyField(mixins=[TitledMixin], required=True)
def test_resolve_links_random(): """ Test if random user data are passed. """ random_user_data = { "created_at": "2014-08-11T05:26:03.869245", "email": "*****@*****.**", "name": "Ken", "test": "bla" } schema = TaxonomyField() with pytest.raises(ValidationError): schema.load(random_user_data)
class ThesisMetadataSchemaV2(CommonMetadataSchemaV2): dateDefended = DateString(required=True) defended = fields.Boolean(required=True) degreeGrantor = TaxonomyField(mixins=[TitledMixin, InstitutionsMixin], required=True) studyField = TaxonomyField(name="studyField", mixins=[TitledMixin, StudyFieldMixin]) @validates("dateDefended") def validate_date_range(self, value): date = arrow.get(value) min_ = arrow.get("1700-01-01") current_date = arrow.get() if date > current_date: raise ValidationError("Date cannot be in the future") if date < min_: raise ValidationError("Records older than from 1700 is not supported")
class RelatedIdentifierSchema(IS): """Related identifier schema.""" def __init__(self, **kwargs): """Related identifier schema constructor.""" super().__init__(allowed_schemes=RDM_RECORDS_IDENTIFIERS_SCHEMES, **kwargs) relation_type = TaxonomyField(mixins=[TitledMixin], required=True) resource_type = ResourceType()
def test_taxonomy_field(): extra = { "title": SanitizedUnicode(required=False), "language": List(SanitizedUnicode(required=False)) } schema = TaxonomyField(extra=extra, name="taxonomy") assert type(schema) == TaxonomyNested assert "title" in schema.nested.fields.keys() assert "language" in schema.nested.fields.keys()
class CreatorSchema(Schema): """Creator schema.""" person_or_org = fields.Nested(PersonOrOrganizationSchema, required=True) role = TaxonomyField(mixins=[TitledMixin]) affiliations = fields.List(fields.Nested(AffiliationSchema)) @validates_schema def validate_role(self, data, **kwargs): """Validate role."""
class ResourceTypeSchema(Schema): """Resource type schema.""" type = TaxonomyField(mixins=[TitledMixin], required=True) subtype = fields.Str() @validates_schema def validate_data(self, data, **kwargs): """Validate resource type.""" pass
class FundingReferenceSchema(StrictKeysMixin): projectID = SanitizedUnicode() projectName = SanitizedUnicode() fundingProgram = SanitizedUnicode() funder = TaxonomyField(mixins=[FunderMixin, TitledMixin]) @validates_schema def required_fields(self, data, **kwargs): if data.get("projectID"): if not data.get("funder"): raise ValidationError("Funder is required")
def test_resolve_links_random_string(app, db, taxonomy_tree): """ Test if random user data (string) are passed. """ random_user_data = "bla bla http://127.0.0.1:5000/2.0/taxonomies/test_taxonomy/a/b" schema = TaxonomyField() result = schema.load(random_user_data) assert result == { 'ancestors': [{ 'links': { 'self': 'http://127.0.0.1:5000/2.0/taxonomies/test_taxonomy/a' }, 'test': 'extra_data' }], 'links': { 'self': 'http://127.0.0.1:5000/2.0/taxonomies/test_taxonomy/a/b' }, 'test': 'extra_data' }
class RelatedItemSchema(StrictKeysMixin): itemTitle = SanitizedUnicode(required=True) itemDOI = DOI() itemISBN = List(ISBN()) itemISSN = List(ISSN()) itemURL = URL() itemYear = Year() itemVolume = SanitizedUnicode() itemIssue = SanitizedUnicode() itemStartPage = SanitizedUnicode() itemEndPage = SanitizedUnicode() itemRelationType = TaxonomyField(mixins=[TitledMixin], required=True) @validates_schema def required_journal(self, data, **kwargs): if data.get("ISSN") or data.get("itemVolume") or data.get( "itemIssue") or data.get("itemStartPage") or data.get( "itemEndPage"): journal_keys = [ "itemVolume", "itemIssue", "itemStartPage", "itemEndPage" ] for key in data.keys(): if key in journal_keys: journal_keys.pop(journal_keys.index(key)) if len(journal_keys) > 0: raise ValidationError( f"Required field(s) is/are missing: {journal_keys}") @validates_schema def validate_pages(self, data, **kwargs): start_page = data.get("itemStartPage") end_page = data.get("itemEndPage") if start_page and end_page: if int(start_page) > int(end_page): raise ValidationError( f"Start page ({start_page}) must be smaller than end page ({end_page})" )
class TestSchema(Schema): field = TaxonomyField(many=True)
class PublicationPlaceSchema(StrictKeysMixin): place = SanitizedUnicode() country = TaxonomyField(mixins=[TitledMixin, CountryMixin])
class ContributorSchema(PersonSchema): role = TaxonomyField(mixins=[TitledMixin, ContributorMixin], required=True)
class DataSetMetadataSchemaV2(InvenioRecordMetadataFilesMixin, InvenioRecordMetadataSchemaV1Mixin, StrictKeysMixin): """DataSet metaddata schema.""" resource_type = ResourceType(required=True) creators = fields.List(fields.Nested(CreatorSchema), required=True, validate=validate.Length( min=1, error=_("Missing data for required field."))) creator = SanitizedUnicode() title = MultilingualStringV2(required=True) additional_titles = List(MultilingualStringV2()) publisher = SanitizedUnicode() publication_date = EDTFDateString(required=True) subjects = List(fields.Nested(SubjectSchema)) contributors = List(fields.Nested(ContributorSchema)) dates = List(fields.Nested(DateSchema)) languages = TaxonomyField(mixins=[TitledMixin], many=True) # alternate identifiers identifiers = IdentifierSet( fields.Nested( partial(IdentifierSchema, allowed_schemes=RDM_RECORDS_IDENTIFIERS_SCHEMES))) related_identifiers = List(fields.Nested(RelatedIdentifierSchema)) version = SanitizedUnicode() rights = TaxonomyField(mixins=[TitledMixin, RightsMixin], many=True) abstract = MultilingualStringV2( required=True) # WARNING: May contain user-input HTML additional_descriptions = fields.List(MultilingualStringV2()) references = fields.List(fields.Nested(ReferenceSchema)) pids = fields.Dict(keys=fields.String(), values=fields.Nested(PIDSchema)) access = NestedAttribute(AccessSchema) keywords = List(SanitizedUnicode()) @pre_load def sanitize_html_fields(self, data, **kwargs): """Sanitize fields that may contain user-input HTML strings.""" if 'abstract' in data: for lang, val in data.get('abstract').items(): raw = data['abstract'][lang] data['abstract'][lang] = SanitizedHTML()._deserialize( raw, 'abstract', data) return data @pre_load def set_created(self, data, **kwargs): """Set created timestamp if not already set.""" dates = data.get('dates') or [] created = None for dat in dates: if dat.get('type', '') == 'created': created = dat.get('date') if not created: dates.append({ 'date': datetime.today().strftime('%Y-%m-%d'), 'type': 'created' }) data['dates'] = dates return data @pre_load def set_creator(self, data, **kwargs): """Set creator to record metadata if not known.""" if not data.get('creator'): if current_user and current_user.is_authenticated: data['creator'] = current_user.email else: data['creator'] = 'anonymous' return data @validates('pids') def validate_pids(self, value): """Validate the keys of the pids are supported providers.""" for scheme, pid_attrs in value.items(): # The required flag applies to the identifier value # It won't fail for empty allowing the components to reserve one id_schema = IdentifierSchema( identifier_required=True, allowed_schemes=RDM_RECORDS_IDENTIFIERS_SCHEMES) id_schema.load({ "scheme": scheme, "identifier": pid_attrs.get("identifier") })
class TestSchema(Schema): field = TaxonomyField(many=True, mixins=[InstitutionMixin])
class CommonMetadataSchemaV2(InvenioRecordMetadataSchemaV1Mixin, FSMRecordSchemaMixin, OARepoCommunitiesMixin, StrictKeysMixin): """Schema for the record metadata.""" abstract = MultilingualStringV2() accessibility = MultilingualStringV2() accessRights = TaxonomyField(mixins=[TitledMixin, AccessRightsMixin], required=True) creator = List(Nested(PersonSchema), required=True) contributor = List(Nested(ContributorSchema)) dateIssued = NRDate(required=True) dateModified = NRDate() resourceType = TaxonomyField(mixins=[TitledMixin], required=True) extent = List(SanitizedUnicode()) # TODO: pokud nemáme extent, spočítat z PDF - asi nepůjde externalLocation = Url() control_number = SanitizedUnicode(required=True) recordIdentifiers = Nested(RecordIdentifier) workIdentifiers = Nested(WorkIdentifersSchema) isGL = Boolean() language = TaxonomyField(mixins=[TitledMixin], required=True) note = List(SanitizedUnicode()) fundingReference = List(Nested(FundingReferenceSchema)) provider = TaxonomyField(mixins=[TitledMixin, InstitutionsMixin], required=True) entities = TaxonomyField(mixins=[TitledMixin, InstitutionsMixin], many=True) publicationPlace = Nested(PublicationPlaceSchema) publisher = List(SanitizedUnicode()) relatedItem = List(Nested(RelatedItemSchema)) rights = TaxonomyField(mixins=[TitledMixin, RightsMixin], many=True) series = List(Nested(SeriesSchema)) subject = TaxonomyField(mixins=[TitledMixin, SubjectMixin, PSHMixin, CZMeshMixin, MedvikMixin], many=True) keywords = List(MultilingualStringV2()) title = List(MultilingualStringV2(required=True), required=True, validate=Length(min=1)) titleAlternate = List(MultilingualStringV2()) rulesExceptions = List(Nested(RulesExceptionsSchema)) @pre_load def check_keyword(self, data, **kwargs): keywords = data.get("keywords", []) if isinstance(keywords, dict): if "error" in keywords: raise ValidationError(keywords["error"]) return data @post_load def check_language(self, data, **kwargs): language = data.get("language") if not language: raise ValidationError("Language is required field", field_name="language") return data @post_load def validate_keywords_subjects(self, data, **kwargs): subject = [x for x in data.get("subject", []) if not x["is_ancestor"]] keywords = data.get("keywords", []) if len(keywords) + len(subject) < 3: raise ValidationError("At least three subjects or keyword are required", field_name="keywords") return data @post_load def copy_to_entities(self, data, **kwargs): entities = data.get("entities") if not entities: data["entities"] = data["provider"] return data @post_load def rules_exceptions(self, data, **kwargs): if "rulesExceptions" in data: raise ValidationError(f"Some rules raises exception: {data['rulesExceptions']}") return data
class TestSchema(Schema): field = TaxonomyField()
class TestSchema(Schema): field = TaxonomyField(many=False)
class Events(StrictKeysMixin): nameOriginal = SanitizedUnicode(required=True) nameAlternate = List(SanitizedUnicode()) nameUnified = TaxonomyField(mixins=[TitledMixin]) date = DateRange(required=True) location = Nested(PublicationPlaceSchema())