class GeneratedFieldsSchema(schema_to_use): """Test schema.""" if marshmallow_version[0] >= 3: class Meta: """Meta attributes for the schema.""" unknown = EXCLUDE gen_function = GenFunction( serialize=serialize_func, deserialize=deserialize_func, ) gen_method = GenMethod( serialize='_serialize_gen_method', deserialize='_desererialize_gen_method', missing='raises-warning', ) def _serialize_gen_method(self, obj): # "meth-foo" from context or "meth-bar" from the object return self.context.get('meth-foo', obj.get('meth-bar', missing)) def _desererialize_gen_method(self, value, data): # "meth-foo" from context or "meth-bar" from the data return self.context.get('meth-foo', data.get('meth-bar', missing))
class CustomFieldSchema(schema_to_use): """Test schema.""" if marshmallow_version[0] >= 3: class Meta: """.""" unknown = EXCLUDE date_string_field = DateString(attribute='date_string_field') sanitized_html_field = SanitizedHTML(attribute='sanitized_html_field') sanitized_unicode_field = SanitizedUnicode( attribute='sanitized_unicode_field') trimmed_string_field = TrimmedString(attribute='trimmed_string_field') gen_function_field = GenFunction( lambda o: 'serialize_gen_function_field', lambda o: 'deserialize_gen_function_field', ) gen_method_field = GenMethod('serialize_gen_method_field', 'deserialize_gen_method_field') persistent_identifier_field = PersistentIdentifier() def serialize_gen_method_field(self, obj): """Serialize a value for the GenMethod field.""" return 'serialize_gen_method_field' def deserialize_gen_method_field(self, value): """Deserialize a value for the GenMethod field.""" return 'deserialize_gen_method_field'
class GeneratedFieldsSchema(StrictKeysMixin): """Test schema.""" gen_function = GenFunction( serialize=serialize_func, deserialize=deserialize_func, ) gen_method = GenMethod( serialize='_serialize_gen_method', deserialize='_desererialize_gen_method', missing='raises-warning', ) def _serialize_gen_method(self, obj): # "meth-foo" from context or "meth-bar" from the object return self.context.get('meth-foo', obj.get('meth-bar', missing)) def _desererialize_gen_method(self, value, data): # "meth-foo" from context or "meth-bar" from the data return self.context.get('meth-foo', data.get('meth-bar', missing))
class CustomFieldSchema(StrictKeysMixin): """Test schema.""" date_string_field = DateString(attribute='date_string_field') sanitized_html_field = SanitizedHTML(attribute='sanitized_html_field') sanitized_unicode_field = SanitizedUnicode( attribute='sanitized_unicode_field') trimmed_string_field = TrimmedString(attribute='trimmed_string_field') gen_function_field = GenFunction( lambda o: 'serialize_gen_function_field', lambda o: 'deserialize_gen_function_field', ) gen_method_field = GenMethod('serialize_gen_method_field', 'deserialize_gen_method_field') persistent_identifier_field = PersistentIdentifier() def serialize_gen_method_field(self, obj): """Serialize a value for the GenMethod field.""" return 'serialize_gen_method_field' def deserialize_gen_method_field(self, value): """Deserialize a value for the GenMethod field.""" return 'deserialize_gen_method_field'
class AuthorSchema(Schema): """Schema for a person.""" name = GenMethod(deserialize='load_name') affiliation = SanitizedUnicode() orcid = PersistentId(scheme='ORCID') def load_name(self, value, data): """Load name field.""" family_name = data.get('family-names') given_name = data.get('given-names') org_name = data.get('name') if org_name: return org_name if family_name and given_name: return u'{}, {}'.format(family_name, given_name) return family_name or given_name @validates_schema def validate_data(self, data): """Validate schema.""" name = data.get('name') if not name: raise ValidationError('Name is required.', field_names=['name'])
class MetadataSchemaV1(BaseSchema): """Schema for the record metadata.""" # Administrative fields _access = Nested(AccessSchemaV1, required=True) _owners = fields.List(fields.Integer, validate=validate.Length(min=1), required=True) _created_by = fields.Integer(required=True) _default_preview = SanitizedUnicode() _files = fields.List(Nested(FilesSchemaV1, dump_only=True)) _internal_notes = fields.List(Nested(InternalNoteSchemaV1)) _embargo_date = DateString(data_key="embargo_date", attribute="embargo_date") _communities = GenMethod('dump_communities') _contact = SanitizedUnicode(data_key="contact", attribute="contact") # Metadata fields access_right = SanitizedUnicode(required=True) identifiers = Identifiers() creators = fields.List(Nested(CreatorSchemaV1), required=True) titles = fields.List(Nested(TitleSchemaV1), required=True) resource_type = Nested(ResourceTypeSchemaV1, required=True) recid = PersistentIdentifier() publication_date = EDTFLevel0DateString( missing=lambda: date.today().isoformat()) subjects = fields.List(Nested(SubjectSchemaV1)) contributors = fields.List(Nested(ContributorSchemaV1)) dates = fields.List(Nested(DateSchemaV1)) language = SanitizedUnicode(validate=validate_iso639_3) related_identifiers = fields.List(Nested(RelatedIdentifierSchemaV1)) version = SanitizedUnicode() licenses = fields.List(Nested(LicenseSchemaV1)) descriptions = fields.List(Nested(DescriptionSchemaV1)) locations = fields.List(Nested(LocationSchemaV1)) references = fields.List(Nested(ReferenceSchemaV1)) extensions = fields.Method('dump_extensions', 'load_extensions') def dump_extensions(self, obj): """Dumps the extensions value. :params obj: invenio_records_files.api.Record instance """ current_app_metadata_extensions = ( current_app.extensions['invenio-rdm-records'].metadata_extensions) ExtensionSchema = current_app_metadata_extensions.to_schema() return ExtensionSchema().dump(obj.get('extensions', {})) def load_extensions(self, value): """Loads the 'extensions' field. :params value: content of the input's 'extensions' field """ current_app_metadata_extensions = ( current_app.extensions['invenio-rdm-records'].metadata_extensions) ExtensionSchema = current_app_metadata_extensions.to_schema() return ExtensionSchema().load(value) def dump_communities(self, obj): """Dumps communities related to the record.""" # NOTE: If the field is already there, it's coming from ES if '_communities' in obj: return CommunityStatusV1().dump(obj['_communities']) record = self.context.get('record') if record: _record = Record(record, model=record.model) return CommunityStatusV1().dump( RecordCommunitiesCollection(_record).as_dict()) @validates('_embargo_date') def validate_embargo_date(self, value): """Validate that embargo date is in the future.""" if arrow.get(value).date() <= arrow.utcnow().date(): raise ValidationError(_('Embargo date must be in the future.'), field_names=['embargo_date']) @validates('access_right') def validate_access_right(self, value): """Validate that access right is one of the allowed ones.""" access_right_key = {'access_right': value} validate_entry('access_right', access_right_key) @post_load def post_load_publication_date(self, obj, **kwargs): """Add '_publication_date_search' field.""" prepare_publication_date(obj) return obj