|
19 | 19 |
|
20 | 20 | """Common JSON schemas.""" |
21 | 21 |
|
22 | | -from marshmallow import RAISE, Schema, ValidationError, fields, validates_schema |
| 22 | +from marshmallow import ( |
| 23 | + RAISE, |
| 24 | + Schema, |
| 25 | + ValidationError, |
| 26 | + fields, |
| 27 | + post_load, |
| 28 | + pre_load, |
| 29 | + validates_schema, |
| 30 | +) |
23 | 31 | from marshmallow.validate import Length |
24 | 32 | from marshmallow_utils.fields import SanitizedHTML |
25 | 33 | from marshmallow_utils.html import sanitize_html |
@@ -176,6 +184,24 @@ class LegacyMARCFieldsSchema(Schema): |
176 | 184 | tag_088 = fields.List(fields.Str(), data_key="088") |
177 | 185 | tag_020 = fields.List(fields.Str(), data_key="020") |
178 | 186 |
|
| 187 | + def _strip_tag_prefix(self, data): |
| 188 | + """Shared logic to convert tag_XXX <-> XXX.""" |
| 189 | + transformed = {} |
| 190 | + for key, value in data.items(): |
| 191 | + if key.startswith("tag_"): |
| 192 | + transformed[key[4:]] = value |
| 193 | + else: |
| 194 | + transformed[key] = value |
| 195 | + return transformed |
| 196 | + |
| 197 | + @pre_load |
| 198 | + def normalize_tag_keys(self, data, **kwargs): |
| 199 | + return self._strip_tag_prefix(data) |
| 200 | + |
| 201 | + @post_load |
| 202 | + def restore_numeric_keys(self, data, **kwargs): |
| 203 | + return self._strip_tag_prefix(data) |
| 204 | + |
179 | 205 |
|
180 | 206 | class DigitizedMetadataSchema(Schema): |
181 | 207 | url = fields.Str() |
@@ -220,7 +246,9 @@ class CurationSchema(StrictKeysSchema): |
220 | 246 | internal_note = fields.List(fields.Str()) |
221 | 247 | legacy_marc_fields = fields.Nested(LegacyMARCFieldsSchema) |
222 | 248 | digitized = fields.List(fields.Nested(DigitizedMetadataSchema)) |
223 | | - digitized_preservation = fields.List(fields.Nested(DigitizedPreservationMetadataSchema)) |
| 249 | + digitized_preservation = fields.List( |
| 250 | + fields.Nested(DigitizedPreservationMetadataSchema) |
| 251 | + ) |
224 | 252 | digitized_description = fields.List(fields.Str()) |
225 | 253 | digitized_language = fields.List(fields.Str()) |
226 | 254 | digitized_keywords = fields.List(fields.Str()) |
|
0 commit comments