"""Logic for Marketing Highlights.""" from oto import response from marketing.constants import marketing from marketing.logic import entities from marketing.models import highlight from marketing.models import ows_product def get_highlights( entity_type, entity_id, client=None, offset=None, limit=None, mkt_program_id=None): """Get marketing highlights for a specific entity. Args: entity_type (str): Describes whether marketing highlight is for a vendor, artist, release or track. entity_id (int): Unique identifier for the given entity. client (str): Describes whether records added/updated from oa or alw. offset (int): record index used to start. limit (int): number of records to fetch. Returns: response.Response: object containing paginated result set if available else not found response. """ return highlight.get_marketing_highlights( entity_type, entity_id, client=client, offset=offset, limit=limit, mkt_program_id=mkt_program_id) def create_highlight(data): """Create a new highlight item. Args: data (dict): dataset for the marketing highlight to create. Returns: Response: the result of the create operation. """ entity_verification = entities.verify_existence( data.get('entity'), data.get('entity_id')) if not entity_verification: return entity_verification return highlight.create_marketing_highlight(**data) def update_highlight(highlight_id, data): """Update a highlight. Args: highlight_id (int): the highlight id. data (dict): dataset to update the id. Returns: Response: the result of the update operation (contains the new entity information.) """ return highlight.update_marketing_highlight(highlight_id, **data) def delete_highlight(highlight_id): """Delete a highlight. Args: highlight_id (int): the highlight's id. Returns: Response: the result of the delete operation (contains the new entity information.) """ return highlight.delete_marketing_highlight(highlight_id) def get_highlight(highlight_id): """Get a marketing highlight by its id. Args: highlight_id (int): the marketing highlight's id. Returns: Response: contains the highlight information. """ return highlight.get_marketing_highlight_by_id(highlight_id) def is_owner(highlight_id, account_type=None, account_id=None): """Check if an account is an owner of an highlight. Args: highlight_id (int): the highlight id. account_type (str): the account's type. account_id (int): the account's id. Returns: Returns: Response: the result of the ownership check. """ current_highlight = get_highlight(highlight_id) if not current_highlight: return current_highlight data = current_highlight.message return entities.is_owner( data.get('entity'), data.get('entity_id'), account_type=account_type, account_id=account_id) def delete_highlights_by_entity(entity_type, entity_id): """Delete a highlight by entity. Args: entity_type (str): Describes whether marketing highlight is for a vendor, artist, release or track. entity_id (str): Unique identifier for the given entity. Returns: Response: the result of the delete operation (contains the new entity information.) """ return highlight.delete_highlight_by_entity( entity_type, entity_id) def _validate_products_for_copy(*products): """Check product ownership and types for copying. Args: products: products to validate Returns: response.Response: success if valid, error if not. """ product_ownerships = {(product['vendor_id'], product['subaccount_id']) for product in products} if len(product_ownerships) > 1: return response.Response(status=403, message='Ownership check failed.') if any(product for product in products if product['context_type'] != 'physical'): return response.Response( status=400, message='Can only copy physical products.') return response.Response() def copy_product_highlights(product_id, new_product_id): """Copy a product highlights. Args: product_id (str): the product id we are copying highlights from. Returns: Response: the result of the copy operation. """ product_response = ows_product.get_product(product_id) if not product_response: return product_response product = product_response.message new_product_response = ows_product.get_product(new_product_id) if not new_product_response: return new_product_response new_product = new_product_response.message validation_response = _validate_products_for_copy(product, new_product) if not validation_response: return validation_response existing_product_highlights_response = get_highlights( 'release', product_id, limit=1, mkt_program_id=marketing.PROGRAM_MARKETING_HIGHLIGHTS) if not existing_product_highlights_response: return response.Response() data = existing_product_highlights_response.message highlight = data['items'][0] if not highlight['description']: return response.Response() highlight.pop('highlight_id') highlight['entity_id'] = new_product_id new_highlight = create_highlight(highlight) if not new_highlight: return new_highlight return response.Response(status=201)