"""Logic for Sales Goals Countries.""" from oto import response import yaml from sales_goals import config from sales_goals.constants import models from sales_goals.models import ows_territories from sales_goals.utils import logic_utils def get_countries(user_id=None): """Get all the Sales Goals countries. Logic for getting all the countries which it is possible to specify the goals for. Args: user_id (str): user id from request header. Return: Response: JSON object representing sales goal countries list in response body. """ all_countries = get_all_countries_available(user_id) countries = [] for country in all_countries: if 'hide_physical' in country and country['hide_physical']: continue countries.append(country) return response.Response({'items': countries}) def get_territories(): """Get all the territories. Logic for getting all the countries which it is possible to specify the marketing highlights for. Return: Response: JSON object representing sales goal countries list in response body. """ countries = {'items': get_all_countries_available()} for country in countries['items']: country['category'] = 'Top Market' territories_response = ows_territories.get_territories() if not territories_response: return territories_response territories = territories_response.message non_dupe_territories = remove_territories_that_are_already_markets( countries, territories) countries['items'].extend(non_dupe_territories) return response.Response(countries) def remove_territories_that_are_already_markets(countries, territories): """Remove territories that already exist as markets.""" market_ids = [country['country_id'] for country in countries['items']] return [territory for territory in territories if territory['country_id'] not in market_ids] def get_all_countries_available(user_id=None): """Get all the countries available to user.""" countries = load_countries() if not logic_utils.is_workstation_user(user_id): return countries countries_result = [] for country in countries.copy(): # for all countries where hidden_retailers is True, we show # to alw user only the store 'Other' if country[models.HIDE_RETAILERS] is True: other_store = next( store for store in country[models.STORES] if store[models.NAME] == 'Other') country[models.STORES] = [other_store] countries_result.append(country) return countries_result def get_stores_available_in_country(country_id): """Get all the Sales Goals stores for a country. Logic for getting all the store related to the provided country. Args: country_id (int): unique identifier of country. Return: Response: JSON object representing stores list in response body. """ countries = load_countries() result = next( country[models.STORES] for country in countries if country[models.COUNTRY_ID] == country_id) return response.Response({'items': result}) def get_countries_with_hidden_retailers(): """Return the list of country ids where the retailers should be hidden. Return: set: ids of countries where hide_retailers is True. """ countries = load_countries() countries_hide_retailers = { country[models.COUNTRY_ID] for country in countries if country[models.HIDE_RETAILERS]} return countries_hide_retailers def load_countries(): """Load countries from yaml file.""" with open(config.COUNTRY_DATA_PATH, 'r') as infile: return yaml.load(infile, Loader=yaml.Loader) def get_country_name_from_id(country_id): """Get the country name from the country id.""" sales_goals_countries = get_territories() country_name = None for item in sales_goals_countries.message['items']: if country_id == item['country_id']: country_name = item[models.NAME] break return country_name