""" AssetReceivedQueue Model. This AssetReceivedQueue model uses sqlalchemy. This direct_delivery table is used to track new assets that need to be encoded by legacy RBEncoder """ from oto import response from sqlalchemy import Column from sqlalchemy import Enum from sqlalchemy import Integer from sqlalchemy import String from sqlalchemy import DateTime from sqlalchemy.exc import SQLAlchemyError from assets.connectors import mysql from assets.connectors import sentry from assets.models.legacy import asset_received_destination_details class AssetReceivedQueue(mysql.DdModel): """Table definition for asset_received_queue table.""" __tablename__ = 'asset_received_queue' asset_received_queue_id = Column( 'id', Integer, primary_key=True, autoincrement=True) asset_type_id = Column(Integer, nullable=False) priority = Column(Integer, nullable=True) picked_up = Column(Enum('Y', 'N', 'P'), nullable=False) import_asset_id = Column(Integer, nullable=False) uploaded_asset_filename = Column(String(255), nullable=False) job_type = Column(Enum('upload', 'copy', 'replace')) last_modified = Column(DateTime) def as_dict(self): """Get dictionary representation. Returns: dict: Representation of asset received queue. """ return { 'id': self.asset_received_queue_id, 'asset_type_id': self.asset_type_id, 'priority': self.priority, 'picked_up': self.picked_up, 'import_asset_id': self.import_asset_id, 'uploaded_asset_filename': self.uploaded_asset_filename, 'job_type': self.job_type, 'last_modified': self.last_modified } def get_asset_received_queue_by_id(asset_received_queue_id): """Get a single asset received queue item by id. Args: asset_received_queue_id (int): unique identifier. Returns: response.Response: contains a dictionary of the item. """ with mysql.dd_db_session() as session: row = session.query(AssetReceivedQueue).filter( AssetReceivedQueue.asset_received_queue_id == asset_received_queue_id).one_or_none() if row: return response.Response(row.as_dict()) return response.create_not_found_response() def create_asset_received_queue( asset_type_id, priority, import_asset_id, uploaded_asset_filename, picked_up='N', job_type='upload'): """Create new asset received queue record. Args: asset_type_id (int): Asset type id priority (int): Job priority picked_up (str): Flag indicating whether asset has been picked up import_asset_id (int): Import asset id uploaded_asset_filename (str): Uploaded asset filename job_type (str): Job type Returns: response.Response: Inserted record info or error """ try: asset_received_queue = AssetReceivedQueue( asset_type_id=asset_type_id, priority=priority, picked_up=picked_up, import_asset_id=import_asset_id, uploaded_asset_filename=uploaded_asset_filename, job_type=job_type ) with mysql.dd_db_session() as session: session.add(asset_received_queue) session.flush() asset_received_queue_data = asset_received_queue.as_dict() return response.Response(asset_received_queue_data) except SQLAlchemyError as e: if sentry.sentry_client: sentry.sentry_client.captureException() return response.create_fatal_response(e.args) def get_assets_by_upc(upc): """Get the list of assets queue items by product upc. Args: upc (int): Product UPC Returns: response.Response: List of assets queue items or error in Response object. """ filters = [ (asset_received_destination_details .AssetReceivedDestinationDetails.upc == upc), (asset_received_destination_details .AssetReceivedDestinationDetails.asset_received_queue_id == AssetReceivedQueue.asset_received_queue_id)] try: with mysql.dd_db_session() as session: items = session.query( AssetReceivedQueue, asset_received_destination_details .AssetReceivedDestinationDetails).filter(*filters).all() assets_data = [{ 'asset_received_item': item[0].as_dict(), 'asset_received_details': item[1].as_dict()} for item in items] return response.Response(assets_data) except SQLAlchemyError as e: if sentry.sentry_client: sentry.sentry_client.captureException() return response.create_fatal_response(e.args)