"""Promo Player Model. This model represents a Promo Player """ import datetime from oto import response from oto import status import sqlalchemy from promo_player.connectors import mysql from promo_player.constants import error from promo_player.constants import promo_player as promo_player_constants class PromoPlayer(mysql.BaseModel): """Promo Player model.""" __tablename__ = 'promo_player' promo_player_id = sqlalchemy.Column(sqlalchemy.BIGINT, primary_key=True) product_id = sqlalchemy.Column(sqlalchemy.BIGINT) vendor_id = sqlalchemy.Column(sqlalchemy.BIGINT, nullable=False) code = sqlalchemy.Column( sqlalchemy.VARCHAR(20), nullable=False, unique=True) active = sqlalchemy.Column(sqlalchemy.Boolean, default=True) expiration_mode = sqlalchemy.Column( sqlalchemy.Enum(*promo_player_constants.EXPIRATION_MODES), nullable=False) skin = sqlalchemy.Column( sqlalchemy.Enum(*promo_player_constants.SKINS), nullable=False) expiry_time = sqlalchemy.Column(sqlalchemy.DateTime) created_date = sqlalchemy.Column(sqlalchemy.DateTime) updated_date = sqlalchemy.Column(sqlalchemy.DateTime) created_by = sqlalchemy.Column(sqlalchemy.VARCHAR(255)) updated_by = sqlalchemy.Column(sqlalchemy.VARCHAR(255)) def to_dict(self): """Convert Promo Player to dict.""" expiry_time = None if isinstance(self.expiry_time, datetime.datetime): expiry_time = self.expiry_time.timestamp() return { 'promo_player_id': self.promo_player_id, 'product_id': self.product_id, 'vendor_id': self.vendor_id, 'code': self.code, 'active': self.active, 'expiration_mode': self.expiration_mode, 'skin': self.skin, 'expiry_time': expiry_time } @mysql.autosession() def create(data, session): """Create a new promo player. Args: data (dict): the data from which to create the promo player. session (Session): the mysql session. Returns: response.Response: containing the created promo player dict. """ if not all(key in data for key in promo_player_constants.CREATE_REQUIRED_FIELDS): return response.create_error_response( code=error.ERROR_CODE_BAD_REQUEST, message=error.ERROR_MESSAGE_MISSING_PARAMETERS, status=status.BAD_REQUEST) if data['expiration_mode'] not in promo_player_constants.EXPIRATION_MODES \ or data['skin'] not in promo_player_constants.SKINS: return response.create_error_response( code=error.ERROR_CODE_BAD_REQUEST, message=error.ERROR_MESSAGE_BAD_PARAMETERS, status=status.BAD_REQUEST) data['created_date'] = datetime.datetime.now() promo_player = PromoPlayer(**data) session.add(promo_player) session.commit() return response.Response(promo_player.to_dict()) @mysql.autosession() def get_active_by_product_id(product_id, session): """Get the active promo player for a product. Args: product_id (string): the product ID. session (Session): the mysql session. Returns: response.Response: containing the promo player dict. """ if not product_id: return response.create_error_response( code=error.ERROR_CODE_BAD_REQUEST, message=error.ERROR_MESSAGE_MISSING_PARAMETERS, status=status.BAD_REQUEST) promo_player = session.query(PromoPlayer)\ .filter_by(product_id=product_id, active=True)\ .order_by(PromoPlayer.promo_player_id.desc())\ .first() if not promo_player: return response.create_not_found_response() return response.Response(promo_player.to_dict()) @mysql.autosession() def update(data, session): """Update a promo player. Args: data (dict): the data from which to update the promo player. session (Session): the mysql session. Returns: response.Response: containing the updated promo player dict. """ if not all(key in data for key in promo_player_constants.UPDATE_REQUIRED_FIELDS): return response.create_error_response( code=error.ERROR_CODE_BAD_REQUEST, message=error.ERROR_MESSAGE_MISSING_PARAMETERS, status=status.BAD_REQUEST) if ('expiration_mode' in data and data['expiration_mode'] not in promo_player_constants.EXPIRATION_MODES) or \ ('skin' in data and data['skin'] not in promo_player_constants.SKINS): return response.create_error_response( code=error.ERROR_CODE_BAD_REQUEST, message=error.ERROR_MESSAGE_BAD_PARAMETERS, status=status.BAD_REQUEST) promo_player_id = data['promo_player_id'] product_id = data.get('product_id', None) promo_player = session.query(PromoPlayer).filter_by( promo_player_id=promo_player_id).one_or_none() if not promo_player or ( promo_player.product_id and promo_player.product_id != product_id): return response.create_not_found_response() for key, value in data.items(): if key in promo_player_constants.UPDATE_ALLOWED_FIELDS: setattr(promo_player, key, value) promo_player.updated_date = datetime.datetime.now() session.commit() return response.Response(promo_player.to_dict()) @mysql.autosession() def get_by_product_id(product_id, session): """Get all the promo players associated with the specified product. Args: product_id (int): the product ID. session (Session): the mysql session. Returns: response.Response: containing a dict with the list of promo players. """ if not product_id: return response.create_error_response( code=error.ERROR_CODE_BAD_REQUEST, message=error.ERROR_MESSAGE_MISSING_PARAMETERS, status=status.BAD_REQUEST) promo_players = session.query(PromoPlayer).filter_by( product_id=product_id).all() return response.Response({ 'items': [item.to_dict() for item in promo_players] }) @mysql.autosession() def get_by_vendor_id(vendor_id, session): """Get all the promo players associated with the specified vendor. Args: vendor_id (int): the vendor ID. session (Session): the mysql session. Returns: response.Response: containing a dict with the list of promo players. """ if not vendor_id: return response.create_error_response( code=error.ERROR_CODE_BAD_REQUEST, message=error.ERROR_MESSAGE_MISSING_PARAMETERS, status=status.BAD_REQUEST) promo_players = session.query(PromoPlayer).filter_by( vendor_id=vendor_id).all() return response.Response({ 'items': [item.to_dict() for item in promo_players] }) @mysql.autosession() def get_by_code(code, session): """Get a promo player by code. Args: code (string): the promo player unique code. session (Session): the mysql session. Returns: response.Response: containing the promo player dict. """ if not code: return response.create_error_response( code=error.ERROR_CODE_BAD_REQUEST, message=error.ERROR_MESSAGE_MISSING_PARAMETERS, status=status.BAD_REQUEST) promo_player = session.query(PromoPlayer).filter_by( code=code).one_or_none() if not promo_player: return response.create_not_found_response() return response.Response(promo_player.to_dict()) @mysql.autosession() def delete_by_product_id(product_id, session): """Delete all the promo players associated with the specified product. Args: product_id (int): the product ID. session (Session): the mysql session. Returns: response.Response: containing a dict with the list of deleted items. """ if not product_id: return response.create_error_response( code=error.ERROR_CODE_BAD_REQUEST, message=error.ERROR_MESSAGE_MISSING_PARAMETERS, status=status.BAD_REQUEST) query = session.query(PromoPlayer).filter_by(product_id=product_id) items = [item.to_dict() for item in query.all()] query.delete(synchronize_session=False) session.commit() return response.Response({'items': items})