def insert_countries(): print("Deleting countries...") Country.objects.all().delete() print("Inserting countries...") c = Country() c.name = "España" c.save()
def test_dispatch_and_register(self): """ Long-form test that tests creating and sending an invite, and then registering a user by following the referral link in the invitation email. """ countries = [ Country(gwno=i, name=str(i), shape={}, simpleshape={}) for i in range(5) ] for c in countries: c.save() i = Invitation(email="*****@*****.**") i.save() i.countries.set(countries) dispatchInvitation(i) m = mail.outbox[0] # Mailed flag self.assertTrue(i.mailed) # Email has proper title self.assertEqual(m.subject, settings.DEFAULT_EMAIL_TITLE) # Email contains ref. key self.assertIsNotNone(re.search(i.refkey, m.body)) r = self.client.get(i.invitationLink(), follow=True) location, *_ = r.redirect_chain[-1] soup = BeautifulSoup(r.content, features="html.parser") regform = soup.find("form") usernameInput = regform.find("input", attrs={"name": "username"}) # Email is in the username form self.assertEqual(usernameInput["value"], i.email) url = regform.action if regform.action else location method = regform.method if regform.method else "POST" getattr(self.client, method.lower())(url, { "username": usernameInput["value"], "password1": hashlib.md5(b"1234").hexdigest(), "password2": hashlib.md5(b"1234").hexdigest() }) # User was created try: u = User.objects.get(email=i.email) except User.DoesNotExist: self.fail("User was not created") # Make sure all countries were added self.assertEqual({c.pk for c in countries}, {c.pk for c in u.profile.countries.all()})
def add_countries(self): # scraps data about countries from source and stores it the DB page = requests.get(self.URL_COUNTRIES) soup = BeautifulSoup(page.content, 'html.parser') tbody = soup.find("tbody") for tr in tbody.find_all("tr"): tds = tr.find_all("td") name = (tds[0].text).strip() country_sample = Country( name = name, flag = "flag", total_infected=int((tds[1].text).replace(',', '')), infected_today=int((tds[2].text).replace(',', '')), total_deaths=int((tds[3].text).replace(',', '')), deaths_today=int((tds[4].text).replace(',', '')), total_recovered=int((tds[5].text).replace(',', '')), recovered_today=int((tds[6].text).replace(',', '')), active=int((tds[7].text).replace(',', '')), critical=int((tds[8].text).replace(',', '')), tests =int((tds[9].text).replace(',', '')), ) print(name,"...", country_sample.total_infected) db.session.add(country_sample) db.session.commit() self.add_states(name)
def insert_countries(): print("Inserting countries...") with open('data/countries.json', 'r') as file: countries_str = file.read() countries_json = json.loads(countries_str) for country_json in countries_json: country = Country() country.pk = country_json.get('pk') country.name = country_json.get('name') country.save()
def updateCountries(request): """ Updating the countries available to the system. """ if not request.user.is_staff: return JsonResponse({"status":"error","messages":"permission denied"},status=status.HTTP_403_FORBIDDEN) class CountryDataUpload(pydantic.BaseModel): small: CountryFeatureCollection large: CountryFeatureCollection try: uploaded = CountryDataUpload(**json.loads(request.body.decode())) getCountries = lambda fc: {c.properties.CNTRY_NAME for c in fc.features} assert len(uploaded.small.features) == len(uploaded.large.features) assert getCountries(uploaded.small) == getCountries(uploaded.large) except Exception as e: return JsonResponse({"status":"error","messages":str(e)},status=status.HTTP_400_BAD_REQUEST) saved = 0 updated = 0 for small,large in zip(uploaded.small.features,uploaded.large.features): try: c = Country.objects.get(gwno = small.properties.GWCODE) except Country.DoesNotExist: c = Country( gwno = small.properties.GWCODE, name = small.properties.CNTRY_NAME, iso2c = small.properties.ISO1AL2, shape = dict(large.geometry), simpleshape = dict(small.geometry) ) saved += 1 else: c.iso2c = small.properties.ISO1AL2 c.name = small.properties.CNTRY_NAME c.shape = dict(large.geometry) c.simpleshape = dict(small.geometry) updated += 1 c.save() return JsonResponse({"status":"success","saved":saved,"updated":updated})
def handle(self, *args, **options): filename = options['filename'][0] boolean_fields = ['is_deprecated', 'independent'] int_fields = [ 'record_type', 'wp_population', 'wb_year', 'region_id', 'inform_score' ] fields_to_save = [ 'name', 'name_en', 'name_es', 'name_fr', 'name_ar', 'iso', 'society_name', 'society_name_en', 'society_name_es', 'society_name_fr', 'society_name_ar', 'society_url', 'key_priorities', 'logo', 'iso3', 'url_ifrc', 'centroid', 'bbox' ] + boolean_fields + int_fields if not os.path.exists(filename): print('File does not exist. Check path?') return with open(filename) as csvfile: all_ids = [] reader = csv.DictReader(csvfile) for row in reader: id = int(row.pop('id')) all_ids.append(id) try: country = Country.objects.get(pk=id) except: country = Country() for key in row.keys(): # print(key) if key in boolean_fields: val = get_bool(row[key]) elif key in int_fields: val = get_int(row[key]) else: val = row[key] if key in fields_to_save: country.__setattr__(key, val) country.save() print('SUCCESS', country.name_en) print('done importing countries') existing_country_ids = [c.id for c in Country.objects.all()] countries_not_in_csv = list(set(existing_country_ids) - set(all_ids)) for country_id in countries_not_in_csv: c = Country.objects.get(pk=country_id) c.delete() print('deleted ids', countries_not_in_csv)
def add_country(self, feature, geom, centroid, bbox, iso, fields): # new country object country = Country() name = feature.get('NAME_ICRC') iso3 = feature.get('ISO3') region = feature.get('REGION_IFR') # get region from db region_id = Region.objects.get(name=self.region_enum[region]) if ('INDEPENDEN' in fields): independent = feature.get('INDEPENDEN') if independent == 'TRUE': country.independent = True elif independent == 'FALSE': country.independent = False else: country.independent = None if ('NATIONAL_S' in fields): country.society_name = feature.get('NATIONAL_S') country.name = name country.record_type = 1 country.iso = iso country.iso3 = iso3 country.region = region_id country.geom = geom.wkt country.centroid = centroid country.bbox = bbox # save country.save()
def test_bulk_add(self): for i, c in enumerate(("Colombia", "Syria", "Mali")): ctry = Country(name=c, shape={}, simpleshape={}, iso2c=c[:2], gwno=i) ctry.save() User.objects.create_superuser(username="******", password="******") self.client.login(username="******", password="******") data = [ { "email": "*****@*****.**", "affiliation": "a", "position": "a", "Colombia": 1, "Syria": 0, "Mali": 0 }, { "email": "*****@*****.**", "affiliation": "b", "position": "a", "Colombia": 0, "Syria": 1, "Mali": 0 }, { "email": "*****@*****.**", "affiliation": "a", "position": "b", "Colombia": 0, "Syria": 0, "Mali": 1 }, { "email": "*****@*****.**", "affiliation": "a", "position": "c", "Colombia": 0, "Syria": 1, "Mali": 1 }, { "email": "*****@*****.**", "affiliation": "b", "position": "a", "Colombia": 1, "Syria": 0, "Mali": 0 }, ] f = io.StringIO() writer = csv.DictWriter(f, fieldnames=data[0].keys()) writer.writeheader() for r in data: writer.writerow(r) cf = ContentFile(f.getvalue().encode()) postData = { "title": "myexperts.csv", "datafile": ContentFile(f.getvalue().encode()) } r = self.client.post(urls.reverse("uploadexcel"), postData, follow=True) self.assertEqual(r.status_code, 200) self.assertEqual(Invitation.objects.count(), 5) self.assertTrue("Colombia" in [ c.name for c in Invitation.objects.get(email="*****@*****.**").countries.all() ]) self.assertTrue("Colombia" not in [ c.name for c in Invitation.objects.get(email="*****@*****.**").countries.all() ])
import csv project_home="/home/aithal/Documents/" import sys,os sys.path.append(project_home) os.environ['DJANGO_SETTINGS_MODULE'] ='Project.settings' from api.models import Country, City, Hotel, RoomType, HotelRoom, RoomAvailability, HotelPhotos with open("/home/aithal/Documents/Project/data/countries.csv",'r') as countrycsv: countryreader = csv.reader(countrycsv) next(countryreader) for row in countryreader: new= Country(name=row[0]) # print(new.save()) try: new.save() except: print("Problem with line:", row) with open("/home/aithal/Documents/Project/data/cities.csv",'r') as citycsv: cityreader = csv.reader(citycsv) next(cityreader) for row in cityreader: # print(Country.objects.filter(name=row[0])[0]) new= City(name=row[1], country=Country.objects.filter(name=row[0])[0]) # print(new.save())
def handle(self, *args, **options): header = { 'X-CSCAPI-KEY': 'UGNFd2VNZWJsOXE4WEFkSHVzZUwzT1Z3aEg2VXNva3MyYWR3WEtaSw==' } base_url = 'https://raw.githubusercontent.com/dr5hn/countries-states-cities-database/master/' # fetch countries countries_url = base_url + 'countries.json' countries_data = requests.get(countries_url, headers=header).json() countries = [] for country in countries_data: countries.append( Country(name=country['name'], country_code=country['iso2'], latitude=float(country['latitude']) if country['latitude'] else None, longitude=float(country['longitude']) if country['longitude'] else None)) Country.objects.bulk_create(countries) self.stdout.write( self.style.SUCCESS( 'Countries fetched. Fetching states/districts...')) # fetch states/districts states_url = base_url + 'states.json' states_data = requests.get(states_url, headers=header).json() states = [] for state in states_data: states.append( State(name=state['name'], country=Country.objects.get( country_code=state['country_code']), state_code=state['state_code'], latitude=float(state['latitude']) if state['latitude'] else None, longitude=float(state['longitude']) if state['longitude'] else None)) State.objects.bulk_create(states, batch_size=1250) self.stdout.write( self.style.SUCCESS('States/districts fetched. Fetching cities...')) # fetch cities cities_url = base_url + 'cities.json' cities_data = requests.get(cities_url, headers=header).json() cities = [] for city in cities_data: country = Country.objects.get(country_code=city['country_code']) try: state = State.objects.get(country=country, state_code=city['state_code']) except State.DoesNotExist as e: state = None city = City( name=city['name'], country=country, state=state, latitude=float(city['latitude']) if city['latitude'] else None, longitude=float(city['longitude']) if city['longitude'] else None) cities.append(city) City.objects.bulk_create(cities, batch_size=1250) self.stdout.write(self.style.SUCCESS('Cities fetched. Done.'))
def test_project_status(self): c = Country(gwno=1,name="Somewhere",iso2c="SW",shape={},simpleshape={}) c.save() with patch("cartographer.services.today",Mock(return_value=date(1999,6,1))): url = reverse(projectStatus,kwargs={"project":1}) r = self.client.get(url) self.assertEqual(r.status_code,403) # Redirects if not logged in login = self.client.login(username="******",password="******") r = self.client.get(url) self.assertEqual(r.status_code,200) data = json.loads(r.content.decode()) self.assertEqual(data["shapes"],0) na = NonAnswer(author = self.u, country = c, date=date(1999,6,1)) na.save() data = json.loads(self.client.get(url).content.decode()) self.assertTrue(data["nonanswer"]) s = Shape.objects.create( author=self.u, country=c, date=date(1998,6,1), shape = randomFeature() ) data = json.loads(self.client.get(url).content.decode()) self.assertTrue(data["nonanswer"]) self.assertEqual(data["shapes"],0) s = Shape.objects.create( author=self.u, country=c, date=date(1999,1,1), shape=randomFeature() ) data = json.loads(self.client.get(url).content.decode()) self.assertTrue(data["nonanswer"]) self.assertEqual(data["shapes"],0) s = Shape.objects.create( author=self.u, country=c, date=date(1999,10,1), shape=randomFeature() ) data = json.loads(self.client.get(url).content.decode()) self.assertTrue(data["nonanswer"]) self.assertEqual(data["shapes"],0) s = Shape.objects.create( author=self.u, country=c, date=date(1999,6,15), shape=randomFeature()) data = json.loads(self.client.get(url).content.decode()) self.assertFalse(data["nonanswer"]) self.assertEqual(data["shapes"],1)
def handle(self, *args, **options): c = Country() c.name = "España" c.save()