"""Validation of `stores` parameter.""" from oto import response as oto_response from analytics import consts from analytics.consts import error from analytics.models import streams def _validate_stores(stores, available_stores): """Validate list of stores. If stores list is empty, treat it as a list of all available stores for (sub)account Args: stores (iterable[str]): List of stores. available_stores (list[str]): List of stores. Returns: oto_response.Response: parameters or error message. """ stores = sorted(filter(None, set(stores))) if not stores: return oto_response.Response(available_stores) if not all(store_id.isdecimal() for store_id in stores): return oto_response.create_error_response( code=error.VALIDATION_ERROR_CODE, message=error.INVALID_STORE_IDS_FORMAT_MESSAGE) if not set(stores).issubset(available_stores): return oto_response.create_error_response( code=error.VALIDATION_ERROR_CODE, message=error.INVALID_STORE_IDS_VALUES_MESSAGE) return oto_response.Response(stores) def validate_streams_stores(stores): """Validate list of stores for sos endpoint. Args: stores (iterable[str]): List of stores. Returns: oto_response.Response: parameters or error message. """ available_stores = streams.Store.get_available_stores() return _validate_stores(stores, available_stores) def validate_demographic_stores(stores): """Validate list of stores for demographic endpoint. Args: stores (iterable[str]): List of stores. Returns: oto_response.Response: parameters or error message. """ available_stores = { str(store_id) for store_id in consts.analytics.AVAILABLE_DEMOGRAPHICS_STORE_IDS} return _validate_stores(stores, available_stores)