def country_upload(request): if request.method == 'POST': dataset = Dataset() new_country = request.FILES['myfile'] imported_data = dataset.load(new_country.read(), format='xlsx') print(imported_data) for data in imported_data: if data[1] is not None: value = Country( data[0], data[1], data[2], data[3], data[4] ) value.save() return redirect('countries-list') return render(request, 'core/countries_upload.html')
def migrate(_req): response = {'status': 'True', 'message': 'Success!'} with open('core/city.list.json') as file: data = json.load(file) print('starting migration...') print('total cities: ', len(data)) for i, cityInfo in enumerate(data): city = City() city.weathermapId = cityInfo['id'] country = Country.objects.filter(name=cityInfo['country']).first() if country is None: country = Country() country.name = cityInfo['country'] country.save() city.country = country city.name = cityInfo['name'] city.save() if (i + 1) % 500 == 0: print(i + 1, ' cities sorted.') return JsonResponse(response, status=200)
def handle(self, *args, **options): response = urlopen(settings.COUNTRIES_URL) data = json.loads(response.read().decode('utf-8')) data = {item['alpha3Code']: item for item in data} added, updated = [], [] for alpha3, item in data.items(): try: country = Country.objects.get(alpha3=alpha3) update_country(country, item) country.save() updated.append(country) self.stdout.write("Updated country {}.".format(alpha3)) except Country.DoesNotExist: country = Country() update_country(country, item) country.save() added.append(country) self.stdout.write("Added country {}.".format(alpha3)) for country in added + updated: # Update languages country_languages = country.languages.all() for language_alpha2 in data[country.alpha3]['languages']: language = Language.objects.get(alpha2=language_alpha2) if language not in country_languages: country.languages.add(language) country.save() self.stdout.write("Language {} added to country {}.".format(language.alpha2, country.alpha3)) for cl in country_languages: if cl.alpha2 not in data[country.alpha3]['languages']: country.languages.remove(cl) country.save() self.stdout.write("Language {} removed from country {}.".format(cl.alpha2, country.alpha3)) # Update neighbors country_neighbors = country.neighbors.all() for neighbor_alpha3 in data[country.alpha3]['borders']: if neighbor_alpha3 not in data: continue neighbor = Country.objects.get(alpha3=neighbor_alpha3) if neighbor not in country_neighbors: country.neighbors.add(neighbor) country.save() self.stdout.write("Neighbouring Country {} added to country {}.".format(neighbor.alpha3, country.alpha3)) for cn in country_neighbors: if cn.alpha3 not in data[country.alpha3]['borders']: country.neighbors.remove(cn) country.save() self.stdout.write("Neighbouring Country {} removed from country {}.".format(cn.alpha3, country.alpha3)) self.stdout.write('\n' + ('=' * 80)) self.stdout.write(self.style.SUCCESS("Added: {}\nUpdated: {}".format(len(added), len(updated))))
"CANARY ISLANDS", "加那利群岛", 312, ), ( "SS", "SOUTH SUDAN", "南苏丹共和国", 260, ), ( "CW", "Curaçao", "库拉索", 0, ), ] for code, name, cn_name, number in countrys: country = Country.objects.filter(code=code).first() if not country: country = Country() country.code = code country.name = name country.cn_name = cn_name country.save() country.cn_name = cn_name country.number = number country.save() print "import country success"
def CrearPaisCiudad(self): print('Creando paises y ciudades.') country = Country(CountryID=1, CountryName='Alemania', CountryPrefix='+54') country.save() city = City(CityID=1, Country=country, CityName='Berlin') city.save() city = City(CityID=2, Country=country, CityName='Munich') city.save() country = Country( CountryID=2, CountryName='Argentina', CountryPrefix='+49') country.save() city = City(CityID=3, Country=country, CityName='Buenos Aires') city.save() city = City(CityID=4, Country=country, CityName='Cordoba') city.save() country = Country( CountryID=3, CountryName='Australia', CountryPrefix='+61') country.save() city = City(CityID=5, Country=country, CityName='Sidney') city.save() city = City(CityID=6, Country=country, CityName='Melbourne') city.save() city = City(CityID=7, Country=country, CityName='Canberra') city.save() country = Country(CountryID=4, CountryName='Austria', CountryPrefix='+43') country.save() city = City(CityID=8, Country=country, CityName='Viena') city.save() country = Country(CountryID=5, CountryName='Belgica', CountryPrefix='+32') country.save() city = City(CityID=9, Country=country, CityName='Bruselas') city.save() city = City(CityID=10, Country=country, CityName='Brujas') city.save() country = Country(CountryID=6, CountryName='Brasil', CountryPrefix='+55') country.save() city = City(CityID=11, Country=country, CityName='Brasilia') city.save() city = City(CityID=12, Country=country, CityName='Rio de janeiro') city.save() city = City(CityID=13, Country=country, CityName='Sao Paulo') city.save() country = Country(CountryID=7, CountryName='Bulgaria', CountryPrefix='+359') country.save() city = City(CityID=14, Country=country, CityName='Sofia') city.save() country = Country(CountryID=8, CountryName='Canada', CountryPrefix='+1') country.save() city = City(CityID=15, Country=country, CityName='Ottawa') city.save() city = City(CityID=16, Country=country, CityName='Toronto') city.save() city = City(CityID=17, Country=country, CityName='Montreal') city.save() country = Country(CountryID=9, CountryName='Chile', CountryPrefix='+56') country.save() city = City(CityID=18, Country=country, CityName='Santiago') city.save() city = City(CityID=19, Country=country, CityName='Valparaiso') city.save() city = City(CityID=20, Country=country, CityName='Valdivia') city.save() city = City(CityID=21, Country=country, CityName='Concepcion') city.save() country = Country(CountryID=10, CountryName='China', CountryPrefix='+86') country.save() city = City(CityID=22, Country=country, CityName='Pekin') city.save() city = City(CityID=23, Country=country, CityName='Shanghai') city.save() country = Country( CountryID=11, CountryName='Colombia', CountryPrefix='+57') country.save() city = City(CityID=24, Country=country, CityName='Bogota') city.save() city = City(CityID=25, Country=country, CityName='Medellin') city.save() country = Country(CountryID=12, CountryName='Croacia', CountryPrefix='+385') country.save() city = City(CityID=26, Country=country, CityName='Zagreb') city.save() country = Country( CountryID=13, CountryName='Dinamarca', CountryPrefix='+45') country.save() city = City(CityID=27, Country=country, CityName='Copenhague') city.save() country = Country(CountryID=14, CountryName='Egipto', CountryPrefix='+20') country.save() city = City(CityID=28, Country=country, CityName='El cairo') city.save() country = Country(CountryID=15, CountryName='España', CountryPrefix='+34') country.save() city = City(CityID=29, Country=country, CityName='Madrid') city.save() city = City(CityID=30, Country=country, CityName='Barcelona') city.save() country = Country(CountryID=16, CountryName='Francia', CountryPrefix='+33') country.save() city = City(CityID=31, Country=country, CityName='Paris') city.save() country = Country(CountryID=17, CountryName='Grecia', CountryPrefix='+30') country.save() city = City(CityID=32, Country=country, CityName='Atenas') city.save() country = Country(CountryID=18, CountryName='Holanda', CountryPrefix='+31') country.save() city = City(CityID=33, Country=country, CityName='Amsterdam') city.save() country = Country(CountryID=19, CountryName='India', CountryPrefix='+91') country.save() city = City(CityID=34, Country=country, CityName='Nueva Delhi') city.save() city = City(CityID=35, Country=country, CityName='Bombay') city.save() country = Country(CountryID=20, CountryName='Italia', CountryPrefix='+39') country.save() city = City(CityID=36, Country=country, CityName='Roma') city.save() city = City(CityID=37, Country=country, CityName='Milan') city.save() country = Country(CountryID=21, CountryName='Japon', CountryPrefix='+81') country.save() city = City(CityID=38, Country=country, CityName='Tokio') city.save() country = Country(CountryID=22, CountryName='Mexico', CountryPrefix='+52') country.save() city = City(CityID=39, Country=country, CityName='Ciudad de Mexico') city.save() city = City(CityID=40, Country=country, CityName='Monterrey') city.save() country = Country(CountryID=23, CountryName='Noruega', CountryPrefix='+47') country.save() city = City(CityID=41, Country=country, CityName='Oslo') city.save() country = Country(CountryID=24, CountryName='Peru', CountryPrefix='+51') country.save() city = City(CityID=42, Country=country, CityName='Lima') city.save() country = Country(CountryID=25, CountryName='Portugal', CountryPrefix='+351') country.save() city = City(CityID=43, Country=country, CityName='Lisboa') city.save() city = City(CityID=44, Country=country, CityName='Oporto') city.save() country = Country(CountryID=26, CountryName='Qatar', CountryPrefix='+974') country.save() city = City(CityID=45, Country=country, CityName='Doha') city.save() country = Country(CountryID=27, CountryName='Rusia', CountryPrefix='+7') country.save() city = City(CityID=46, Country=country, CityName='Moscu') city.save() city = City(CityID=47, Country=country, CityName='San Petersburgo') city.save() country = Country( CountryID=28, CountryName='Reino Unido', CountryPrefix='+44') country.save() city = City(CityID=48, Country=country, CityName='Londres') city.save() country = Country( CountryID=29, CountryName='Sudafrica', CountryPrefix='+27') country.save() city = City(CityID=49, Country=country, CityName='Manchester') city.save() country = Country(CountryID=30, CountryName='Suecia', CountryPrefix='+46') country.save() city = City(CityID=50, Country=country, CityName='Estocolmo') city.save() country = Country(CountryID=31, CountryName='Suiza', CountryPrefix='+41') country.save() city = City(CityID=51, Country=country, CityName='Berna') city.save() city = City(CityID=52, Country=country, CityName='Zurich') city.save() country = Country( CountryID=32, CountryName='Tailandia', CountryPrefix='+66') country.save() city = City(CityID=53, Country=country, CityName='Bangkok') city.save() country = Country(CountryID=33, CountryName='Turquia', CountryPrefix='+90') country.save() city = City(CityID=54, Country=country, CityName='Ankara') city.save() city = City(CityID=55, Country=country, CityName='Estambul') city.save() country = Country(CountryID=34, CountryName='Usa', CountryPrefix='+1') country.save() city = City(CityID=56, Country=country, CityName='Washington') city.save() city = City(CityID=57, Country=country, CityName='Los Angeles') city.save() city = City(CityID=58, Country=country, CityName='New York') city.save() city = City(CityID=59, Country=country, CityName='Miami') city.save() country = Country(CountryID=35, CountryName='Uruguay', CountryPrefix='+598') country.save() city = City(CityID=60, Country=country, CityName='Montevideo') city.save() country = Country(CountryID=36, CountryName='Vietnam', CountryPrefix='+84') country.save() city = City(CityID=61, Country=country, CityName='Hanoi') city.save() return