import datetime from typing import Any, Dict, List from flask import Request, request from analytics.constants.distributors import DISTRIBUTORS from analytics.constants.parameters import ALL_TIME from analytics.logic import data_availability def _parse_int_list(req: Request, param_name: str) -> List[int]: values = req.args.getlist(param_name) return [int(value) for value in values if int(value) > 0] def _get_csv_list(param_name: str) -> List[str]: """Return a query-string list param from the current request. Accepts both repeated keys (``?k=a&k=b``) and comma-separated values (``?k=a,b``). The comma form lets callers keep large filters such as ``country_code`` under the edge query-string size limit, while repeated keys keep working unchanged. """ return [ item for raw in request.args.getlist(param_name) for item in raw.split(",") if item ] def _get_query_string_params( accepted_params: List[str], previous_params: Dict[str, Any] = None ) -> Dict[str, Any]: """parse out accepted query string params from request""" params = previous_params or {} for param in accepted_params: if request.args.get(param): params[param] = request.args.get(param) return params def _get_query_string_list_params( accepted_params: Dict[str, str], previous_params: Dict[str, Any] = None ) -> Dict[str, Any]: """parse out accepted query string params from request and return a list""" params = previous_params or {} for key, value in accepted_params.items(): if request.args.get(key): params[value] = request.args.getlist(key) return params def _get_global_filters(): """Return global filters from request args. Returns: start_date (datetime.date): Start date end_date (datetime.date): End date countries (list): List of countries distributors (list): List of distributors names """ start_date = None end_date = None countries = [] store_ids = [] start_date_arg = request.args.get("start_date") end_date_arg = request.args.get("end_date") # we have some occurrences of distributors being passed as a list of strings # or as a single list distributors_str = request.args.get("distributors", DISTRIBUTORS) distributors_list_from_str = distributors_str.split(",") distributors_list = request.args.getlist("distributors") or DISTRIBUTORS.split(",") if len(distributors_list) > len(distributors_list_from_str): distributors = distributors_list else: distributors = distributors_list_from_str if request.args.get("country_code"): countries = _get_csv_list("country_code") if request.args.get("store_ids"): store_ids = [int(store_id) for store_id in request.args.getlist("store_ids")] if start_date_arg == ALL_TIME: start_date = start_date_arg if start_date_arg and ( end_date_arg or (request.args.get("days") and start_date_arg != ALL_TIME) ): if end_date_arg: if start_date_arg != ALL_TIME: start_date = datetime.datetime.strptime( start_date_arg, "%Y-%m-%d" ).date() end_date = datetime.datetime.strptime(end_date_arg, "%Y-%m-%d").date() else: start_date, end_date = data_availability.get_date_range( start_date_arg, int(request.args.get("days")) ) return countries, store_ids, start_date, end_date, distributors def user_has_full_access(permissions): """Check if a user has * access.""" if ( not permissions["permission_label_ids"] and not permissions["permission_subaccount_ids"] and not permissions["permission_artist_ids"] and not permissions["permission_label_participant_ids"] ): return True else: return False def filter_non_empty_items(params_list: list) -> list: """ Filters out empty or falsy items (example: None, '', 0, False, [], etc.) from the given list. :param params_list: given list of query parameters to filter :return: list of filtered items """ return list(filter(None, params_list))