"""Promo Player logic.""" import random import string from oto import response from promo_player.models import promo_player from promo_player.models import promo_player_track def generate_code(): """Generate a unique 20 characters alphanumeric string. Returns: string: the generated string. """ return ''.join( random.SystemRandom().choice( string.ascii_letters + string.digits) for _ in range(20)) def create(data): """Create a new promo player. Args: data (dict): the data from which to create the promo player. Returns: response.Response: containing the created promo player dict. """ product_id = data.get('product_id', None) if product_id: active_promo_player_response = get_active_by_product_id(product_id) if active_promo_player_response: active_promo_player = active_promo_player_response.message update_data = { 'promo_player_id': active_promo_player['promo_player_id'], 'product_id': data['product_id'], 'active': False } update(update_data) data['code'] = generate_code() return promo_player.create(data) def get_active_by_product_id(product_id): """Get the active promo player for a product. Args: product_id (int): the product ID. Returns: response.Response: containing the promo player dict. """ promo_player_response = promo_player.get_active_by_product_id(product_id) return get_tracks_for_promo_player(promo_player_response) def update(data): """Update a promo player. Args: data (dict): the data from which to update the promo player. Returns: response.Response: containing the updated promo player dict. """ return promo_player.update(data) def get_by_product_id(product_id): """Get all the promo players associated with the specified product. Args: product_id (int): the product ID. Returns: response.Response: containing a dict with the list of promo players. """ return promo_player.get_by_product_id(product_id) def get_by_vendor_id(vendor_id): """Get all the promo players associated with the specified vendor. Args: vendor_id (int): the vendor ID. Returns: response.Response: containing a dict with the list of promo players. """ return promo_player.get_by_vendor_id(vendor_id) def get_by_code(code): """Get a promo player by code. Args: code (string): the promo player unique code. Returns: response.Response: containing the promo player dict. """ promo_player_response = promo_player.get_by_code(code) return get_tracks_for_promo_player(promo_player_response) def delete_by_product_id(product_id): """Delete all the promo players associated with the specified product. Args: product_id (int): the product ID. Returns: response.Response: containing a dict with the list of deleted items. """ return promo_player.delete_by_product_id(product_id) def get_tracks_for_promo_player(promo_player_response): """Get the promo player tracks for a promo player. Args: promo_player_response (response.Response): containing the promo player. Returns: response.Response: containing the promo player with the tracks. """ if not promo_player_response: return promo_player_response promo_player = promo_player_response.message tracks_response = promo_player_track.get_by_promo_player_id( promo_player['promo_player_id']) if not tracks_response: return tracks_response tracks = tracks_response.message['items'] promo_player['tracks'] = tracks return response.Response(promo_player)