"""Common schemas used across the whole project. TODO: This should be moved to a separate PyPI package. """ from marshmallow import fields, post_dump from marshmallow import Schema class BaseSchema(Schema): """Marshmallow base schema.""" @classmethod def normalize(cls, obj, many=False, **kwargs): """Normalize input to match schema.""" instance = cls() instance.context.update(kwargs) return instance.dump(obj, many=many).data class LabelEntitySchema(BaseSchema): """Base schema for entities with label and subaccount fields.""" label_id = fields.Integer(dump_to='labelId') subaccount_name = fields.String(dump_to='subaccount') subaccount_id = fields.Integer(dump_to='subaccountId') @post_dump def _remove_subaccount_name(self, data): """Remove subaccount name if account_id matches subaccountId.""" account_id = self.context.get('account_id') if account_id and data.get('subaccount'): if str(account_id) == str(data.get('subaccountId')): del data['subaccount'] class TrackCoreMetadataSchema(LabelEntitySchema): """Marshmallow for track core metadata.""" isrc = fields.String() artist_name = fields.String(dump_to='artistName') image_location = fields.Url(dump_to='imageLocation') name = fields.String(dump_to='trackName') version = fields.String(dump_to='version') # Aliases track_name = name track_version = version