def migrate_to_2char_country_names(apps, schema_editor): Site = apps.get_model("workshops", "Site") Airport = apps.get_model("workshops", "Airport") # hard-coded list of mappings that fail to convert mapping = { 'United-States': 'US', 'United-Kingdom': 'GB', 'European-Union': 'EU', None: None } for site in Site.objects.all(): country = site.country if country in mapping: site.country = mapping[country] else: country = country.replace('-', ' ') site.country = countries.by_name(country) site.save() for airport in Airport.objects.all(): country = airport.country if country in mapping: airport.country = mapping[country] else: country = country.replace('-', ' ') airport.country = countries.by_name(country) airport.save()
def test_fetch_by_name_custom(self): with self.settings(COUNTRIES_ONLY={ "AU": { "name": "Oz" }, "NZ": { "names": ["New Zealand", "Hobbiton"] }, }): self.assertEqual(countries.by_name("Oz"), "AU") self.assertEqual(countries.by_name("New Zealand"), "NZ") self.assertEqual(countries.by_name("Hobbiton"), "NZ")
def get_country_code_by_name(name): code = countries.by_name(name) if not code: code = TRANSFERTO_COUNTRY_NAMES.get(name, None) return code if code else None
def handle(self, *args, **options): country_weightings = {} with open(args[1], 'wb') as output: with open(args[0]) as csvfile: writer = csv.writer(output) reader = csv.DictReader(csvfile) writer.writerow( ['Country', 'Region', 'Print', 'Radio', 'TV', 'Online']) for row in reader: if row['Country'] == "Ivory Coast": row['Country'] = u"C\xf4te d'Ivoire" code = countries.by_name(row['Country']) region = CountryRegion.objects.get(country=code).region if not code: self.stdout.write('Country not found %s' % row['Country']) break writer.writerow([ code, region, row['Print'], row['Radio'], row['TV'], row['Online'] ]) country_weightings[code] = { 'Region': region, 'Print': row['Print'], 'Radio': row['Radio'], 'Television': row['TV'], 'Internet': row['Online'] } pprint(country_weightings)
def load_business_area(): url = "{}businessareas".format(config.INSIGHT_URL) response = requests.get( url, headers={'Ocp-Apim-Subscription-Key': config.INSIGHT_SUB_KEY}) if response.status_code in [401, 403]: raise PermissionError('%s - %s: Invalid credentials' % (url, response.status_code)) data = response.json()['ROWSET']['ROW'] results = SyncResult() for entry in data: defaults = { 'name': entry['BUSINESS_AREA_NAME'], 'country': countries.by_name(entry['BUSINESS_AREA_NAME']), 'region': Region.objects.get_or_create( code=entry['REGION_CODE'], defaults={'name': entry['REGION_NAME']})[0] } area, created = BusinessArea.objects.update_or_create( code=entry['BUSINESS_AREA_CODE'], defaults=defaults) results.log(area, created) logger.info(f"BusinessArea sync completed: {results}") return results
def handle(self, *args, **options): country_weightings = {} with open(args[1], 'wb') as output: with open(args[0]) as csvfile: writer = csv.writer(output) reader = csv.DictReader(csvfile) writer.writerow(['Country', 'Region', 'Print', 'Radio', 'TV', 'Online']) for row in reader: if row['Country'] == "Ivory Coast": row['Country'] = u"C\xf4te d'Ivoire" code = countries.by_name(row['Country']) region = CountryRegion.objects.get(country=code).region if not code: self.stdout.write('Country not found %s' % row['Country']) break writer.writerow([ code, region, row['Print'], row['Radio'], row['TV'], row['Online'] ]) country_weightings[code] = { 'Region': region, 'Print': row['Print'], 'Radio': row['Radio'], 'Television': row['TV'], 'Internet': row['Online'] } pprint(country_weightings)
def populate_country_region(apps, schema_editor): from django_countries import countries CountryRegion = apps.get_model("forms", "CountryRegion") db_alias = schema_editor.connection.alias country_region_objs = CountryRegion.objects.using(db_alias).all() region_map = {} # Map country codes to regions for country_region in COUNTRY_REGION: code = countries.by_name(country_region[0]) if code: if country_region[1] in region_map: region_map[country_region[1]].append(code) else: region_map[country_region[1]] = [code] # Create CountryRegion for unmapped countries if not country_region_objs.filter(country="ZZ"): CountryRegion.objects.using(db_alias).create(country="ZZ", region="Unmapped") # Create CountryRegion objects for supplied pairs for region, country_list in region_map.iteritems(): for country in country_list: # Is this check necessary? if not country_region_objs.filter(country=country): CountryRegion.objects.using(db_alias).create(country=country, region=region)
def populate_country_region(apps, schema_editor): from django_countries import countries CountryRegion = apps.get_model("forms", "CountryRegion") db_alias = schema_editor.connection.alias country_region_objs = CountryRegion.objects.using(db_alias).all() region_map = {} # Map country codes to regions for country_region in COUNTRY_REGION: code = countries.by_name(country_region[0]) if code: if country_region[1] in region_map: region_map[country_region[1]].append(code) else: region_map[country_region[1]] = [code] # Create CountryRegion for unmapped countries if not country_region_objs.filter(country="ZZ"): CountryRegion.objects.using(db_alias).create( country="ZZ", region="Unmapped") # Create CountryRegion objects for supplied pairs for region, country_list in region_map.iteritems(): for country in country_list: # Is this check necessary? if not country_region_objs.filter(country=country): CountryRegion.objects.using(db_alias).create( country=country, region=region)
def flag(country): country_code = countries.by_name(country) if not country_code: if country == 'United Kingdom': country_code = 'GB' return render_to_string('dance/_flag.html', { 'country': country, 'code': country_code })
def get_region(field: Field) -> Optional[str]: request = field.context['request'] if request.user.profile.country: return request.user.profile.country.code if field.parent.instance and field.parent.instance.country: code = countries.by_name(field.parent.instance.country) if code: return code if hasattr(request, 'geo_data') and not request.geo_data.is_unknown: return request.geo_data.country_code return None
def get_region_map(CountryRegion): from django_countries import countries region_map = {} # Map new country code(s) to regions for country_region in COUNTRY_REGION: code = countries.by_name(country_region[0]) if code: if country_region[1] in region_map: region_map[country_region[1]].append(code) else: region_map[country_region[1]] = [code] return region_map
def country_code_by_name(name): """Converts given country name to corresponding ISO-3166 alpha-2 country code. :param name: human readable country name :type name: str :returns: ISO-3166 alpha-2 country code :rtype: str """ if name is None: return None elif name == 'United States': return 'US' elif name == 'Cote d\'Ivoire': return 'CI' else: code = countries.by_name(name) return code if code else None
def assert_company(self, company, data): self.assertEqual(company.number, data['company_number']) self.assertEqual(company.name, data['company_name']) registered_office_address = data.get('registered_office_address', {}) self.assertEqual(company.address_line1, registered_office_address.get('address_line_1', '')) self.assertEqual(company.address_line2, registered_office_address.get('address_line_2', '')) self.assertEqual(company.postcode, registered_office_address.get('postal_code', '')) self.assertEqual(company.region, registered_office_address.get('region', '')) self.assertEqual(company.locality, registered_office_address.get('locality', '')) country_of_origin = data.get('country_of_origin') if not country_of_origin: self.assertEqual(company.country, country_of_origin) else: self.assertEqual(company.country.code, countries.by_name(data['country_of_origin'])) self.assertEqual(company.company_type, data['type']) self.assertEqual(company.status, data['company_status']) self.assert_date(company.date_of_creation, data.get('date_of_creation')) self.assert_date(company.date_of_dissolution, data.get('date_of_dissolution')) self.assertTrue(company.raw)
def load_business_area(): url = "{}GetBusinessAreaList_JSON".format(config.INSIGHT_URL) response = requests.get(url, auth=get_vision_auth()).json() data = json.loads(response['GetBusinessAreaList_JSONResult']) results = SyncResult() for entry in data: defaults = { 'name': entry['BUSINESS_AREA_NAME'], 'long_name': entry['BUSINESS_AREA_LONG_NAME'], 'country': countries.by_name(entry['BUSINESS_AREA_NAME']), 'region': Region.objects.get_or_create( code=entry['REGION_CODE'], defaults={'name': entry['REGION_NAME']})[0] } area, created = BusinessArea.objects.update_or_create( code=entry['BUSINESS_AREA_CODE'], defaults=defaults) results.log(area, created) logger.info(f"BusinessArea sync completed: {results}") return results
def countries_(country): """ return the country's ISO names Args: country (str): the country name Returns: (str) country ISO name """ countries.countries['US'] = 'USA' countries.countries['GB'] = 'UK' countries.countries['AE'] = 'UAE' countries.countries['PS'] = 'Palestine' countries.countries['CD'] = 'DRC' countries.countries['CI'] = 'Ivory Coast' countries.countries['BL'] = 'St. Barth' countries.countries['VI'] = 'U.S. Virgin Islands' countries.countries['VA'] = 'Vatican City' countries.countries['CAR'] = 'CAR' countries.countries['KR'] = 'S. Korea' countries.countries['FO'] = 'Faeroe Islands' countries.countries['MF'] = 'Saint Martin' countries.countries['SVG'] = 'St. Vincent Grenadines' return countries.by_name(country)
def test_fetch_by_name_no_match(self): self.assertEqual(countries.by_name('Neverland'), '')
def test_fetch_by_name_i18n(self): code = countries.by_name('Estados Unidos', language='es') self.assertEqual(code, 'US')
def test_fetch_by_name_old_case_insensitive(self): code = countries.by_name('Czech republic') self.assertEqual(code, 'CZ')
def test_fetch_by_name(self): self.assertEqual(countries.by_name('United States'), 'US')
def test_fetch_by_name(self): code = countries.by_name("United States of America") self.assertEqual(code, "US")
def test_fetch_by_name(self): code = countries.by_name('United States of America') self.assertEqual(code, 'US')
def test_fetch_by_name_i18n(self): code = countries.by_name("Estados Unidos", language="es") self.assertEqual(code, "US")
def test_fetch_by_name_no_match(self): self.assertEqual(countries.by_name("Neverland"), "")
def test_fetch_by_name_old(self): code = countries.by_name("Czech Republic") self.assertEqual(code, "CZ")
def test_fetch_by_name_case_insensitive(self): code = countries.by_name("bRuNeI") self.assertEqual(code, "BN")
def test_fetch_by_name_regex(self): codes = countries.by_name(r"([ao])\1", regex=True) # Cook Islands, Cameroon, Sint Maarten self.assertEqual(set(codes), {"CK", "CM", "SX"})
def test_fetch_by_name_official(self): code = countries.by_name("brunei darussalam") self.assertEqual(code, "BN")
def test_fetch_by_name_old(self): code = countries.by_name('Czech Republic') self.assertEqual(code, 'CZ')
def handle(self, *args, **options): # Mapping from mispelled country names to the correct ones. COUNTRY_NAME_MAP = { 'European Community': 'European Union', # 'European Union': None, 'Global': None, 'Korea': 'North Korea', 'Kosovo': None, 'Morrocco': 'Morocco', 'Netherland': 'Netherlands', 'The Netherlands': 'Netherlands', 'Russian Federation': 'Russia', 'UK': 'United Kingdom', 'England': 'United Kingdom', 'United States': 'United States of America', 'Vatican City': None } EUROPEAN_UNION_COUNTRIES = ('Austria', 'Belgium', 'Bulgaria', 'Croatia', 'Cyprus', 'Czech Republic', 'Denmark', 'Estonia', 'Finland', 'France', 'Germany', 'Greece', 'Hungary', 'Ireland', 'Italy', 'Latvia', 'Lithuania', 'Luxembourg', 'Malta', 'Netherlands', 'Poland', 'Portugal', 'Romania', 'Slovakia', 'Slovenia', 'Spain', 'Sweden', 'United Kingdom') with transaction.atomic(): print 'Correcting country names' for country in models.Country.objects.all(): if country.label in COUNTRY_NAME_MAP: new_label = COUNTRY_NAME_MAP[country.label] print '%s -> %s' % (country.label, new_label) if new_label is not None: # Move all studies to the new country. new_country, _ = models.Country.objects.get_or_create( label=new_label) for study in country.study_set.all(): new_country.study_set.add(study) country.delete() print 'Splitting European Union' eu = models.Country.objects.get(label='European Union') for label in EUROPEAN_UNION_COUNTRIES: country, _ = models.Country.objects.get_or_create(label=label) for study in eu.study_set.all(): country.study_set.add(study) eu.delete() print 'Adding country codes' for country in models.Country.objects.all(): code = countries.by_name(country.label) if code == '': print 'Invalid: %s (%d)' % (country, country.study_set.count()) country.delete() continue alpha3 = countries.alpha3(code) assert alpha3 is not None country.alpha3 = alpha3 country.save()
def test_fetch_by_name_case_insensitive(self): code = countries.by_name("United states of America") self.assertEqual(code, "US")
def test_fetch_by_name(self): code = countries.by_name("Brunei") self.assertEqual(code, "BN")
def test_fetch_by_name_old_case_insensitive(self): code = countries.by_name("Czech republic") self.assertEqual(code, "CZ")
if CHECK_ICAO_CODE: reference_codes = load_reference_icao_codes() suspicious_codes = set() for aerodrome in load_bitbringers_data(): aerodrome_icao_code = aerodrome["icao"] if CHECK_ICAO_CODE and aerodrome_icao_code not in reference_codes: suspicious_codes.add(aerodrome_icao_code) aerodrome_name = aerodrome["name"] aerodrome_city = aerodrome["city"] aerodrome_country = countries.by_name(aerodrome["country"]) if not any([aerodrome_name, aerodrome_city, aerodrome_country]): logging.warn(f"Empty name/city/country for {aerodrome_icao_code}!") obj, created = Aerodrome.objects.update_or_create( icao_code=aerodrome_icao_code, defaults={ "name": aerodrome_name, "city": aerodrome_city, "country": aerodrome_country, "latitude": aerodrome["lat"], "longitude": aerodrome["lng"], "elevation": aerodrome["elev"], "priority": aerodrome["distance"], })
def flag(country): country_code = countries.by_name(country) if not country_code: if country == 'United Kingdom': country_code = 'GB' return render_to_string('dance/_flag.html', {'country': country, 'code': country_code})
def test_fetch_by_name_case_insensitive(self): code = countries.by_name('United states of America') self.assertEqual(code, 'US')
def from_gsx(cls, sn, device=None, cached=True, user=None): """ Initialize new Device with warranty info from GSX Or update existing one """ sn = sn.upper() cache_key = 'device-%s' % sn # Only cache unsaved devices if cached and device is None: if cache.get(cache_key): return cache.get(cache_key) arg = gsxws.validate(sn) if arg not in ("serialNumber", "alternateDeviceId",): raise ValueError(_(u"Invalid input for warranty check: %s") % sn) product = gsxws.Product(sn) if user and user.location: ship_to = user.location.gsx_shipto else: gsx_act = GsxAccount.get_default_account() ship_to = gsx_act.ship_to wty = product.warranty(ship_to=ship_to) model = product.model() if device is None: # serialNumber may sometimes come back empty serial_number = wty.serialNumber or sn device = Device(sn=serial_number) from servo.lib.utils import empty if empty(device.notes): device.notes = wty.notes or '' device.notes += wty.csNotes or '' device.has_onsite = product.has_onsite device.is_vintage = product.is_vintage device.description = product.description device.fmip_active = product.fmip_is_active device.slug = slugify(device.description) device.configuration = wty.configDescription or '' device.purchase_country = countries.by_name(wty.purchaseCountry) device.config_code = model.configCode device.product_line = model.productLine.replace(" ", "") device.parts_and_labor_covered = product.parts_and_labor_covered device.sla_description = wty.slaGroupDescription or '' device.contract_start_date = wty.contractCoverageStartDate device.contract_end_date = wty.contractCoverageEndDate device.onsite_start_date = wty.onsiteStartDate device.onsite_end_date = wty.onsiteEndDate if wty.estimatedPurchaseDate: device.purchased_on = wty.estimatedPurchaseDate device.image_url = wty.imageURL or '' device.manual_url = wty.manualURL or '' device.exploded_view_url = wty.explodedViewURL or '' if wty.warrantyStatus: device.set_wty_status(wty.warrantyStatus) if product.is_ios: ad = device.get_activation() device.imei = ad.imeiNumber or '' device.unlocked = product.is_unlocked(ad) device.applied_activation_policy = ad.appliedActivationDetails or '' device.initial_activation_policy = ad.initialActivationPolicyDetails or '' device.next_tether_policy = ad.nextTetherPolicyDetails or '' cache.set(cache_key, device) return device