def delete(self, country_code, year, sex): data = PopulationChange.parser.parse_args() if not valid_country_code(country_code): return { 'message': '{} is not a valid country_code'.format(country_code) }, 400 if not valid_year(year): return {'message': 'Please enter a valid year'}, 400 if not valid_sex(sex): return {'message': 'Please enter a valid sex code'}, 400 # post_format tells us if we have an admin or subdiv code post_format = [0, 0] admin = data['admin'] if admin: if not valid_admin(admin): return {'message': 'Please enter a valid admin code'}, 400 post_format[0] = 1 # delete null value from data object subdiv = data['subdiv'] if subdiv: if not valid_subdiv(subdiv): return {'message': 'Please enter a valid subdiv code'}, 400 post_format[1] = 1 # if have admin and subdiv if post_format[0] == 1 and post_format[1] == 1: entry = PopulationModel.find_by_cysas(country_code, year, sex, admin, subdiv) # if have admin if post_format[0] == 1 and post_format[1] == 0: entry = PopulationModel.find_by_cysa(country_code, year, sex, admin) # if have subdiv if post_format[0] == 0 and post_format[1] == 1: entry = PopulationModel.find_by_cyss(country_code, year, sex, subdiv) # if have neither admin or data if post_format[0] == 0 and post_format[1] == 0: entry = PopulationModel.find_by_cys(country_code, year, sex) if entry: if len(entry) > 1: return { 'message': "More than one population entry was found with the given parameters. Please supply either an admin or subdiv code as required.", 'entries': [population.json() for population in entry] }, 400 entry[0].delete_from_db() return {'message': 'Entry deleted.'} return {'message': 'Item not found.'}, 404
def populate_population_table(): # with open('/var/www/html/items-rest/raw_data/populations.csv', 'r') as population_file: with open('./raw_data/populations.csv', 'r') as population_file: pop_reader = csv.reader(population_file) for row in pop_reader: new_population_object = PopulationModel(*row) if new_population_object.year == "Year": continue population = PopulationModel.find_by_cysas( new_population_object.country_code, new_population_object.year, new_population_object.sex, new_population_object.admin, new_population_object.subdiv) if population: continue new_population_object.save_to_db()
def get(self): query = {} country_code = request.args.get('country', type=str) if country_code: if not valid_country_code(country_code): return { 'message': '{} is not a valid country_code'.format(country_code) }, 400 query['country_code'] = country_code year = request.args.get('year', type=str) if year: if not valid_year(year): return {'message': 'Please enter a valid year'}, 400 query['year'] = year sex = request.args.get('sex', type=str) if sex: if not valid_sex(sex): return {'message': 'Please enter a valid sex code'}, 400 query['sex'] = sex admin = request.args.get('admin', type=str) if admin: if not valid_admin(admin): return {'message': 'Please enter a valid admin code'}, 400 query['admin'] = admin else: query['admin'] = "" subdiv = request.args.get('subdiv', type=str) if subdiv: if not valid_subdiv(subdiv): return {'message': 'Please enter a valid subdiv code'}, 400 query['subdiv'] = subdiv else: query['subdiv'] = "" result = [ entry.json() for entry in PopulationModel.search_populations(query) ] if result: if len(result) > 1: return { 'message': "More than one population entry was found matching your query." }, 400 return {'entry': result[0]}, 200 return {'message': "No populations match your query."}, 404
def get(self): query = {} country_code = request.args.get('country', type=str) if country_code: if not valid_country_code(country_code): return { 'message': '{} is not a valid country_code'.format(country_code) }, 400 query['country_code'] = country_code year = request.args.get('year', type=str) if year: if not valid_year(year): return {'message': 'Please enter a valid year'}, 400 query['year'] = year sex = request.args.get('sex', type=str) if sex: if not valid_sex(sex): return {'message': 'Please enter a valid sex code'}, 400 query['sex'] = sex admin = request.args.get('admin', type=str) if admin: if not valid_admin(admin): return {'message': 'Please enter a valid admin code'}, 400 query['admin_code'] = admin subdiv = request.args.get('subdiv', type=str) if subdiv: if not valid_subdiv(subdiv): return {'message': 'Please enter a valid subdiv code'}, 400 query['subdiv_code'] = subdiv cause = request.args.get('cause', type=str) if cause: cause_upper = cause.upper() if not valid_cause(cause_upper): return {'message': 'Please enter a valid cause code'}, 400 cause_code_list_extended = {} icd_codes = [ code.json() for code in IcdModel.find_by_code(cause_upper) ] for icd_code in icd_codes: matching_codes = [ matching_icd_code.json() for matching_icd_code in IcdModel.search_specific(icd_code['description']) ] unspecified_codes = [] generic_codes = [] pattern = "unspecified" match = re.search(pattern, icd_code['description']) if not match: unspecified_code = icd_code['description'] + \ " (unspecified)" unspecified_codes = [ matching_icd_code.json() for matching_icd_code in IcdModel.search_specific(unspecified_code) ] else: position = match.span() start = position[0] - 2 end = position[1] + 1 generic_code = icd_code['description'][0:start] + \ icd_code['description'][end:] generic_codes = [ matching_icd_code.json() for matching_icd_code in IcdModel.search_specific(generic_code) ] for matching_code in matching_codes: cause_code_list_extended[ matching_code['list']] = matching_code['code'] if generic_codes: for matching_code in generic_codes: cause_code_list_extended[ matching_code['list']] = matching_code['code'] if unspecified_codes: for matching_code in unspecified_codes: cause_code_list_extended[ matching_code['list']] = matching_code['code'] if "103" not in cause_code_list_extended: try: cause_code_list_extended['103'] = cause_code_list_extended[ '104'][:-1] except: pass code_list_entry = CodeListRefModel.find_by_year_and_country( year, country_code) try: cause = cause_code_list_extended[code_list_entry.code_list] except: return { 'message': "Can't find matching cause code for the specified country." }, 400 query['cause'] = cause results = [ entry.json() for entry in MortalityDataModel.search_mortalities(query) ] if code_list_entry.code_list == "104" and len(cause_code_list_extended[ code_list_entry.code_list]) == 3 and len(results) == 0: cause = cause + "9" query['cause'] = cause results = [ entry.json() for entry in MortalityDataModel.search_mortalities(query) ] if code_list_entry.code_list == "10M" and len(cause_code_list_extended[ code_list_entry.code_list]) == 3 and len(results) == 0: cause = cause + "9" query['cause'] = cause result = [ entry.json() for entry in MortalityDataModel.search_mortalities(query) ] if code_list_entry.code_list == "10M" and len(cause_code_list_extended[ code_list_entry.code_list]) == 4 and len(results) == 0: cause = cause[:-1] query['cause'] = cause result = [ entry.json() for entry in MortalityDataModel.search_mortalities(query) ] if not results: return {'message': "No mortality entries match your query."}, 404 if results: if len(results) > 1: return { 'message': "More than one population entry was found matching your query." }, 400 for entry in results: pop_query = {} pop_query['country_code'] = entry['country']['code'] pop_query['year'] = entry['year'] pop_query['sex'] = entry['sex']['code'] admin = entry['admin'] if admin != "None": pop_query['admin'] = admin['code'] subdiv = entry['subdiv'] if subdiv != "None": pop_query['subdiv'] = subdiv['code'] pop_data = PopulationModel.search_single_population(pop_query) if pop_data: pop_data = pop_data.json() else: country = CountryModel.find_by_code(country_code).json() return { 'message': "No population data available for '{}' in year {} for sex '{}'." .format(country['description'], year, sex) }, 404 del entry['infant_age_breakdown'] del entry['age_format'] del entry['infant_age_format'] things_to_skip = [ "country", "admin", "subdiv", "year", "code_list", "cause", "sex", "infant_age_breakdown" ] for key, value in entry.items(): if key in things_to_skip: continue # format age_breakdown data per 100,000 if key == "age_breakdown": ages_to_delete = [] for age_range, value in entry[key].items(): pop = pop_data[key].get(age_range) entry[key][age_range] = pop if pop: if pop == "0": continue entry[key][age_range] = str( round_up( float(value) / float(pop) * 100000, 3)) if pop == None: ages_to_delete.append(age_range) for index in ages_to_delete: del entry[key][index] else: entry[key] = str( round_up( float(value) / float(pop_data[key]) * 100000, 3)) return {'adjusted_entries': results}, 200
def get(self): def strip_whitespace(string): return ('').join(string.split(' ')) country_code_input = request.args.get('country', type=str) year_input = request.args.get('year', type=str) sex_code_input = request.args.get('sex', type=str) cause_code_input = request.args.get('cause', type=str) admin_code_input = request.args.get('admin', type=str) subdiv_code_input = request.args.get('subdiv', type=str) if not country_code_input or not year_input or not sex_code_input or not cause_code_input: return { 'message': "Please add at least a year, country, sex and cause variable" }, 400 country_code_list = strip_whitespace(country_code_input).split(',') year_list = strip_whitespace(year_input).split(',') sex_code_list = strip_whitespace(sex_code_input).split(',') cause_code_list = strip_whitespace(cause_code_input).split(',') cause_code_list = list(map(lambda x: x.upper(), cause_code_list)) cause_code_list_extended = {} for icd_code in filter(valid_cause, cause_code_list): complete_icd_codes = [ code.json() for code in IcdModel.find_by_code(icd_code) ] for icd_code in complete_icd_codes: matching_codes = [ matching_icd_code.json() for matching_icd_code in IcdModel.search_specific(icd_code['description']) ] # try to make it possible to compare countries that use different code lists # check if contains "unspecified". if not, add it and search again. else remove "(unspecified)" # TODO: in case that unspecified doesn't match. sum up all instances of code + extra digit. # e.g. A02 input by user. if code list is 104, sum all A020, A021, ...A029. return sum # TODO: if user input is 4 digit cause code, remove last digit and search for 103 code list with that unspecified_codes = [] generic_codes = [] pattern = "unspecified" match = re.search(pattern, icd_code['description']) if not match: unspecified_code = icd_code['description'] + \ " (unspecified)" unspecified_codes = [ matching_icd_code.json() for matching_icd_code in IcdModel.search_specific(unspecified_code) ] else: position = match.span() start = position[0] - 2 end = position[1] + 1 generic_code = icd_code['description'][0:start] + \ icd_code['description'][end:] generic_codes = [ matching_icd_code.json() for matching_icd_code in IcdModel.search_specific(generic_code) ] for matching_code in matching_codes: cause_code_list_extended[ matching_code['list']] = matching_code['code'] if generic_codes: for matching_code in generic_codes: cause_code_list_extended[ matching_code['list']] = matching_code['code'] if unspecified_codes: for matching_code in unspecified_codes: cause_code_list_extended[ matching_code['list']] = matching_code['code'] if "103" not in cause_code_list_extended: try: cause_code_list_extended[ '103'] = cause_code_list_extended['104'][:-1] except: pass # add "" to admin and subdiv lists to ensure some results if no specific code given admin_code_list = [] if admin_code_input: admin_code_list = strip_whitespace(admin_code_input).split(',') admin_code_list.append("") subdiv_code_list = [] if subdiv_code_input: subdiv_code_list = strip_whitespace(subdiv_code_input).split(',') subdiv_code_list.append("") # results of each mortality adj search results = [] # loop over all permutations of list items and validate codes. for country_code in filter(valid_country_code, country_code_list): for year in filter(valid_year, year_list): for sex in filter(valid_sex, sex_code_list): for cause in filter(valid_cause, cause_code_list): for admin in filter(valid_admin, admin_code_list): for subdiv in filter(valid_subdiv, subdiv_code_list): # generate query code_list_entry = CodeListRefModel.find_by_year_and_country( year, country_code) try: cause = cause_code_list_extended[ code_list_entry.code_list] except: continue query = {} query['country_code'] = country_code query['sex'] = sex query['year'] = year query['cause'] = cause query['admin_code'] = admin query['subdiv_code'] = subdiv result = [ entry.json() for entry in MortalityDataModel. search_mortalities(query) ] if code_list_entry.code_list == "104" and len( cause_code_list_extended[ code_list_entry.code_list] ) == 3 and len(result) == 0: cause = cause + "9" query['cause'] = cause result = [ entry.json() for entry in MortalityDataModel. search_mortalities(query) ] if code_list_entry.code_list == "10M" and len( cause_code_list_extended[ code_list_entry.code_list] ) == 3 and len(result) == 0: cause = cause + "9" query['cause'] = cause result = [ entry.json() for entry in MortalityDataModel. search_mortalities(query) ] if code_list_entry.code_list == "10M" and len( cause_code_list_extended[ code_list_entry.code_list] ) == 4 and len(result) == 0: cause = cause[:-1] query['cause'] = cause result = [ entry.json() for entry in MortalityDataModel. search_mortalities(query) ] if result: if len(result) > 1: continue # adjust for population and add to results list for entry in result: pop_query = {} pop_query['country_code'] = entry[ 'country']['code'] pop_query['year'] = entry['year'] pop_query['sex'] = entry['sex']['code'] admin = entry['admin'] if admin != "None": pop_query['admin'] = admin['code'] subdiv = entry['subdiv'] if subdiv != "None": pop_query['subdiv'] = subdiv['code'] pop_data = PopulationModel.search_single_population( pop_query) if pop_data: pop_data = pop_data.json() else: country = CountryModel.find_by_code( country_code).json() del entry['infant_age_breakdown'] del entry['age_format'] del entry['infant_age_format'] del entry['age_breakdown'] entry[ 'all_ages'] = "No population data." continue # remove infant mortality data as no related population data given del entry['infant_age_breakdown'] # delete age format numbers as now irrelevant del entry['age_format'] del entry['infant_age_format'] # divide mortality data by corresponding population number and multiply by 100,000 # population is population of that specific age/sex. not total country population things_to_skip = [ "country", "admin", "subdiv", "year", "code_list", "cause", "sex", "infant_age_breakdown" ] for key, value in entry.items(): if key in things_to_skip: continue # format age_breakdown data per 100,000 if key == "age_breakdown": ages_to_delete = [] for age_range, value in entry[ key].items(): pop = pop_data[key].get( age_range) entry[key][age_range] = pop if pop: if pop == "0": continue entry[key][ age_range] = str( round_up( float(value) / float(pop) * 100000, 3)) if pop == None: ages_to_delete.append( age_range) for index in ages_to_delete: del entry[key][index] else: entry[key] = str( round_up( float(value) / float(pop_data[key]) * 100000, 3)) # append to results list if anything found if result: results.append(result[0]) if len(results) == 0: return {'message': "No mortality entries match your query."}, 404 return {'results': results}, 200
def post(self, country_code, year, sex): # check country/admin/subdiv exist in respective databases before adding new population data = PopulationChange.parser.parse_args() # validation if not valid_country_code(country_code): return { 'message': '{} is not a valid country_code'.format(country_code) }, 400 if not valid_year(year): return {'message': 'Please enter a valid year'}, 400 if not valid_sex(sex): return {'message': 'Please enter a valid sex code'}, 400 # post_format tells us if we have an admin or subdiv code while allowing us to change their values post_format = [0, 0] admin = data['admin'] if admin: if not valid_admin(admin): return {'message': 'Please enter a valid admin code'}, 400 post_format[0] = 1 # delete null value from data object del data['admin'] subdiv = data['subdiv'] if subdiv: if not valid_subdiv(subdiv): return {'message': 'Please enter a valid subdiv code'}, 400 post_format[1] = 1 del data['subdiv'] # if have admin and subdiv if post_format[0] == 1 and post_format[1] == 1: if PopulationModel.find_by_cysas(country_code, year, sex, admin, subdiv): return { 'message': "An entry already exists for your given year, country, sex, admin and subdiv." }, 400 # if have admin if post_format[0] == 1 and post_format[1] == 0: if PopulationModel.find_by_cysa(country_code, year, sex, admin): return { 'message': "An entry already exists for your given year, country, sex and admin." }, 400 subdiv = "" # delete the null values from the data object del data['subdiv'] # if have subdiv if post_format[0] == 0 and post_format[1] == 1: if PopulationModel.find_by_cyss(country_code, year, sex, subdiv): return { 'message': "An entry already exists for your given year, country, sex and subdiv." }, 400 admin = "" del data['admin'] # if have neither admin or data if post_format[0] == 0 and post_format[1] == 0: if PopulationModel.find_by_cys(country_code, year, sex): return { 'message': "An entry already exists for your given year, country, sex." }, 400 del data['admin'] admin = "" del data['subdiv'] subdiv = "" entry = PopulationModel(country_code, admin, subdiv, year, sex, **data) try: entry.save_to_db() except: return {"message": "An error occurred inserting the item."}, 500 return entry.json(), 201
def get(self): populations = [entry.json() for entry in PopulationModel.find_all()] return {'populations': populations}, 200
def get(self): country_code_input = request.args.get('country', type=str) year_input = request.args.get('year', type=str) sex_code_input = request.args.get('sex', type=str) admin_code_input = request.args.get('admin', type=str) subdiv_code_input = request.args.get('subdiv', type=str) if not country_code_input or not year_input or not sex_code_input: return { 'message': "Please add at least a year, country and sex variable" }, 400 country_code_list = strip_whitespace(country_code_input).split(',') year_list = strip_whitespace(year_input).split(',') sex_code_list = strip_whitespace(sex_code_input).split(',') # add "" to admin and subdiv lists to ensure some results if no specific code given admin_code_list = [] if admin_code_input: admin_code_list = strip_whitespace(admin_code_input).split(',') else: admin_code_list.append("") subdiv_code_list = [] if subdiv_code_input: subdiv_code_list = strip_whitespace(subdiv_code_input).split(',') else: subdiv_code_list.append("") results = [] for country_code in filter(valid_country_code, country_code_list): for year in filter(valid_year, year_list): for sex in filter(valid_sex, sex_code_list): for admin in filter(valid_admin, admin_code_list): for subdiv in filter(valid_subdiv, subdiv_code_list): query = {} query['country_code'] = country_code query['sex'] = sex query['year'] = year query['admin'] = admin query['subdiv'] = subdiv result = [ entry.json() for entry in PopulationModel.search_populations(query) ] if result: if len(result) > 1: continue results.append(result[0]) if len(results) == 0: return { 'message': 'No populations match your search parameters' }, 404 return {'results': results}, 200
def put(self, country_code, year, sex): data = PopulationChange.parser.parse_args() if not valid_country_code(country_code): return { 'message': '{} is not a valid country_code'.format(country_code) }, 400 if not valid_year(year): return {'message': 'Please enter a valid year'}, 400 if not valid_sex(sex): return {'message': 'Please enter a valid sex code'}, 400 # post_format tells us if we have an admin or subdiv code post_format = [0, 0] admin = data['admin'] if admin: if not valid_admin(admin): return {'message': 'Please enter a valid admin code'}, 400 post_format[0] = 1 del data['admin'] subdiv = data['subdiv'] if subdiv: if not valid_subdiv(subdiv): return {'message': 'Please enter a valid subdiv code'}, 400 post_format[1] = 1 del data['subdiv'] # if have admin and subdiv if post_format[0] == 1 and post_format[1] == 1: entry = PopulationModel.find_by_cysas(country_code, year, sex, admin, subdiv) # if have admin if post_format[0] == 1 and post_format[1] == 0: entry = PopulationModel.find_by_cysa(country_code, year, sex, admin) # if have subdiv if post_format[0] == 0 and post_format[1] == 1: entry = PopulationModel.find_by_cyss(country_code, year, sex, subdiv) # if have neither admin or data if post_format[0] == 0 and post_format[1] == 0: entry = PopulationModel.find_by_cys(country_code, year, sex) if entry: entry.age_format = data['age_format'] entry.pop1 = data['pop1'] entry.pop2 = data['pop2'] entry.pop3 = data['pop3'] entry.pop4 = data['pop4'] entry.pop5 = data['pop5'] entry.pop6 = data['pop6'] entry.pop7 = data['pop7'] entry.pop8 = data['pop8'] entry.pop9 = data['pop9'] entry.pop10 = data['pop10'] entry.pop11 = data['pop11'] entry.pop12 = data['pop12'] entry.pop13 = data['pop13'] entry.pop14 = data['pop14'] entry.pop15 = data['pop15'] entry.pop16 = data['pop16'] entry.pop17 = data['pop17'] entry.pop18 = data['pop18'] entry.pop19 = data['pop19'] entry.pop20 = data['pop20'] entry.pop21 = data['pop21'] entry.pop22 = data['pop22'] entry.pop23 = data['pop23'] entry.pop24 = data['pop24'] entry.pop25 = data['pop25'] entry.pop26 = data['pop26'] entry.live_births = data['live_births'] else: entry = PopulationModel(country_code, admin, subdiv, year, sex, **data) entry.save_to_db() return entry.json()