def handle_get(http_server, database_manager, ip, account, api_key): query_params = http_server.get_query_parameters() if "city" not in query_params: raise ApiParamNoCityProvidedError if "state" not in query_params: raise ApiParamNoStateCodeProvidedError raw_city_string = query_params["city"] raw_state_code_string = query_params["state"] city_string = raw_city_string.title() state_code_string = raw_state_code_string.upper() match = re.match(r'^[A-Z]{2}$', state_code_string) if match is None: raise ApiParamInvalidStateCodeError zip_conditions = [ { 'column': 'city', 'equivalence': '=', 'value': city_string.upper() }, { 'column': 'state_code', 'equivalence': '=', 'value': state_code_string } ] zip_codes = database_manager.fetch_by( ZipCode(database_manager), zip_conditions ) if len(zip_codes) == 0: raise ApiParamInvalidStateCodeError state = UsState.fetch_by_code(database_manager, state_code_string) zip_code_counties_conditions = [{ 'column': 'zip', 'equivalence': ' IN ', 'value': [z.zip_code for z in zip_codes] }] county_percentages = database_manager.fetch_by( county2zipCode(database_manager), zip_code_counties_conditions ) county_fips = [] if len(county_percentages) > 0: county_fips = [c.get_state_county_fips() for c in county_percentages] county_fips = list(set(county_fips)) subscription_plan = SubscriptionPlan.fetch_by_id( database_manager, account.subscription_plan_id ) for header in eligible_headers: eligible_headers[header] = get_is_eligible( subscription_plan, header ) zip_totals = {} if len(county_fips) > 0: voting_county_conditions = [{ "column": "county_code", "equivalence": "IN", "value": county_fips }] county_totals = database_manager.fetch_by( VotesCounty(database_manager), voting_county_conditions ) else: raise ApiParamInvalidStateCodeError for header, is_eligible in eligible_headers.items(): if is_eligible is True: zip_totals[header] = 0 for county_total in county_totals: for county_percentage in county_percentages: if county_percentage.get_state_county_fips() == \ county_total.county_code: total = getattr( county_total, header ) if total is None: total = 0 zip_totals[header] += ( round( total * county_percentage.zip_population_percent ) ) break years = [str(x) for x in range(1980, 2010)] raw_output_data = OrderedDict({}) totals = {} for year in years: output_year = {} for header, value in zip_totals.items(): if year in header: output_year[header[:-len(year)-1]] = value if len(output_year) > 0: raw_output_data[year] = output_year if year not in totals: if year in raw_output_data: totals[year] = raw_output_data[year]["total"] # get percentages output_data = OrderedDict({}) notes = [] for year in raw_output_data: output_data[year] = {"breakdown":{}} for key, value in raw_output_data[year].items(): if key != "total": output_data[year]["breakdown"][key] = 100 * value / totals[year] if get_is_eligible(subscription_plan, "totals") is True: output_data[year]['total_votes'] = totals[year] else: notes.append("Upgrade to a paid plan for totals") if len(output_data) == 1: notes.append("Upgrade plan for historical data") notes = list(set(notes)) state_name = "" state_code = "" if state.code is not None: state_code = state.code if state.name is not None: state_name = state.name output = OrderedDict({ "voting_ranges": output_data, "units": "percent", "location": { "city": { "name": city_string, }, "state": { "name": state_name, "code": state_code.upper(), "api_endpoint": '/v1/race/state?code={}'.format(state_code.upper()), }, "country": { "name": "United States of America", "code": "US", "api_endpoint": '/v1/race/country?code=US', } }, "notes": notes }) http_server.print_headers() http_server.print_json(output)
def handle_get(http_server, database_manager, ip, account, api_key): query_params = http_server.get_query_parameters() if "code" not in query_params: raise ApiParamNoCountryCodeProvidedError raw_country_code_string = query_params["code"] country_code_string = raw_country_code_string.upper() if country_code_string is None: raise ApiParamInvalidCountryCodeError match = re.match(r'^[A-Za-z]{2}$', country_code_string) if match is None: raise ApiParamInvalidCountryCodeError country = Country.fetch_by_code(database_manager, country_code_string) if country is None: raise ApiParamInvalidCountryCodeError country_name = country.name if country.code == "US": country_name = "United States" county_total = GenderCounty.fetch_by_country_name(database_manager, country_name.upper()) subscription_plan = SubscriptionPlan.fetch_by_id( database_manager, account.subscription_plan_id) for header in eligible_headers: eligible_headers[header] = get_is_eligible(subscription_plan, header) zip_totals = {} for header, is_eligible in eligible_headers.items(): if is_eligible is True: zip_totals[header] = 0 total = getattr(county_total, header) if total is None: total = 0 zip_totals[header] = (round(total)) years = [str(year) for year in range(2000, 2010)] raw_output_data = OrderedDict({}) totals = {} for year in years: output_year = {} for header, value in zip_totals.items(): if year in header: output_year[header[:-len(year) - 1]] = value if len(output_year) > 0: raw_output_data[year] = output_year if year not in totals: if year in raw_output_data: totals[year] = sum( [v for k, v in raw_output_data[year].items()]) # get percentages output_data = OrderedDict({}) notes = [] for year in raw_output_data: output_data[year] = {"breakdown": {}} for key, value in raw_output_data[year].items(): output_data[year]["breakdown"][key] = 100 * value / totals[year] if get_is_eligible(subscription_plan, "totals") is True: output_data[year]['total'] = totals[year] else: notes.append("Upgrade to a paid plan for totals") if len(output_data) == 1: notes.append("Upgrade plan for historical data") notes = list(set(notes)) output = OrderedDict({ "gender_breakdown": output_data, "units": "percent", "location": { "country": { "name": "United States of America", "code": "US", } }, "notes": notes }) http_server.print_headers() http_server.print_json(output)
def handle_get(http_server, database_manager, ip, account, api_key): query_params = http_server.get_query_parameters() if "zip" not in query_params: raise ApiParamNoZipCodeProvidedError raw_zip_code_string = query_params["zip"] zip_code_string = raw_zip_code_string if "-" in raw_zip_code_string: dash_position = raw_zip_code_string.find("-") zip_code_string = raw_zip_code_string[0:dash_position] match = re.match(r'^\d+$', zip_code_string) if match is None: raise ApiParamInvalidZipCodeError zip_code_int = int(zip_code_string) zip_code = ZipCode.fetch_by_zip_int(database_manager, zip_code_int, is_decommisioned=False) if zip_code is None: raise ApiParamInvalidZipCodeError state = UsState.fetch_by_id(database_manager, zip_code.state_id) city_name = zip_code.city if zip_code.city is not None: city_name = zip_code.city.title() county_percentages = zip_code.get_county_percentages() county_fips = [] if len(county_percentages) > 0: county_fips = [c.get_state_county_fips() for c in county_percentages] county_fips = list(set(county_fips)) subscription_plan = SubscriptionPlan.fetch_by_id( database_manager, account.subscription_plan_id) for header in eligible_headers: eligible_headers[header] = get_is_eligible(subscription_plan, header) zip_totals = {} if len(county_fips) > 0: population_county_conditions = [{ "column": "county_code", "equivalence": "IN", "value": county_fips }] county_totals = database_manager.fetch_by( PopulationCounty(database_manager), population_county_conditions) else: raise ApiParamInvalidZipCodeError for header, is_eligible in eligible_headers.items(): if is_eligible is True: zip_totals[header] = 0 for county_total in county_totals: for county_percentage in county_percentages: if county_percentage.get_state_county_fips() == \ county_total.county_code: total = getattr(county_total, header) if total is None: total = 0 zip_totals[header] += (round( total * county_percentage.zip_population_percent)) break years = [str(x) for x in range(1930, 2011)] raw_output_data = OrderedDict({}) totals = {} for year in years: output_year = {} for header, value in zip_totals.items(): if year in header: output_year[header[:-len(year) - 1]] = value if len(output_year) > 0: raw_output_data[year] = output_year # get percentages output_data = OrderedDict({}) notes = [] for year in raw_output_data: output_data[year] = None for key, value in raw_output_data[year].items(): output_data[year] = value if len(output_data) == 1: notes.append("Upgrade plan for historical data") notes = list(set(notes)) state_name = "" state_code = "" if state.code is not None: state_code = state.code if state.name is not None: state_name = state.name output = OrderedDict({ "population_estimates": output_data, "units": "people", "location": { "city": { "name": city_name, "api_endpoint": '/v1/population/city?city={}&state={}'.format( requote_uri(city_name), state_code.upper()), }, "state": { "name": state_name, "code": state_code.upper(), "api_endpoint": '/v1/population/state?code={}'.format(state_code.upper()), }, "country": { "name": "United States of America", "code": "US", "api_endpoint": '/v1/population/country?code=US', } }, "notes": notes }) http_server.print_headers() http_server.print_json(output)
def handle_get(http_server, database_manager, ip, account, api_key): query_params = http_server.get_query_parameters() if "code" not in query_params: raise ApiParamNoStateCodeProvidedError raw_state_code_string = query_params["code"] state_code_string = raw_state_code_string.upper() match = re.match(r'^[A-Za-z]{2}$', state_code_string) if match is None: raise ApiParamInvalidStateCodeError state = UsState.fetch_by_code(database_manager, state_code_string) if state is None: raise ApiParamInvalidStateCodeError county_total = AncestryCounty.fetch_by_state_name(database_manager, state.name.upper()) subscription_plan = SubscriptionPlan.fetch_by_id( database_manager, account.subscription_plan_id) for header in eligible_headers: eligible_headers[header] = get_is_eligible(subscription_plan, header) zip_totals = {} for header, is_eligible in eligible_headers.items(): if is_eligible is True: zip_totals[header] = 0 total = getattr(county_total, header) if total is None: total = 0 zip_totals[header] = (round(total)) years = [str(x) for x in range(1980, 2010)] raw_output_data = OrderedDict({}) totals = {} for year in years: output_year = {} for header, value in zip_totals.items(): if year in header: output_year[header[:-len(year) - 1]] = value if len(output_year) > 0: raw_output_data[year] = output_year if year not in totals: totals[year] = 0 else: totals[year] += value # get percentages output_data = OrderedDict({}) notes = [] for year in raw_output_data: output_data[year] = {"breakdown": {}} for key, value in raw_output_data[year].items(): output_data[year]["breakdown"][key] = value / totals[year] if get_is_eligible(subscription_plan, "totals") is True: output_data[year]['total'] = totals[year] else: notes.append("Upgrade to a paid plan for totals") if len(output_data) == 1: notes.append("Upgrade plan for historical data") notes = list(set(notes)) output = { "ancestry_ranges": output_data, "units": "percent", "location": { "state": { "name": state.name, "code": state.code.upper(), }, "country": { "name": "United States of America", "code": "US", "api_endpoint": '/v1/ancestry/country?code=US', } }, "notes": notes } http_server.print_headers() http_server.print_json(output)
def handle_get(http_server, database_manager, ip, account, api_key): query_params = http_server.get_query_parameters() if "code" not in query_params: raise ApiParamNoStateCodeProvidedError raw_state_code_string = query_params["code"] state_code_string = raw_state_code_string.upper() match = re.match(r'^[A-Za-z]{2}$', state_code_string) if match is None: raise ApiParamInvalidStateCodeError state = UsState.fetch_by_code(database_manager, state_code_string) if state is None: raise ApiParamInvalidStateCodeError county_total = EducationCounty.fetch_by_state_name(database_manager, state.name.upper()) subscription_plan = SubscriptionPlan.fetch_by_id( database_manager, account.subscription_plan_id) for header in eligible_headers: eligible_headers[header] = get_is_eligible(subscription_plan, header) zip_totals = {} for header, is_eligible in eligible_headers.items(): if is_eligible is True: zip_totals[header] = 0 total = getattr(county_total, header) if total is None: total = 0 zip_totals[header] = (round(total)) years = ["1980", "1990", "2000", "2009"] raw_output_data = OrderedDict({}) totals = {} for year in years: output_year = {} for header, value in zip_totals.items(): if "num_adults" not in header: if year in header: ending = year end = len(ending) + 1 if "_2005_" in header: ending = "2005_{}".format(year) end = len(ending) + 1 output_year[header[:-end]] = 100 * value / zip_totals[ "num_adults_{}".format(ending)] if len(output_year) > 0: raw_output_data[year] = output_year if year not in totals: if year in raw_output_data: ending = year if year == "2009": ending = "2005_2009" totals[year] = zip_totals["num_adults_{}".format(ending)] # get percentages output_data = OrderedDict({}) notes = [] for year in raw_output_data: output_data[year] = {"breakdown": {}} for key, value in raw_output_data[year].items(): output_data[year]["breakdown"][key] = value if get_is_eligible(subscription_plan, "totals") is True: output_data[year]['total_adults'] = totals[year] else: notes.append("Upgrade to a paid plan for totals") notes = list(set(notes)) state_name = "" state_code = "" if state.code is not None: state_code = state.code if state.name is not None: state_name = state.name output = { "education_ranges": output_data, "location": { "state": { "name": state.name, "code": state.code.upper(), }, "country": { "name": "United States of America", "code": "US", "api_endpoint": '/v1/education/country?code=US', } }, "notes": notes } http_server.print_headers() http_server.print_json(output)
def handle_get(http_server, database_manager, ip, account, api_key): query_params = http_server.get_query_parameters() if "zip" not in query_params: raise ApiParamNoZipCodeProvidedError raw_zip_code_string = query_params["zip"] zip_code_string = raw_zip_code_string if "-" in raw_zip_code_string: dash_position = raw_zip_code_string.find("-") zip_code_string = raw_zip_code_string[0:dash_position] match = re.match(r'^\d+$', zip_code_string) if match is None: raise ApiParamInvalidZipCodeError zip_code_int = int(zip_code_string) zip_code = ZipCode.fetch_by_zip_int( database_manager, zip_code_int, is_decommisioned=False ) if zip_code is None: raise ApiParamInvalidZipCodeError state = UsState.fetch_by_id(database_manager, zip_code.state_id) city_name = zip_code.city if zip_code.city is not None: city_name = zip_code.city.title() county_percentages = zip_code.get_county_percentages() county_fips = [] if len(county_percentages) > 0: county_fips = [c.get_state_county_fips() for c in county_percentages] county_fips = list(set(county_fips)) subscription_plan = SubscriptionPlan.fetch_by_id( database_manager, account.subscription_plan_id ) for header in eligible_headers: eligible_headers[header] = get_is_eligible( subscription_plan, header ) zip_totals = {} if len(county_fips) > 0: education_county_conditions = [{ "column": "county_code", "equivalence": "IN", "value": county_fips }] county_totals = database_manager.fetch_by( EducationCounty(database_manager), education_county_conditions ) else: raise ApiParamInvalidZipCodeError for header, is_eligible in eligible_headers.items(): if is_eligible is True: zip_totals[header] = 0 for county_total in county_totals: for county_percentage in county_percentages: if county_percentage.get_state_county_fips() == \ county_total.county_code: total = getattr( county_total, header ) if total is None: total = 0 zip_totals[header] = ( round( total * county_percentage.zip_population_percent ) ) break years = ["1980", "1990", "2000", "2009"] raw_output_data = OrderedDict({}) totals = {} for year in years: output_year = {} for header, value in zip_totals.items(): if "num_adults" not in header: if year in header: ending = year end = len(ending) + 1 if "_2005_" in header: ending = "2005_{}".format(year) end = len(ending) + 1 output_year[header[:-end]] = 100 * value / zip_totals[ "num_adults_{}".format(ending) ] if len(output_year) > 0: raw_output_data[year] = output_year if year not in totals: if year in raw_output_data: ending = year if year == "2009": ending = "2005_2009" totals[year] = zip_totals["num_adults_{}".format(ending)] # get percentages output_data = OrderedDict({}) notes = [] for year in raw_output_data: output_data[year] = {"breakdown":{}} for key, value in raw_output_data[year].items(): output_data[year]["breakdown"][key] = value if get_is_eligible(subscription_plan, "totals") is True: output_data[year]['total_adults'] = totals[year] else: notes.append("Upgrade to a paid plan for totals") if len(output_data) == 1: notes.append("Upgrade plan for historical data") notes = list(set(notes)) state_name = "" state_code = "" if state.code is not None: state_code = state.code if state.name is not None: state_name = state.name output = OrderedDict({ "education_ranges": output_data, "city": { "name": city_name, "api_endpoint": '/v1/education/city?city={}&state={}'.format( requote_uri(city_name), state_code.upper() ), }, "state": { "name": state_name, "code": state_code.upper(), "api_endpoint": '/v1/education/state?code={}'.format(state_code.upper()), }, "country": { "name": "United States of America", "code": "US", "api_endpoint": '/v1/education/country?code=US', } }) http_server.print_headers() http_server.print_json(output)