def setUp(self): db.create_all() laureate1 = Laureate("Yuan", 1, "1956-11-2", "M", "u1", 1) laureate1.search_text = "yuan taiwan" laureate2 = Laureate("Yuan", 2, "1939-4-7", "F", "u2", 1) laureate2.search_text = "yuan" prize1 = Prize("Physics", 1991, 2, "motivation1", "u1") prize1.search_text = "one yuan" prize2 = Prize("Physics", 1991, 2, "motivation1", "u1") prize2.search_text = "taiwan one yuan" country1 = Country("SE", "Sweden", 10, 7, 10000000, "u1") country1.search_text = "taiwan ldksfjsl;fkjsd yuan" country2 = Country("SR", "Sweden", 10, 7, 10000000, "u1") country2.search_text = "two yuan" db.session.add(laureate1) db.session.add(laureate2) db.session.add(prize1) db.session.add(prize2) db.session.add(country1) db.session.add(country2) db.session.commit()
countryModel = Country.query.filter_by(country_code = countryCode).first() # if country didn't already exist if countryModel == None : countryResponse = requests.get("https://restcountries.eu/rest/v1/alpha?codes="+countryCode) if countryResponse.ok: country = json.loads(countryResponse.text)[0] #again only one item countryModel = Country(countryCode, country["name"], 0, 0, country["population"], "/countries/"+country["name"].replace(" ", "_")) db.session.add(countryModel) print(countryModel) db.session.commit() countryModel.nr_laureates += 1 countryModel.nr_prizes += 1 db.session.commit() laureateModel = Laureate(personName, 1, person["born"], person["gender"], "/laureates/"+personName.replace(" ","_"), countryCode) else: laureateModel = Laureate(personName, 1, person["born"], person["gender"], "/laureates/"+personName.replace(" ","_"), None) print(laureateModel) db.session.add(laureateModel) else: # increment # prize for laureate and country if already exist laureateModel = Laureate.query.filter_by(name = personName).first() laureateModel.nr_prizes = laureateModel.nr_prizes + 1 if laureateModel.country_id != None : c = Country.query.get(laureateModel.country_id) c.nr_prizes += 1 prizeModel.laureates.append(laureateModel) db.session.commit()