"""Module with functions that takes data from art_relation database.""" from collections import OrderedDict from oto import response from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.sql import text from salessheets.connectors import mysql from salessheets.connectors.sentry import sentry_capture_exception from salessheets.constants import errors from salessheets.constants import models from salessheets.models.sql import albums from salessheets.models.sql import artists def get_album(product_id): """Get album info from art_relations for a provided release id. Args: product_id (str): album id. Returns: response.Response: containing dict of dict message dict keyed by product_id """ try: if not product_id: return response.Response(message={}) with mysql.session_scope() as session: rows = session.execute( text(albums.SELECT_ALBUM_BY_PRODUCT_ID), {models.PRODUCT_ID: product_id} ).mappings() result = rows.first() if not result: return response.create_not_found_response() return response.Response(message=dict(result)) except SQLAlchemyError as e: if sentry_capture_exception: sentry_capture_exception() return response.create_fatal_response(e.args) def get_tracks(product_id): """Get track info from track for a product id. Args: product_id (str): release id. Returns: response.Response: containing dict of dict message dict keyed by track_id """ try: if not product_id: return response.Response(message={}) with mysql.session_scope() as session: rows = session.execute( text(albums.SELECT_TRACKS_DETAILS_BY_PRODUCT_ID), {models.PRODUCT_ID: product_id} ).mappings().all() result = OrderedDict() for row in rows: row_dict = dict(row) result[row_dict[models.TRACK_ID]] = row_dict if not result: return response.create_not_found_response() return response.Response(message=result) except SQLAlchemyError as e: if sentry_capture_exception: sentry_capture_exception() return response.create_fatal_response(e.args) def get_artists(product_id): """Get artists info from release_artist for a product_id. Args: product_id (str): release id. Returns: response.Response: containing list message """ try: if not product_id: return response.Response(message=[]) with mysql.session_scope() as session: rows = session.execute( text(artists.GET_AUTHOR_BY_PRODUCT_ID), {models.PRODUCT_ID: product_id} ).mappings().all() result = [dict(row)[models.ARTIST_NAME] for row in rows] if not result: return response.create_not_found_response() return response.Response(message=result) except SQLAlchemyError as e: if sentry_capture_exception: sentry_capture_exception() return response.create_fatal_response(e.args) def get_product_type_id(product_id): """Get product_type_id from releases for a product id. Args: product_id (str): product id. Returns: response.Response: containing dict message """ try: if not product_id: return response.Response(message={}) with mysql.session_scope() as session: rows = session.execute( text(albums.SELECT_PRODUCT_TYPE_ID_BY_PRODUCT_ID), {models.PRODUCT_ID: product_id} ).mappings() result = rows.first() if not result: return response.create_not_found_response() return response.Response(dict(result)) except SQLAlchemyError as e: if sentry_capture_exception: sentry_capture_exception() return response.create_fatal_response(e.args) def get_distribution_format(product_id): """Get distribution format from `product_physical` table. Args: product_id (str): product id. Returns: response.Response: containing dict message """ try: if not product_id: return response.Response(message={}) with mysql.session_scope() as session: result = _get_product_configuration(product_id, session) if not result: return response.create_not_found_response() return response.Response(result) except SQLAlchemyError as e: if sentry_capture_exception: sentry_capture_exception() return response.create_fatal_response(e.args) def _get_product_configuration(product_id, session): """Get displayed configuration for a product. Args: product_id (str): id of the product to retrieve the configuration for session: database connection object Returns: product_configuration (dict): display configuration dictionary """ rows = session.execute( text(albums.SELECT_DISPLAY_CONFIGURATION_BY_PRODUCT_ID), {models.PRODUCT_ID: product_id} ).mappings() result = rows.first() if not result: return result return {'display_distribution_format': value for _, value in dict(result).items()} def get_context_type_and_status(product_id): """Get context_type and status from art_relations by provided release id. Args: product_id (str): album id. Returns: response.Response: containing dict message. The structure of the message in case of success: { 'context_type': 'digital', 'status': 'in_content' } """ try: if not product_id: return response.Response(message={}) with mysql.session_scope() as session: rows = session.execute( text(albums.SELECT_CONTEXT_TYPE_AND_STATUS_BY_PRODUCT_ID), {models.PRODUCT_ID: product_id} ).mappings() result = rows.first() if not result: return response.create_not_found_response() return response.Response(message=dict(result)) except SQLAlchemyError as e: if sentry_capture_exception: sentry_capture_exception() return response.create_fatal_response(e.args) def get_album_for_digital(product_id): """Get album info from art_relations for a provided release id. Get album info for digital product. Args: product_id (str): album id. Returns: response.Response: containing dict message. The structure of the message in case of success: { 'product_id': '12345', 'product_name': 'Product Name', ... 'context_type': 'digital', 'vendor_name': 'Vendor Name' } """ try: if not product_id: return response.Response(message={}) with mysql.session_scope() as session: rows = session.execute( text(albums.SELECT_ALBUM_FOR_DIGITAL_BY_PRODUCT_ID), {models.PRODUCT_ID: product_id} ).mappings() result = rows.first() if not result: return response.create_not_found_response() return response.Response(message=dict(result)) except SQLAlchemyError as e: if sentry_capture_exception: sentry_capture_exception() return response.create_fatal_response(e.args) def get_genre_and_subgenre(product_id): """Get genre and subgenre from art_relations by provided product id. Args: product_id (str): id of product. Returns: response.Response: containing dict message. The structure of the message in case of success: { 'genre': 'example genre', 'subgenre': 'example subgenre' } """ try: if not product_id: return response.create_error_response( code=errors.ERROR_CODE_MISSING_PRODUCT_ID, message=errors.ERROR_MESSAGE_MISSING_PRODUCT_ID) with mysql.session_scope() as session: rows = session.execute( text(albums.SELECT_GENRE_AND_SUBGENRE_BY_PRODUCT_ID), {models.PRODUCT_ID: product_id} ).mappings() result = rows.first() if not result: return response.create_not_found_response() return response.Response(message=dict(result)) except SQLAlchemyError as e: if sentry_capture_exception: sentry_capture_exception() return response.create_fatal_response(e.args)