""" Import asset batch model. This import asset batch model uses sqlalchemy. It's used to store asset batch details in AR database """ from oto import response from sqlalchemy import Column from sqlalchemy import Enum from sqlalchemy import Integer from sqlalchemy.exc import SQLAlchemyError from assets.connectors import mysql from assets.connectors import sentry class ImportAssetBatch(mysql.ArModel): """Table definition for import_asset_batch table.""" __tablename__ = 'import_asset_batch' import_batch_id = Column( 'id', Integer, primary_key=True, autoincrement=True) vendor_contact_id = Column(Integer) impersonated = Column(Integer, default=0) ip_address = Column(Integer) import_type = Column(Enum(*['upload', 'copy', 'replace'])) def as_dict(self): """Return object as dict. Returns: dict: Dictionary representation of the object """ batch_dict = { 'id': self.import_batch_id, 'vendor_contact_id': self.vendor_contact_id, 'ip_address': self.ip_address, 'import_type': self.import_type, 'impersonated': self.impersonated } return batch_dict def create_import_asset_batch( vendor_contact_id, impersonated, ip_address=0, import_type='upload'): """Create new import asset batch record. Args: vendor_contact_id (int): Vendor contact id. impersonated (int): OA user id. ip_address (int): Client IP Address. import_type (str): asset batch import type. Returns: response.Response: Inserted record info or error. """ try: import_asset_batch = ImportAssetBatch( vendor_contact_id=vendor_contact_id, impersonated=impersonated, ip_address=ip_address, import_type=import_type) with mysql.ar_db_session() as session: session.add(import_asset_batch) session.flush() asset_batch_data = import_asset_batch.as_dict() return response.Response(asset_batch_data) except SQLAlchemyError as e: if sentry.sentry_client: sentry.sentry_client.captureException() return response.create_fatal_response(e.args)