""" Import asset detail model. This import asset model uses sqlalchemy. It's used to store imported asset details in AR database """ from oto import response from sqlalchemy import BigInteger from sqlalchemy import Column from sqlalchemy import Integer from sqlalchemy.exc import SQLAlchemyError from assets.connectors import mysql from assets.connectors import sentry from assets.constants import error class ImportAssetDetail(mysql.ArModel): """Table definition for import_asset_detail table.""" __tablename__ = 'import_asset_detail' import_asset_detail_id = Column( 'id', Integer, primary_key=True, autoincrement=True) import_asset_id = Column(Integer) upc = Column(BigInteger) track_id = Column(Integer) def as_dict(self): """Return object as dict. Returns: dict: Dictionary representation of the object """ detail_dict = { 'id': self.import_asset_detail_id, 'import_asset_id': self.import_asset_id, 'upc': self.upc, 'track_id': self.track_id, } return detail_dict def create_import_asset_detail(import_asset_id, upc, track_id): """Create new import asset detail record. Args: import_asset_id (int): Imported asset id upc (int): Product UPC track_id (int): Unique track id Returns: response.Response: Inserted record info or error """ try: import_asset_detail = ImportAssetDetail( import_asset_id=import_asset_id, upc=upc, track_id=track_id) with mysql.ar_db_session() as session: session.add(import_asset_detail) session.flush() import_asset_detail_data = import_asset_detail.as_dict() return response.Response(import_asset_detail_data) except SQLAlchemyError as e: if sentry.sentry_client: sentry.sentry_client.captureException() return response.create_fatal_response(e.args) def get_by_import_asset_id(import_asset_id): """Get import asset data from db by filename. Args: import_asset_id (int): Import asset id. Returns: response.Response: Data of import asset detail or error response. """ try: with mysql.ar_db_session() as session: import_asset_detail = session.query(ImportAssetDetail).filter( ImportAssetDetail.import_asset_id == import_asset_id).first() if not import_asset_detail: return response.create_not_found_response( error.ERROR_MESSAGE_ASSET_NOT_FOUND) import_asset_detail_dict = import_asset_detail.as_dict() return response.Response(import_asset_detail_dict) except SQLAlchemyError as ex: if sentry.sentry_client: sentry.sentry_client.captureException() return response.create_fatal_response(str(ex))