"""Product Migration Status Model. This model represents a Product Migration Status. """ import datetime from oto import response from pricing.connectors import mysql from pricing.constants import error import sqlalchemy class ProductMigrationStatus(mysql.BaseModel): """Product Migration Status model.""" __tablename__ = 'product_migration_status' product_migration_status_id = sqlalchemy.Column( sqlalchemy.BIGINT, primary_key=True, autoincrement=True) product_id = sqlalchemy.Column(sqlalchemy.Integer) migration_status = sqlalchemy.Column(sqlalchemy.VARCHAR(45)) created_date = sqlalchemy.Column(sqlalchemy.DateTime) created_by = sqlalchemy.Column(sqlalchemy.VARCHAR(45)) updated_date = sqlalchemy.Column(sqlalchemy.DateTime) updated_by = sqlalchemy.Column(sqlalchemy.VARCHAR(45)) def to_dict(self): """Return the object as dictionary.""" return dict( product_migration_status_id=self.product_migration_status_id, product_id=self.product_id, migration_status=self.migration_status) @mysql.autosession() def get_by_migration_status(migration_status, session): """Get all product migration status with the corresponding status. Args: migration_status (String): the status session (Session): the mysql session. """ rows = session.query(ProductMigrationStatus).filter_by( migration_status=migration_status).all() if not rows: return response.create_not_found_response() result = [row.to_dict() for row in rows] return response.Response({'items': result}) @mysql.autosession() def create(data, session): """Create a new Product Migration Status. Args: data (dict): the data to create the product migration status. session (Session): the mysql session. Returns: response.Response: containing the created product migration status dict. """ if not data: return response.create_error_response( 400, error.ERROR_MESSAGE_EMPTY_BODY) data['created_date'] = datetime.datetime.now() product_migration_status = ProductMigrationStatus(**data) session.add(product_migration_status) session.commit() return response.Response(product_migration_status.to_dict()) @mysql.autosession() def update_by_status_and_product_id(status, product_id, data, session): """Update a Product Migration Status. Args: status (string): the current product migration status product_id (int): the product id data (dict): the data to update the product migration status. session (Session): the mysql session. Returns: response.Response: containing the updated product migration status dict. """ if not data: return response.create_error_response( 400, error.ERROR_MESSAGE_EMPTY_BODY) data['updated_date'] = datetime.datetime.now() found = _get_by_status_and_product_id( status, product_id, session) if not found: return response.create_not_found_response() for key, value in data.items(): setattr(found, key, value) session.commit() return response.Response(found.to_dict()) def _get_by_status_and_product_id(status, product_id, session): """Get a product migration status by its status and its product id. Args: status (string): the status product_id (int): the product id session (Session): the mysql session Returns: ProductMigrationStatus: the found product migration status """ found = session.query(ProductMigrationStatus).filter_by( migration_status=status, product_id=product_id).one_or_none() return found