"""Project Model.""" from oto import response from sqlalchemy import BigInteger from sqlalchemy import Column from sqlalchemy import Date from sqlalchemy import Enum from sqlalchemy import Integer from sqlalchemy import String from sqlalchemy import Text from project_manager.connector import mysql class Project(mysql.BaseModel): """Project Model. Represents Project Data """ __tablename__ = 'project' project_id = Column( BigInteger, primary_key=True, autoincrement=True, nullable=False) project_code = Column(String) vendor_id = Column(Integer, nullable=False) subaccount_id = Column(Integer, nullable=False) project_name = Column(String, nullable=False) created_date_utc = Column(Date, nullable=False) updated_date_utc = Column(Date, nullable=False) correlation_id = Column(String) artist_id = Column(Integer) description = Column(Text) deletions = Column(Enum('Y', 'N'), nullable=False, default='N') def to_dict(self): """Return a dictionary of the project's properties.""" return { 'project_id': self.project_id, 'project_code': self.project_code, 'vendor_id': self.vendor_id, 'subaccount_id': self.subaccount_id, 'project_name': self.project_name, 'created_date_utc': self.created_date_utc, 'updated_date_utc': self.updated_date_utc, 'correlation_id': self.correlation_id, 'artist_id': self.artist_id, 'description': self.description, 'deletions': self.deletions} @mysql.wrap_db_errors def delete_project(project_id): """Delete a project from the project table. Args: project_id (int): The unique id of a project. Returns: response.Response: A response object with the outcome of the delete. """ with mysql.pm_session_scope() as session: existing_project = session.query(Project).get(project_id) if not existing_project: return response.create_not_found_response( message='project was not found') existing_project.deletions = 'Y' session.merge(existing_project) session.flush() return response.Response() @mysql.wrap_db_errors def hard_delete_project(project_id): """Hard delete a project from the project table. Args: project_id (int): The unique id of a project. Returns: response.Response: A response object with the outcome of the delete. """ with mysql.pm_session_scope() as session: existing_project = session.query(Project).get(project_id) if not existing_project: return response.create_not_found_response( message='project was not found') session.delete(existing_project) session.flush() return response.Response() @mysql.wrap_db_errors def get_project_instance(project_id): """Get a project from the project table. Args: project_id (int): The unique id of a project. Returns: response.Response: A response object with the outcome of the fetch. """ with mysql.pm_session_scope() as session: existing_project = session.query(Project).get(project_id) if not existing_project: return response.create_not_found_response( message='project was not found') return response.Response(existing_project)