"""Store Pricing Tier Code. This model represents a Store Pricing Code """ import datetime from oto import response from pricing.connectors import mysql from pricing.constants import error import sqlalchemy class StorePricingTierCode(mysql.BaseModel): """Store Pricing Tier Code model.""" __tablename__ = 'store_pricing_tier_code' store_pricing_tier_code_id = sqlalchemy.Column( sqlalchemy.BIGINT, primary_key=True) store_pricing_tier_id = sqlalchemy.Column(sqlalchemy.BIGINT) price_code = sqlalchemy.Column(sqlalchemy.VARCHAR(45)) resolution = sqlalchemy.Column(sqlalchemy.VARCHAR(45)) applies_worldwide = sqlalchemy.Column(sqlalchemy.Boolean) territory_list_include = sqlalchemy.Column(sqlalchemy.Boolean) 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 Store Pricing Tier Code to dict.""" return dict( store_pricing_tier_code_id=self.store_pricing_tier_code_id, store_pricing_tier_id=self.store_pricing_tier_id, price_code=self.price_code, resolution=self.resolution, applies_worldwide=self.applies_worldwide, territory_list_include=self.territory_list_include) @mysql.autosession() def get_by_store_pricing_tier_id(store_pricing_tier_id, session): """Return the store pricing tier codes for a specific store pricing tier. Args: store_pricing_tier_id (int): the ID of the store pricing tier. session (Session): the mysql session. Returns: response.Response: containing a list of store pricing tier code dicts. """ store_pricing_tier_codes = session.query(StorePricingTierCode).filter_by( store_pricing_tier_id=store_pricing_tier_id).all() return response.Response( { 'items': [ item.to_dict() for item in store_pricing_tier_codes ] }) @mysql.autosession() def get_by_store_pricing_tier_code_id(store_pricing_tier_code_id, session): """Get a store pricing tier code by its id. Args: store_pricing_tier_code_id (int): the ID of the store pricing tier code session (Session): the mysql session Returns: response.Response: containing the store pricing tier code dict """ instance = _get_by_store_pricing_tier_code_id( store_pricing_tier_code_id, session) if not instance: return response.create_not_found_response() return response.Response(instance.to_dict()) def _get_by_store_pricing_tier_code_id(store_pricing_tier_code_id, session): """Get a store pricing tier code by its id. Args: store_pricing_tier_code_id (int): the ID of the store pricing tier code session (Session): the mysql session Returns: response.Response: containing the store pricing tier code dict """ instance = session.query(StorePricingTierCode).filter_by( store_pricing_tier_code_id=store_pricing_tier_code_id).one_or_none() return instance @mysql.autosession() def create(store_pricing_tier_id, data, session): """Create a new store pricing tier code. Args: store_pricing_tier_id (int): the ID of the store pricing tier. data (dict): the data from which to create the store pricing tier code. session (Session): the mysql session. Returns: response.Response: containing the created store pricing tier code dict. """ if not data: return response.create_error_response( 400, error.ERROR_MESSAGE_EMPTY_BODY) data['store_pricing_tier_id'] = store_pricing_tier_id data['created_date'] = datetime.datetime.now() store_pricing_tier_code = StorePricingTierCode(**data) session.add(store_pricing_tier_code) session.commit() return response.Response(store_pricing_tier_code.to_dict()) @mysql.autosession() def update(store_pricing_tier_id, store_pricing_tier_code_id, data, session): """Update a store pricing tier code by its ID. Args: store_pricing_tier_id (int): the ID of the store pricing tier. store_pricing_tier_code_id (int): the store pricing tier code ID. data (dict): the data from which to update the store pricing tier code. session (Session): the mysql session. Returns: response.Response: containing the updated store pricing tier code dict. """ if not data: return response.create_error_response( 400, error.ERROR_MESSAGE_EMPTY_BODY) data['updated_date'] = datetime.datetime.now() found_store_pricing_tier_code = session.query( StorePricingTierCode).filter_by( store_pricing_tier_id=store_pricing_tier_id, store_pricing_tier_code_id=store_pricing_tier_code_id).\ one_or_none() if not found_store_pricing_tier_code: return response.create_not_found_response() for key, value in data.items(): setattr(found_store_pricing_tier_code, key, value) session.commit() return response.Response(found_store_pricing_tier_code.to_dict()) @mysql.autosession() def find_duplicate(store_pricing_tier_id, data, session): """Find a duplicate store pricing tier code. Args: store_pricing_tier_id (int): the ID of the store pricing tier. data (dict): the data to use to find the duplicate. session (Session): the mysql session. Returns: response.Response: containing True if there's a duplicate and False if there isn't. """ if not data: return response.create_error_response( 400, error.ERROR_MESSAGE_EMPTY_BODY) data['store_pricing_tier_id'] = store_pricing_tier_id match = session.query(StorePricingTierCode).filter_by(**data).first() if match is None: return response.Response({'found': False}) else: return response.Response({'found': True, 'item': match.to_dict()}) @mysql.autosession() def delete(store_pricing_tier_code_id, session): """Delete a store pricing tier code. Args: store_pricing_tier_code_id (int): the ID of the store pricing tier code. session (Session): the mysql session. Returns: response.Response: containing the deleted store pricing tier code dict. """ result = _delete(store_pricing_tier_code_id, session) session.commit() return result def _delete(store_pricing_tier_code_id, session): """Delete a store pricing tier code. Args: store_pricing_tier_code_id (int): the ID of the store pricing tier code session (Session): the mysql session Returns: response.Response: containing the deleted store pricing tier code dict """ instance = _get_by_store_pricing_tier_code_id( store_pricing_tier_code_id, session) if not instance: return response.create_not_found_response() session.delete(instance) return response.Response(instance.to_dict())