"""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) class LabelEntitySchema(BaseSchema): """Base schema for entities with label and subaccount fields.""" label_id = fields.Integer(data_key='labelId') subaccount_name = fields.String(data_key='subaccount') subaccount_id = fields.Integer(data_key='subaccountId') @post_dump def _remove_subaccount_name(self, data, **kwargs): """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'] return data class TrackCoreMetadataSchema(LabelEntitySchema): """Marshmallow for track core metadata.""" isrc = fields.String() artist_name = fields.String(data_key='artistName') image_location = fields.Url(data_key='imageLocation') name = fields.String(data_key='trackName') version = fields.String(data_key='version') # Aliases track_name = name track_version = version