"""Instant Grats Persister. Handles doing CRUD operations on the track_instant_grat and related tables. """ from sqlalchemy import Column from sqlalchemy import DateTime from sqlalchemy import Enum from sqlalchemy import ForeignKey from sqlalchemy import func from sqlalchemy import Integer from sqlalchemy import TIMESTAMP from backend.connectors import mysql class InstantGrat(mysql.BaseModel): """Encapsulates Instant Grat data. Represents track_instant_grat table in the art_relations database. Instant Grats is a form which is being used in both ALW and OA and can be viewed/edited from both platforms. Each Instant Grat belongs to one track and one store. Instant Gratification allows customers to release some tracks of product before actual product release date. """ __tablename__ = 'track_instant_grat' instant_grat_id = Column( 'id', Integer, primary_key=True, autoincrement=True, nullable=False) tuid = Column( 'unique_track_id', ForeignKey('track.id'), nullable=False) store_id = Column( 'customer_master_master_id', Integer, nullable=False) active = Column( 'active', Enum('Y', 'N'), default='Y', nullable=False) date = Column('date', DateTime, nullable=True) user_id = Column('added_by_user_id', Integer, nullable=True) user_type = Column('added_by_user_type', Enum('oa', 'alw'), default='oa') date_created = Column( 'date_created', TIMESTAMP, server_default=func.now(), server_onupdate=func.now()) @property def created_by(self): """String in format 'user_type:user_id' if type and id exist. Returns None if at least one of those fields is empty. """ return ( '{account_type}:{account_id}'.format( account_type=str(self.user_type), account_id=str(self.user_id)) if self.user_id and self.user_type else '') def to_dict(self): """Python dict representation of Instant Grats model.""" return { 'tuid': self.tuid, 'store_id': self.store_id, 'date': self.date, 'created_by': self.created_by, 'created_at': self.date_created, 'active': self.active }