"""CRUD operations around period table in art_relations db.""" from sqlalchemy import Column from sqlalchemy import Enum from sqlalchemy import Integer from sqlalchemy import SmallInteger import mysql class Period(mysql.BaseModel): """Class represents the period table.""" __tablename__ = 'period' period_id = Column(Integer, primary_key=True, autoincrement=True) year = Column(SmallInteger, nullable=False) quarter = Column(Integer, nullable=False) month = Column(Integer, nullable=False) status = Column(Enum('open', 'closed', 'processing'), default='open') def to_dict(self): """Convert Period data to dict.""" data = { 'period_id': self.period_id, 'year': self.year, 'quarter': self.quarter, 'month': self.month, 'status': self.status } return data @mysql.wrap_db_errors def get_all_periods(): """Get all period records from period table. Returns: periods (list): list of all the period objects from database. """ with mysql.ar_db_session() as session: periods = session.query(Period).all() return [period.to_dict() for period in periods]