def clean_network(self): network = self.cleaned_data['network'] if not network: return network try: region, carrier = network.split('_') except ValueError: raise forms.ValidationError('Network {0} not in the Bango format ' 'of COUNTRY_NETWORK.'.format(network)) try: mobile_codes.alpha3(region) except KeyError: raise forms.ValidationError('Invalid country: {0}'.format(region)) self.cleaned_data['carrier'] = carrier self.cleaned_data['region'] = region return network
def build_regions_js(): """ Generate a supported regions modules for Marketplace frontend projects. Maps from region slug to gettexted region names. ex: {"fr": gettext("France"),...} """ js_module_template = open(lib_path('templates/regions.js'), 'r').read() countries = name(glob.glob(py_path('countries.py'))[0]) countries = importlib.import_module(countries) # REGION_CHOICES_SLUG: Region slug to gettext mapping. data = { 'REGION_CHOICES_SLUG': { 'restofworld': "gettext('Rest of World')" }, 'MOBILE_CODES': {} } for k, country in countries.COUNTRY_DETAILS.items(): data['REGION_CHOICES_SLUG'][country['slug'].lower()] = ( "gettext('%s')" % country['name']) # MOBILE_CODES: Mobile code to region slug mapping. for alpha3, country in countries.COUNTRY_DETAILS.items(): try: mccs = mobile_codes.alpha3(alpha3).mcc except KeyError: continue if not isinstance(mccs, list): mccs = [mccs] for mcc in mccs: data['MOBILE_CODES'][mcc] = country['slug'] # Serialize. data = json.dumps(data) # Unquote the gettexts. "gettext('France')" -> gettext("France"). It's # important to keep gettext calls with double quotes, since we have escape # sequences in them for unicode stuff. pattern = re.compile(r'"gettext\(\'(.*?)\'\)"') data = pattern.sub(r'gettext("\1")', data) # Write the data. output = js_path('regions.js') change = 'Updating' if os.path.exists(output) else 'Creating' print '{0} file: {1}'.format(change, output) open(output, 'w').write(js_module_template % data)
def build_regions_js(): """ Generate a supported regions modules for Marketplace frontend projects. Maps from region slug to gettexted region names. ex: {"fr": gettext("France"),...} """ js_module_template = open(lib_path('templates/umd.js'), 'r').read() countries = name(glob.glob(py_path('countries.py'))[0]) countries = importlib.import_module(countries) # REGION_CHOICES_SLUG: Region slug to name (non-translated - if you need # translations, better directly call the region API instead of having # 250+ translated names in your javascript). data = { 'REGION_CHOICES_SLUG': { 'restofworld': 'Rest of World' }, 'MOBILE_CODES': {} } for k, country in countries.COUNTRY_DETAILS.items(): data['REGION_CHOICES_SLUG'][country['slug'].lower()] = country['name'] # MOBILE_CODES: Mobile code to region slug mapping. for alpha3, country in countries.COUNTRY_DETAILS.items(): try: mccs = mobile_codes.alpha3(alpha3).mcc except KeyError: continue if not isinstance(mccs, list): mccs = [mccs] for mcc in mccs: data['MOBILE_CODES'][mcc] = country['slug'] # Serialize. data = json.dumps(data) # Write the data. output = js_path('regions.js') change = 'Updating' if os.path.exists(output) else 'Creating' print '{0} file: {1}'.format(change, output) open(output, 'w').write(js_module_template % data)
def test_alpha3(self): country = mobile_codes.alpha3(u'ATA') self.assertEqual(country.mcc, None)
def test_alpha3(self): country = mobile_codes.alpha3(u'CAN') self.assertEqual(country.alpha3, u'CAN')
def test_alpha3_fail(self): with pytest.raises(KeyError): mobile_codes.alpha3("XYZ")
def test_alpha3(self): country = mobile_codes.alpha3("CAN") assert country.alpha3 == "CAN"
def test_alpha3(self): country = mobile_codes.alpha3("ATA") assert country.mcc is None