def test_invalid_geo_param(self): req = self.factory.get("/", data={"geo": "france"}, HTTP_CF_IPCOUNTRY="de") assert get_country_from_request(req) == "DE" req = self.factory.get("/", data={"geo": ""}, HTTP_CF_IPCOUNTRY="de") assert get_country_from_request(req) == "DE" req = self.factory.get("/", data={"geo": "france"}) assert get_country_from_request(req) is None
def test_geo_param(self): """Country code from header should be overridden by query param for pre-prod domains.""" req = self.factory.get("/", data={"geo": "fr"}, HTTP_CF_IPCOUNTRY="de") assert get_country_from_request(req) == "FR" # should use header if at prod domain req = self.factory.get("/", data={"geo": "fr"}, HTTP_CF_IPCOUNTRY="de", HTTP_HOST="www.mozilla.org") assert get_country_from_request(req) == "DE"
def geolocate(request): """Return the country code provided by our CDN https://support.cloudflare.com/hc/en-us/articles/200168236-What-does-CloudFlare-IP-Geolocation-do- Mimics the responses from the Mozilla Location Service: https://mozilla.github.io/ichnaea/api/region.html """ country_code, source = get_country_from_request(request) if country_code is None: return JsonResponse( { "error": { "errors": [{ "domain": "geolocation", "reason": "notFound", "message": "Not found", }], "code": 404, "message": "Not found", } }, status=404) return JsonResponse({ 'country_code': country_code, 'data_source': source, })
def vpn_download_page(request): template_name = "products/vpn/download.html" country = get_country_from_request(request) ftl_files = ["products/vpn/download", "products/vpn/shared"] windows_download_url = f"{settings.VPN_ENDPOINT}r/vpn/download/windows" mac_download_url = f"{settings.VPN_ENDPOINT}r/vpn/download/mac" linux_download_url = f"{settings.VPN_ENDPOINT}r/vpn/download/linux" android_download_url = "https://play.google.com/store/apps/details?id=org.mozilla.firefox.vpn" ios_download_url = "https://apps.apple.com/us/app/firefox-private-network-vpn/id1489407738" block_download = country in settings.VPN_BLOCK_DOWNLOAD_COUNTRY_CODES context = { "windows_download_url": windows_download_url, "mac_download_url": mac_download_url, "linux_download_url": linux_download_url, "android_download_url": android_download_url, "ios_download_url": ios_download_url, "connect_devices": settings.VPN_CONNECT_DEVICES, "block_download": block_download, } return l10n_utils.render(request, template_name, context, ftl_files=ftl_files)
def get_template_names(self): country_code = get_country_from_request(self.request) template = self.geo_template_names.get(country_code) if template: return [template] return super().get_template_names()
def firefox_os_geo_redirect(request): country = get_country_from_request(request) version = settings.FIREFOX_OS_COUNTRY_VERSIONS.get( country, settings.FIREFOX_OS_COUNTRY_VERSIONS['default'] ) return HttpResponseRedirect(reverse('firefox.os.ver.{0}'.format(version)))
def firefox_os_geo_redirect(request): locale = l10n_utils.get_locale(request) if locale == "en-US": version = "2.5" else: country = get_country_from_request(request) version = settings.FIREFOX_OS_COUNTRY_VERSIONS.get(country, settings.FIREFOX_OS_COUNTRY_VERSIONS["default"]) return HttpResponseRedirect(reverse("firefox.os.ver.{0}".format(version)))
def get_redirect_url(self, *args, **kwargs): country_code, _ = get_country_from_request(self.request) url = self.geo_urls.get(country_code, self.default_url) if re.match(r'https?://', url, re.I): self.url = url else: self.pattern_name = url return super().get_redirect_url(*args, **kwargs)
def firefox_os_geo_redirect(request): locale = l10n_utils.get_locale(request) if locale == 'en-US': version = '2.5' else: country = get_country_from_request(request) version = settings.FIREFOX_OS_COUNTRY_VERSIONS.get( country, settings.FIREFOX_OS_COUNTRY_VERSIONS['default']) return HttpResponseRedirect(reverse('firefox.os.ver.{0}'.format(version)))
def get_template_names(self): locale = l10n_utils.get_locale(self.request) country = get_country_from_request(self.request) variant = self.request.GET.get("v", None) experience = self.request.GET.get("xv", None) # ensure variant matches pre-defined value if variant not in self.variations: variant = None if country == "RU" and locale == "ru" and switch("firefox-yandex"): template = "firefox/new/desktop/download_yandex.html" elif ftl_file_is_active("firefox/new/desktop") and experience != "basic": template = "firefox/new/desktop/download.html" else: template = "firefox/new/basic/base_download.html" return [template]
def vpn_landing_page(request): template_name = "products/vpn/landing.html" ftl_files = ["products/vpn/landing", "products/vpn/shared"] available_countries = settings.VPN_AVAILABLE_COUNTRIES country = get_country_from_request(request) vpn_available_in_country = country in settings.VPN_COUNTRY_CODES attribution_available_in_country = country in settings.VPN_AFFILIATE_COUNTRIES vpn_affiliate_attribution_enabled = vpn_available_in_country and attribution_available_in_country and switch( "vpn-affiliate-attribution") context = { "vpn_available": vpn_available_in_country, "available_countries": available_countries, "connect_servers": settings.VPN_CONNECT_SERVERS, "connect_countries": settings.VPN_CONNECT_COUNTRIES, "connect_devices": settings.VPN_CONNECT_DEVICES, "vpn_affiliate_attribution_enabled": vpn_affiliate_attribution_enabled, } return l10n_utils.render(request, template_name, context, ftl_files=ftl_files)
def set_country(request, token): """Allow a user to set their country""" initial = {} countrycode, _ = get_country_from_request(request) if countrycode: initial['country'] = countrycode.lower() form = CountrySelectForm('en-US', data=request.POST or None, initial=initial) if form.is_valid(): try: basket.request('post', 'user-meta', data=form.cleaned_data, token=token) except basket.BasketException: log.exception("Error updating user's country in basket") messages.add_message(request, messages.ERROR, general_error) else: return redirect(reverse('newsletter.country_success')) return l10n_utils.render(request, 'newsletter/country.html', {'form': form})
def get_template_names(self): country = get_country_from_request(self.request) locale = l10n_utils.get_locale(self.request) version = self.kwargs.get("version") or "" variant = self.request.GET.get("v", None) # ensure variant matches pre-defined value if variant not in self.variations: variant = None oldversion = self.request.GET.get("oldversion", "") # old versions of Firefox sent a prefixed version if oldversion.startswith("rv:"): oldversion = oldversion[3:] channel = detect_channel(version) if channel == "nightly": template = "firefox/nightly/whatsnew.html" elif channel == "developer": if show_102_dev_whatsnew(version): if switch("firefox-developer-whatsnew-mdnplus" ) and ftl_file_is_active( "firefox/whatsnew/whatsnew-developer-mdnplus"): template = "firefox/developer/whatsnew-mdnplus.html" else: template = "firefox/developer/whatsnew.html" elif show_57_dev_whatsnew(version): template = "firefox/developer/whatsnew.html" else: template = "firefox/whatsnew/index.html" elif version.startswith("102."): if locale.startswith("en"): template = "firefox/whatsnew/whatsnew-fx102-en.html" elif locale == "de": template = "firefox/whatsnew/whatsnew-fx102-de.html" elif locale == "fr": template = "firefox/whatsnew/whatsnew-fx102-fr.html" else: template = "firefox/whatsnew/index.html" elif version.startswith("101."): if locale.startswith("en"): if variant == "2": template = "firefox/whatsnew/whatsnew-fx101-vpn-mobile-unfree-en.html" elif variant == "3": template = "firefox/whatsnew/whatsnew-fx101-vpn-mobile.html" else: template = "firefox/whatsnew/whatsnew-fx101-vpn-en.html" elif locale == "de": template = "firefox/whatsnew/whatsnew-fx101-vpn-de.html" elif locale == "fr": template = "firefox/whatsnew/whatsnew-fx101-vpn-fr.html" elif locale in ["es-ES", "fi", "it", "nl", "sv-SE" ] and ftl_file_is_active( "firefox/whatsnew/whatsnew-101-vpn-mobile"): template = "firefox/whatsnew/whatsnew-fx101-vpn-mobile.html" else: template = "firefox/whatsnew/index.html" elif version.startswith("99."): if locale.startswith("en"): if variant == "2": template = "firefox/whatsnew/whatsnew-fx99-en-rally.html" else: template = "firefox/whatsnew/whatsnew-fx99-en.html" elif locale == "de": template = "firefox/whatsnew/whatsnew-fx99-de.html" elif locale == "fr": template = "firefox/whatsnew/whatsnew-fx99-fr.html" else: template = "firefox/whatsnew/index.html" elif version.startswith("98."): if locale.startswith("en"): if country in ["SE", "FI"]: template = "firefox/whatsnew/whatsnew-fx98-vpn-eu.html" else: template = "firefox/whatsnew/whatsnew-fx98-vpn-en.html" elif ftl_file_is_active("firefox/whatsnew/whatsnew-98-vpn-eu" ) and country in ["SE", "FI"]: template = "firefox/whatsnew/whatsnew-fx98-vpn-eu.html" elif locale == "de": template = "firefox/whatsnew/whatsnew-fx98-mobile-de.html" elif locale == "fr": template = "firefox/whatsnew/whatsnew-fx98-mobile-fr.html" else: template = "firefox/whatsnew/index.html" elif version.startswith("97.") and locale == "de": template = "firefox/whatsnew/whatsnew-fx97-de.html" elif version.startswith("97.") and locale == "fr": template = "firefox/whatsnew/whatsnew-fx97-fr.html" elif version.startswith("97.") and locale.startswith("en"): template = "firefox/whatsnew/whatsnew-fx97-en.html" else: if show_default_account_whatsnew(version) and ftl_file_is_active( "firefox/whatsnew/whatsnew-account"): template = "firefox/whatsnew/index-account.html" else: template = "firefox/whatsnew/index.html" # do not promote Mozilla VPN in excluded countries. if country in settings.VPN_EXCLUDED_COUNTRY_CODES and template in self.vpn_excluded_templates: template = "firefox/whatsnew/index-account.html" # return a list to conform with original intention return [template]
def test_alternate_geo_header_order(self): """Country code from alternate request header should win""" req = self.factory.get("/", HTTP_CF_IPCOUNTRY="de", HTTP_CLOUDFRONT_VIEWER_COUNTRY="fr") assert get_country_from_request(req) == "FR"
def vpn_available(request): country = get_country_from_request(request) country_list = settings.VPN_COUNTRY_CODES return country in country_list
def test_geo_no_header(self): """Country code when header absent should be None""" req = self.factory.get("/") assert get_country_from_request(req) is None
def test_geo_header(self): """Country code from request header should work""" req = self.factory.get("/", HTTP_CF_IPCOUNTRY="de") assert get_country_from_request(req) == "DE"
def get_template_names(self): country = get_country_from_request(self.request) locale = l10n_utils.get_locale(self.request) version = self.kwargs.get("version") or "" variant = self.request.GET.get("v", None) # ensure variant matches pre-defined value if variant not in self.variations: variant = None oldversion = self.request.GET.get("oldversion", "") # old versions of Firefox sent a prefixed version if oldversion.startswith("rv:"): oldversion = oldversion[3:] channel = detect_channel(version) if channel == "nightly": template = "firefox/nightly/whatsnew.html" elif channel == "developer": if show_57_dev_whatsnew(version): template = "firefox/developer/whatsnew.html" else: template = "firefox/whatsnew/index.html" elif version.startswith("96.") and locale == "de": template = "firefox/whatsnew/whatsnew-fx96-de.html" elif version.startswith("96.") and locale == "fr": template = "firefox/whatsnew/whatsnew-fx96-fr.html" elif version.startswith("96.") and locale.startswith("en"): if variant == "2": template = "firefox/whatsnew/whatsnew-fx96-en-qr.html" elif variant == "3": template = "firefox/whatsnew/whatsnew-fx96-en-rally.html" else: template = "firefox/whatsnew/whatsnew-fx96-en-s2d.html" elif version.startswith("95.") and locale == "de": template = "firefox/whatsnew/whatsnew-fx95-de.html" elif version.startswith("95.") and locale.startswith("en"): template = "firefox/whatsnew/whatsnew-fx95-en.html" elif version.startswith("93.") and locale.startswith("en-"): template = "firefox/whatsnew/whatsnew-fx93-en.html" elif version.startswith("93.") and locale == "de": template = "firefox/whatsnew/whatsnew-fx93-de.html" elif version.startswith("93.") and locale == "fr": template = "firefox/whatsnew/whatsnew-fx93-fr.html" elif version.startswith("93.") and locale.startswith("es-"): template = "firefox/whatsnew/whatsnew-fx93-es.html" elif version.startswith("93.") and locale == "it": template = "firefox/whatsnew/whatsnew-fx93-it.html" elif version.startswith("93.") and locale == "nl": template = "firefox/whatsnew/whatsnew-fx93-nl.html" else: if show_default_account_whatsnew(version) and ftl_file_is_active("firefox/whatsnew/whatsnew-account"): template = "firefox/whatsnew/index-account.html" else: template = "firefox/whatsnew/index.html" # do not promote Mozilla VPN in China. if country == "CN" and template in self.china_excluded_templates: template = "firefox/whatsnew/index-account.html" # return a list to conform with original intention return [template]
def geo(request): return {"country_code": get_country_from_request(request)}