from marshmallow import fields from apollo_notifications.charts.config import ChartsSchemaConfig, MovesSchemaConfig from apollo_notifications.charts.utils import build_moves_push_message, build_push_message, get_chart_push_key from apollo_notifications.serializers import PushBaseSchema, PushDataBaseSchema from apollo_notifications.utils import get_country_code, get_country_flag class PushDataChartsBaseSchema(PushDataBaseSchema): """Base schema for charts push message data.""" isrc = fields.Str(attribute="isrc") artist_name = fields.Str(attribute="artist_name") position = fields.Int(attribute="position") target = fields.Method("get_target") topic = fields.Method("get_topic") vendor = fields.Method("get_vendor") track_id = fields.Str(attribute="track_id") track_name = fields.Str(attribute="track_name") country_code = fields.Method("get_country_code") def get_target(self, track): return self.context["target"] def get_topic(self, track): return self.context["topic"] def get_vendor(self, track): return self.context["vendor"] def get_country_code(self, track): return self.context["country_code"] class PushDataChartsMovesSchema(PushDataChartsBaseSchema): """Base schema for major moves push message data.""" change = fields.Int(attribute="moves") class PushChartsBaseSchema(PushBaseSchema): """Base schema for charts push message.""" _data_schema_cls = PushDataChartsBaseSchema title = fields.Method("get_title") tokens = fields.Method("get_tokens") user_id = fields.Str(attribute="user_id") message = fields.Method("get_message") def __init__( self, config: ChartsSchemaConfig, date: str, market: str, *args, push_cls=None, push_data_cls=None, **kwargs): super().__init__(*args, push_cls=push_cls, push_data_cls=push_data_cls, **kwargs) self.context = self.get_base_context(config, date, market) def _get_message_key(self, track) -> str: """Returns UUID from from unique set of arguments.""" ctx = self.context return str(get_chart_push_key( ctx["date"], track.user_id, ctx["topic"], ctx["market"], ctx["vendor"], track.track_id)) def get_title(self, track): return self.context["title"] def get_tokens(self, track): return track.device_tokens.split(',') def get_message(self, track): return build_push_message(track, self.context["template"], self.context["country_flag"]) def get_base_context(self, config: ChartsSchemaConfig, date: str, market: str): country_code = get_country_code(market) return dict( topic=config.TOPIC, title=config.TITLE, target=config.PUSH_TARGET, vendor=config.VENDOR, template=config.MESSAGE_TEMPLATE, date=date, market=market, country_code=country_code, country_flag=get_country_flag(country_code) ) class PushChartsMovesSchema(PushChartsBaseSchema): """Base schema for major moves push message.""" _data_schema_cls = PushDataChartsMovesSchema message = fields.Method("get_message") def get_message(self, track): template_name = "template" if track.moves < 0 else "drop_template" return build_moves_push_message(track, self.context[template_name], self.context["country_flag"]) def get_base_context(self, config: MovesSchemaConfig, date: str, market: str): context = super().get_base_context(config, date, market) context["drop_template"] = config.SIGNIFICANT_DROP_TEMPLATE return context