"""Logic related to fetching releases from database.""" from oto import response from availability.constants import stores from availability.models import product_in_store as ps def get_status_for_products(product_ids): """Retrieve release status for products using provided IDs. Args: product_ids (list): List of integer product IDs. Returns: oto.response.Response: .message with statuses on success, .errors on failure. """ model_response = ps.list_store_statuses_for_products(product_ids) if not model_response: return model_response response_message = {'items': _make_stores_data(model_response.message)} return response.Response(response_message) def _make_stores_data(products): """Convert list of ProductInStore dicts. Each product_id will get subdicts with data for each store where it exists. E.g. {'product_id': 1, 'stores': [{ store-specific data }]} Args: products (list): List of ProductInStore dicts. Returns: (iterable): List of dicts with store status data. """ result = {} for product in products: product_id = product['product_id'] result[product_id] = {'product_id': product_id, 'stores': []} for product in products: result[product['product_id']]['stores'].append({ 'store_id': product['store_id'], 'status': product['status'], 'go_live_date': product['go_live_date'], 'link': _make_storefront_url( product['store_internal_id'], product['store_id']), 'countries': product['countries'] }) return list(result.values()) def _make_storefront_url(store_internal_id, store_id): """Create storefront url using predefined store-specific template. Args: store_internal_id (str): Release ID in specific store. store_id (int): ID of store Returns: (str): release url in particular store or empty string if store_internal_id is empty. """ if not store_internal_id: return '' return stores.STOREFRONT_URLS[store_id].format( store_internal_id=store_internal_id)