"""Highlight Model. Model representing marketing highlight metadata. """ from oto import response import sqlalchemy from sqlalchemy.orm import relationship from product_digital_marketing.connectors import mysql class MarketingHighlight(mysql.BaseModel): """Marketing Highlight Model.""" __tablename__ = 'marketing_highlight' pk = sqlalchemy.Column( 'id', sqlalchemy.Integer, primary_key=False, autoincrement=True) product_id = sqlalchemy.Column( sqlalchemy.Integer, primary_key=True, nullable=False) global_highlight = sqlalchemy.Column(sqlalchemy.Text) spotify_highlight = sqlalchemy.Column(sqlalchemy.Text) apple_highlight = sqlalchemy.Column(sqlalchemy.Text) spotify_total = sqlalchemy.Column( sqlalchemy.Integer, default=None, server_default=None) apple_total = sqlalchemy.Column( sqlalchemy.Integer, default=None, server_default=None) downloads_total = sqlalchemy.Column( sqlalchemy.Integer, default=None, server_default=None) updated_at = sqlalchemy.Column( sqlalchemy.DateTime, nullable=False, default=sqlalchemy.func.now(), onupdate=sqlalchemy.func.now()) updated_by = sqlalchemy.Column(sqlalchemy.VARCHAR(127)) projections = relationship( 'MarketingHighlightProjection', cascade='delete', lazy='joined', order_by='MarketingHighlightProjection.pk' ) def to_dict(self): """Create a dictionary representation of the object. Note: this dictionary does not return the last updated logging information. Returns: dict: the representation of the object. """ return dict( apple_highlight=self.apple_highlight, global_highlight=self.global_highlight, spotify_highlight=self.spotify_highlight, apple_total=self.apple_total, spotify_total=self.spotify_total, downloads_total=self.downloads_total, projections=[ projection.to_dict() for projection in self.projections] ) @mysql.wrap_db_errors def get_marketing_highlight_by_product_id(product_id): """Fetch marketing highlight by product id. Args: product_id (int): Product id. Returns: response.Response: object containing highlight if available else not found response. """ with mysql.db_session() as session: highlight = session.query(MarketingHighlight).filter( MarketingHighlight.product_id == product_id).first() if not highlight: return response.create_not_found_response() return response.Response(message=highlight.to_dict()) @mysql.wrap_db_errors def upsert_marketing_highlight(product_id, highlight_data, orchard_user_id): """ Create or update marketing highlight for a product. Args: highlight_data (dict): Highlight data to be used. product_id (int): Product id. orchard_user_id (str): Orchard user id Returns: response.Response: object containing highlight if available else not found response. """ with mysql.db_session() as session: highlight_obj = session.query(MarketingHighlight).filter( MarketingHighlight.product_id == product_id).first() if highlight_obj: highlight_obj.updated_by = orchard_user_id highlight_obj.apple_highlight = \ highlight_data.get('apple_highlight') highlight_obj.global_highlight = \ highlight_data.get('global_highlight') highlight_obj.spotify_highlight = \ highlight_data.get('spotify_highlight') highlight_obj.apple_total = highlight_data.get('apple_total') highlight_obj.spotify_total = \ highlight_data.get('spotify_total') highlight_obj.downloads_total = \ highlight_data.get('downloads_total') else: highlight_obj = MarketingHighlight( product_id=product_id, updated_by=orchard_user_id, apple_highlight=highlight_data.get('apple_highlight'), global_highlight=highlight_data.get('global_highlight'), spotify_highlight=highlight_data.get('spotify_highlight'), apple_total=highlight_data.get('apple_total'), spotify_total=highlight_data.get('spotify_total'), downloads_total=highlight_data.get('downloads_total'), ) session.add(highlight_obj) session.flush() return response.Response(message=highlight_obj.to_dict(), status=201)