"""Product Pricing Override Store. This model represents a Product Pricing Override Store """ import datetime from oto import response from pricing.connectors import mysql import sqlalchemy class ProductPricingOverrideStore(mysql.BaseModel): """Product Pricing Override Store model.""" __tablename__ = 'product_pricing_override_store' product_pricing_override_id = sqlalchemy.Column( sqlalchemy.BIGINT, primary_key=True) store_id = sqlalchemy.Column( sqlalchemy.BIGINT, primary_key=True) 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): """Convert a Product Pricing Override Store to dict.""" return dict( product_pricing_override_id=self.product_pricing_override_id, store_id=self.store_id) @mysql.autosession() def get_by_product_pricing_override_id(product_pricing_override_id, session): """Return the product pricing override stores for an override ID. Args: product_pricing_override_id (int): the ID of the product override. session (Session): the mysql session. Returns: response.Response: containing a list of override store dicts. """ product_pricing_override_stores = _get_by_product_pricing_override_id( product_pricing_override_id, session ) return response.Response( { 'items': product_pricing_override_stores }) def _get_by_product_pricing_override_id(product_pricing_override_id, session): """Get the product pricing override stores for an override ID. Args: product_pricing_override_id (int): the ID of the parent override. session (Session): the mysql session. Returns: [dict]: a list of product pricing override store dicts. """ rows = session.query(ProductPricingOverrideStore).where( ProductPricingOverrideStore.product_pricing_override_id == product_pricing_override_id ).all() return [row.to_dict() for row in rows] @mysql.autosession() def create(product_pricing_override_id, store_id, session): """Create a new product pricing override store. Args: product_pricing_override_id (int): the ID of the parent code. store_id (string): the ID of the store. session (Session): the mysql session. Returns: response.Response: containing the created product pricing override store dict. """ result = _create(product_pricing_override_id, store_id, session) session.commit() return result def _create(product_pricing_override_id, store_id, session): """Create a new product pricing override store. Args: product_pricing_override_id (int): the ID of the parent override. store_id (string): the ID of the store. session (Session): the mysql session. Returns: response.Response: containing the created product pricing override store dict. """ data = { 'product_pricing_override_id': product_pricing_override_id, 'store_id': store_id, 'created_date': datetime.datetime.now() } created = ProductPricingOverrideStore(**data) session.add(created) return response.Response(created.to_dict()) @mysql.autosession() def update_stores(product_pricing_override_id, store_ids, session): """Create or delete product pricing override stores. Args: product_pricing_override_id (int): the ID of the parent code. store_ids ([string]): a list of store IDs. session (Session): the mysql session. Returns: response.Response: containing the list of created and deleted product pricing override stores. """ existing_stores = _get_by_product_pricing_override_id( product_pricing_override_id, session) existing_store_ids = [item['store_id'] for item in existing_stores] create_new_store_ids = list(set( store_ids) - set(existing_store_ids)) delete_old_store_ids = list(set( existing_store_ids) - set(store_ids)) created = [] deleted = [] for store_id in create_new_store_ids: create_result = _create( product_pricing_override_id, store_id, session) created.append(create_result.message) for store_id in delete_old_store_ids: delete_result = _delete( product_pricing_override_id, store_id, session) deleted.append(delete_result.message) session.commit() return response.Response({ 'created': created, 'deleted': deleted}) @mysql.autosession() def delete(product_pricing_override_id, store_id, session): """Delete a product pricing override store. Args: product_pricing_override_id (int): the ID of the parent code. store_id (string): the ID of the store. session (Session): the mysql session. Returns: response.Response: containing the deleted product pricing override store dict. """ result = _delete(product_pricing_override_id, store_id, session) session.commit() return result def _delete(product_pricing_override_id, store_id, session): """Delete a product pricing override store. Args: product_pricing_override_id (int): the ID of the parent override. store_id (string): the ID of the store. session (Session): the mysql session. Returns: response.Response: containing the deleted product pricing override store dict. """ instance = session.query(ProductPricingOverrideStore).filter_by( product_pricing_override_id=product_pricing_override_id, store_id=store_id).one_or_none() if not instance: return response.create_not_found_response() session.delete(instance) return response.Response(instance.to_dict()) @mysql.autosession() def delete_by_product_pricing_override_id( product_pricing_override_id, session): """Delete the product pricing override store for an override ID. Args: product_pricing_override_id (int): the ID of the parent override. session (Session): the mysql session. Returns: [dict]: a list of deleted instances. """ instances = session.query(ProductPricingOverrideStore)\ .filter_by(product_pricing_override_id=product_pricing_override_id) result = [] for instance in instances: result.append(instance) session.delete(instance) return response.Response( { 'deleted': len(result) })