import structlog from marshmallow import EXCLUDE, ValidationError, fields from marshmallow_sqlalchemy.fields import Nested from delphi_api.v3.data_models.postgres_db import ( Artist, Chart, ChartRank, Dsp, Label, Playlist, PlaylistRank, Product, Region, Track, ) from delphi_api.v3.data_models.schemas import ma from delphi_api.v3.data_models.schemas.generics import ( ChartTrackPositionSimpleSchema, FollowerSchema, ImageSimpleSchema, PlaylistTrackPositionSimpleSchema, ) LOG = structlog.get_logger(__name__) class RemoteImageBaseSchema(ma.ModelSchema): """``image`` field is populated via :module:`delphi_api.v3.atlas.`""" image = fields.Method('get_image') # noinspection PyMethodMayBeStatic def get_image(self, obj): try: schema = ImageSimpleSchema(context={ 'model_name': self.Meta.model.__name__ }) return schema.load(obj) except ValidationError as e: LOG.warning(str(e)) except Exception as e: LOG.exception(str(e)) return None class DspSchema(ma.ModelSchema): class Meta: model = Dsp # noinspection PyUnresolvedReferences fields = ( Dsp.dsp_id.key, Dsp.name.key, Dsp.slug.key, ) class ArtistSimpleSchema(RemoteImageBaseSchema): class Meta: model = Artist # noinspection PyUnresolvedReferences fields = ( Artist.artist_id.key, Artist.full_name.key, Artist.first_name.key, Artist.last_name.key, Artist.image.key, ) # spotify_popularity = fields.Method('get_spotify_popularity') spotify_popularity = fields.Integer(required=False, missing=None, default=None) def get_spotify_popularity(self): return class ProductSimpleSchema(ma.ModelSchema): class Meta: model = Product # noinspection PyUnresolvedReferences fields = ( Product.name.key, Product.product_id.key, Product.release_date.key, ) class TrackSimpleSchema(ma.ModelSchema): class Meta: model = Track # noinspection PyUnresolvedReferences fields = ( Track.name.key, Track.track_name_suppl.key, Track.track_id.key, Track.product.key, Track.isrc.key, Track.release_date.key, Track.lyrics_version.key, ) product = Nested(ProductSimpleSchema) class ArtistSchema(ArtistSimpleSchema): class Meta: model = Artist # noinspection PyUnresolvedReferences fields = ArtistSimpleSchema.Meta.fields + ( Artist.tracks.key, Artist.products.key, ) tracks = Nested(TrackSimpleSchema, many=True) products = Nested(ProductSimpleSchema, many=True) class ArtistFollowersSchema(ArtistSimpleSchema): """Unused""" followers = Nested(FollowerSchema, many=True) class LabelSchema(ma.ModelSchema): class Meta: model = Label fields = ( Label.name.key, Label.label_id.key, ) class RegionSchema(ma.ModelSchema): class Meta: model = Region fields = ( Region.region_name.key, Region.region_id.key, Region.country_code.key, Region.country_name.key, ) class PlaylistRankSimpleSchema(ma.ModelSchema): class Meta: model = PlaylistRank fields = ( PlaylistRank.playlist_id.key, PlaylistRank.report_date.key, PlaylistRank.rank.key, PlaylistRank.dsp_id.key, PlaylistRank.country_code.key, ) class PlaylistSimpleSchema(RemoteImageBaseSchema): class Meta: model = Playlist # noinspection PyUnresolvedReferences fields = ( Playlist.country_code.key, Playlist.dsp.key, Playlist.dsp_playlist_id.key, Playlist.image.key, Playlist.name.key, Playlist.num_tracks.key, Playlist.playlist_id.key, Playlist.playlist_rank.key, Playlist.uri.key, 'rank', ) dsp = Nested(DspSchema) playlist_rank = Nested(PlaylistRankSimpleSchema, load_only=True) class ChartRankSimpleSchema(ma.ModelSchema): class Meta: model = ChartRank fields = ( ChartRank.chart_id.key, ChartRank.report_date.key, ChartRank.rank.key, ChartRank.dsp_id.key, ChartRank.country_code.key, ) class ChartSimpleSchema(ma.ModelSchema): class Meta: model = Chart # noinspection PyUnresolvedReferences fields = ( Chart.chart_group.key, Chart.chart_id.key, Chart.country_code.key, Chart.dsp.key, Chart.name.key, Chart.num_tracks.key, Chart.chart_rank.key, Chart.uri.key, 'rank', ) dsp = Nested(DspSchema) chart_rank = Nested(ChartRankSimpleSchema, load_only=True) class ChartTrackPositionSchema(ChartTrackPositionSimpleSchema): class Meta: unknown = EXCLUDE chart_id = fields.String(required=False, missing=None, default=None) chart = Nested(ChartSimpleSchema, required=False, missing=None) class ChartRankSchema(ChartRankSimpleSchema): class Meta: model = ChartRank # noinspection PyUnresolvedReferences fields = ChartRankSimpleSchema.Meta.fields + ( # ChartRank.dsp.key, ChartRank.chart.key, ) dsp = Nested(DspSchema) chart = Nested(ChartSimpleSchema) class PlaylistTrackPositionSchema(PlaylistTrackPositionSimpleSchema): class Meta: unknown = EXCLUDE playlist_id = fields.String(required=False, missing=None) playlist = Nested(PlaylistSimpleSchema, required=False, missing=None) streams = fields.Integer(required=False, missing=None, default=None) class PlaylistSchema(PlaylistSimpleSchema): class Meta: model = Playlist fields = PlaylistSimpleSchema.Meta.fields dsp = Nested(DspSchema) track_positions = Nested(PlaylistTrackPositionSimpleSchema, many=True) class PlaylistRankSchema(PlaylistRankSimpleSchema): class Meta: model = PlaylistRank # noinspection PyUnresolvedReferences fields = PlaylistRankSimpleSchema.Meta.fields + ( # PlaylistRank.dsp.key, PlaylistRank.playlist.key, ) dsp = Nested(DspSchema) playlist = Nested(PlaylistSimpleSchema) class ChartSchema(ChartSimpleSchema): class Meta: model = Chart fields = ChartSimpleSchema.Meta.fields dsp = Nested(DspSchema) track_positions = Nested(ChartTrackPositionSimpleSchema, many=True) class ProductSchema(ma.ModelSchema): class Meta: model = Product # noinspection PyUnresolvedReferences fields = ProductSimpleSchema.Meta.fields + ( Product.artists.key, Product.tracks.key, ) artists = Nested(ArtistSimpleSchema, many=True) tracks = Nested(TrackSimpleSchema, many=True) class TrackSchema(TrackSimpleSchema): class Meta: # noinspection PyUnresolvedReferences fields = TrackSimpleSchema.Meta.fields + ( Track.artists.key, ) artists = Nested(ArtistSimpleSchema, many=True)