"""Model representing a mkt priority.""" from datetime import datetime from oto import response import sentry_sdk from sqlalchemy import Column from sqlalchemy import DateTime from sqlalchemy import Enum from sqlalchemy import Integer from sqlalchemy.exc import SQLAlchemyError from project_manager.connector import mysql from project_manager.constant import error_const from project_manager.models import project class MktPriority(mysql.BaseModel): """Mkt Priority Project model.""" __tablename__ = 'mkt_priority_project' mkt_id = Column('mkt_priority_project_id', Integer, autoincrement=True, primary_key=True) priority = Column(Enum(*('a', 'b')), nullable=False) project_id = Column(Integer, nullable=True) country_id = Column(Integer, nullable=False) created_by = Column(Integer, nullable=True) updated_by = Column(Integer, nullable=True) created_on = Column(DateTime, nullable=True) updated_on = Column(DateTime, nullable=True) def to_dict(self): """Return dictionary of mkt priorities data. Returns: dict: mkt priority metadata. """ return { 'id': self.mkt_id, 'priority': self.priority, 'project_id': self.project_id, 'territory_id': self.country_id } def get_mkt_priority_by_project_id(project_id): """Get all of the mkt priorities for the given project. Args: project_id (int): project_id of the project. Returns: response.Response: response containing mkt priorities of project. """ mkt_priority_dicts = [] with mysql.pm_session_scope() as session: try: mkt_priorities = session.query(MktPriority).filter_by( project_id=project_id) for mkt_priority in mkt_priorities: mkt_priority_dicts.append(mkt_priority.to_dict()) return response.Response(message=mkt_priority_dicts) except SQLAlchemyError: sentry_sdk.capture_exception() return response.create_error_response( code=error_const.ERROR_MSG_INTERNAL_SERVER, message='could not connect to mysql', status=500) def get_bulk_mkt_priority_by_project_ids(project_ids): """Get all of the mkt priorities for the given project ids. Args: project_ids (list): project_id of the projects. Returns: response.Response: response containing mkt priorities of project. """ mkt_priority_dicts = [] with mysql.pm_session_scope() as session: try: mkt_priorities = session.query(MktPriority).filter( MktPriority.project_id.in_(project_ids)).all() mkt_priority_dicts = [ mkt_priority.to_dict() for mkt_priority in mkt_priorities ] return response.Response(message=mkt_priority_dicts) except SQLAlchemyError: sentry_sdk.capture_exception() return response.create_error_response( code=error_const.ERROR_MSG_INTERNAL_SERVER, message='could not connect to mysql', status=500) def set_mkt_priority_by_project_id(project_id, country_id, priority, user_id): """Set marketing priority based on project_id and country_id. Args: project_id (int): project_id of the project. country_id (int): country_id of country. priority (str): this would be either 'a' or 'b' Returns: response.Response: response containing mkt priorities of project. """ usr_id = user_id if user_id else None with mysql.pm_session_scope() as session: mkt_priority = session.query(MktPriority).filter_by( project_id=project_id, country_id=country_id, ).first() if (mkt_priority): return update_mkt_priority(session, mkt_priority, priority, usr_id) else: return create_mkt_priority(session, project_id, country_id, priority, usr_id) def update_mkt_priority(session, priority_obj, priority, user_id): """Update market priority. Args: session (obj): session object priority_obj (obj): mkt_priority_project object priority (str): this would be either 'a' or 'b'. Returns: response.Response: response containing updated mkt_priority_project object. """ priority_obj.priority = priority priority_obj.updated_on = datetime.utcnow() priority_obj.updated_by = user_id session.add(priority_obj) return response.Response(message=priority_obj.to_dict()) def create_mkt_priority(session, project_id, country_id, priority, user_id): """Create market priority. Args: session (obj): session object project_id (int): project_id of the project country_id (int): country_id of the country priority (str): this would be either 'a' or 'b'. Returns: response.Response: response containing created mkt_priority_project object. """ project_obj = project.get_project_instance(project_id) if not project_obj: return response.create_error_response( code=error_const.ERROR_CODE_NOT_FOUND, message='no project id', status=404 ) now = datetime.utcnow() priority_obj = MktPriority( priority=priority, country_id=country_id, project_id=project_id, created_by=user_id, updated_by=user_id, created_on=now, updated_on=now, ) session.add(priority_obj) session.flush() return response.Response(message=priority_obj.to_dict()) def delete_mkt_priority_by_project_id(project_id, projection_id): """Delete market priority. Args: project_id (int): project_id of the project projection_id (int): projection_id of the country. Returns: response.Response: empty response object. """ with mysql.pm_session_scope() as session: mkt_priority = session.query(MktPriority).filter( MktPriority.project_id == project_id, MktPriority.mkt_id == projection_id, ).first() if not mkt_priority: return response.create_not_found_response() session.delete(mkt_priority) return response.Response() def bulk_delete_mkt_priority_for_project(project_id): """Delete all marketing priorities of a project. Args: project_id (int): project_id of the project Returns: response.Response: empty response object. """ with mysql.pm_session_scope() as session: session.query(MktPriority).filter( MktPriority.project_id == project_id ).delete() return response.Response()