"""Model for sales_goal table in sales_goals database.""" from datetime import datetime from oto import response from sqlalchemy import Column from sqlalchemy import DateTime from sqlalchemy import Integer from sqlalchemy import String from sqlalchemy import Text from sqlalchemy.ext.hybrid import hybrid_property from sales_goals.connectors import mysql from sales_goals.constants import error from sales_goals.models import error_handlers class SalesGoal(mysql.BaseModel): """Class representing the sales goal.""" __tablename__ = 'sales_goal' sales_goal_id = Column('id', Integer, primary_key=True, autoincrement=True) product_id = Column(Integer, nullable=False, unique=True) notes = Column(Text) hmv_notes = Column(Text) updated_by = Column(String(127)) _updated_at = Column( 'updated_at', DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow) @hybrid_property def updated_at(self): """Getter for private attribute _updated_at.""" return self._updated_at def to_dict(self): """Return object as dict. Returns: dict: Dictionary representation of object """ sales_goal_dict = { 'sales_goal_id': self.sales_goal_id, 'product_id': self.product_id, 'notes': self.notes, 'hmv_notes': self.hmv_notes, 'updated_by': self.updated_by, 'updated_at': self.updated_at } return sales_goal_dict @error_handlers.sqlalchemy_error_handler @error_handlers.integrity_error_handler( error_code=error.ERROR_CODE_MODEL_VALIDATION) def create( *, product_id, notes=None, hmv_notes=None, updated_by): """Create a new record in the sales_goal table. Args: product_id (int): product_id from art_relations. notes(str): notes value. hmv_notes(str): hmv_notes value. updated_by (str): user_id who performs the creation. Returns: response.Response: .message with SalesGoal.to_dict() on success, .errors on failure. """ sales_goal_obj = SalesGoal( product_id=product_id, notes=notes, hmv_notes=hmv_notes, updated_by=updated_by) with mysql.sales_goals_session_scope() as session: session.add(sales_goal_obj) return response.Response(sales_goal_obj.to_dict()) @error_handlers.sqlalchemy_error_handler @error_handlers.integrity_error_handler( error_code=error.ERROR_CODE_MODEL_VALIDATION) def fetch_by_product_id(product_id): """Get sales goal data from db by product_id. Args: product_id (int): product id of sales goal. Returns: response.Response: .message with SalesGoal.to_dict() on success, .errors on failure. """ with mysql.sales_goals_session_scope() as session: sales_goal = session.query(SalesGoal).filter_by( product_id=product_id).first() if not sales_goal: return response.create_not_found_response( error.ERROR_MESSAGE_SALES_GOAL_DOES_NOT_EXIST.format( product_id=product_id)) return response.Response(sales_goal.to_dict()) @error_handlers.sqlalchemy_error_handler @error_handlers.integrity_error_handler( error_code=error.ERROR_CODE_MODEL_VALIDATION) def update(sales_goal_id, values): """Update sales goal data by sales_goal_id (pk). Args: sales_goal_id (int): id of sales goal that should be updated. values (dict): dictionary of values to store. Returns: response.Response: empty .message on success, .errors on failure. """ with mysql.sales_goals_session_scope() as session: sales_goal = session.query(SalesGoal).filter_by( sales_goal_id=sales_goal_id) updated_goal = sales_goal.update(values) if not updated_goal: return response.create_not_found_response( 'Sales goals with id {sales_goal_id} do not exist'.format( sales_goal_id=sales_goal_id)) return response.Response() @error_handlers.sqlalchemy_error_handler @error_handlers.integrity_error_handler( error_code=error.ERROR_CODE_MODEL_VALIDATION) def update_by_product_id( *, product_id, first_week_estimate=None, notes=None, hmv_notes=None, updated_by): """Update an existing record in the sales_goal table. Args: product_id (int): product_id from art_relations. first_week_estimate (int): first week estimate value. notes(str): notes value. hmv_notes(str): hmv_notes value. updated_by (str): user_id who performs the update. Returns: response.Response: .message with SalesGoal.to_dict() on success, .errors on failure. """ with mysql.sales_goals_session_scope() as session: sales_goal_obj = ( session.query(SalesGoal) .with_for_update() .filter_by(product_id=product_id).first()) if not sales_goal_obj: return response.create_not_found_response( error.ERROR_MESSAGE_SALES_GOAL_DOES_NOT_EXIST.format( product_id=product_id)) nullable_attrs = { 'first_week_estimate': first_week_estimate, 'notes': notes, 'hmv_notes': hmv_notes} for attr, value in nullable_attrs.items(): if value is not None: setattr(sales_goal_obj, attr, value) sales_goal_obj.updated_by = updated_by session.add(sales_goal_obj) return response.Response(sales_goal_obj.to_dict())