def test_get_cookie_when_no_querystring(mock_get, rf): settings.COUNTRY_COOKIE_NAME = 'country' url = reverse('index') request = rf.get(url, mock_get) request.COOKIES = {'country': 'GB'} actual = helpers.get_user_country(request) assert actual == 'GB'
def home_page_context_modifier(context, request): country_code = get_user_country(request) country_name = dict(COUNTRY_CHOICES).get(country_code, '') return { 'tariffs_country': { # used for flag icon css class. must be lowercase 'code': country_code.lower(), 'name': country_name, }, 'tariffs_country_selector_form': forms.TariffsCountryForm(initial={'tariffs_country': country_code}), }
def get_context_data(self, *args, **kwargs): country_code = helpers.get_user_country(self.request) # if there is a country already detected we can hide the selector hide_country_selector = bool(country_code) country_name = dict(COUNTRY_CHOICES).get(country_code, '') country = { # used for flag icon css class. must be lowercase 'code': country_code.lower(), 'name': country_name, } country_form_kwargs = self.get_country_form_kwargs() return super().get_context_data( hide_country_selector=hide_country_selector, country=country, country_selector_form=self.country_form_class( **country_form_kwargs), *args, **kwargs)
def get_country_form_initial_data(request): return {'country': helpers.get_user_country(request).upper() or None}
def region(self): country_code = get_user_country(self.request).upper() or None if country_code in EU_COUNTRIES: return 'eu'