def import_spreadsheet_proxy(request, state): state_info = get_state_info(state) if not state_info: raise Http404 try: data = merge_state_data(state) # Filter out empty reports and cases data["reports"] = [ # Here we export the `report` again, including only the fields we # want (the old JSON can come with other columns). { "date": report["date"], "notes": report["notes"], "state": state_info.state, "url": report["url"], } for report in data["reports"] if any(report.values()) ] data["cases"] = [ case for case in data["cases"] if any(value for key, value in case.items() if key != "municipio" and value not in ("", None)) ] return JsonResponse(data, safe=False) except SpreadsheetValidationErrors as e: return JsonResponse({"errors": e.error_messages}, status=400)
def historical_data(request, period): state = request.GET.get("state", None) if period not in ("daily", "weekly"): raise Http404 elif state is not None and not get_state_info(state): raise Http404 if period == "daily": from_states = stats.historical_case_data_for_state_per_day(state) from_registries = stats.historical_registry_data_for_state_per_day(state) from_registries_excess = stats.excess_deaths_registry_data_for_state_per_day(state) elif period == "weekly": from_states = stats.historical_case_data_for_state_per_epiweek(state) from_registries = stats.historical_registry_data_for_state_per_epiweek(state) from_registries_excess = stats.excess_deaths_registry_data_for_state_per_epiweek(state) # Remove last period since it won't be complete if period == "daily": from_states = clean_daily_data(from_states, skip=0, diff=-1) from_registries = clean_daily_data(from_registries, skip=7, diff=-14) from_registries_excess = clean_daily_data(from_registries_excess, skip=7, diff=-14) if period == "weekly": from_states = clean_weekly_data(from_states, diff_days=-7) from_registries = clean_weekly_data(from_registries, skip=1, diff_days=-14) from_registries_excess = clean_weekly_data(from_registries_excess, skip=1, diff_days=-14) state_data = row_to_column(from_states) registry_data = row_to_column(from_registries) registry_excess_data = row_to_column(from_registries_excess) data = { "from_states": state_data, "from_registries": registry_data, "from_registries_excess": registry_excess_data, } return JsonResponse(data)
def dashboard(request, state=None): if state is not None and not get_state_info(state): raise Http404 if state: state = state.upper() country_aggregate = make_aggregate( reports=stats.total_reports, confirmed=stats.total_confirmed, deaths=stats.total_deaths, affected_cities=stats.number_of_affected_cities, cities=stats.number_of_cities, affected_population=stats.affected_population, population=stats.total_population, cities_with_deaths=stats.cities_with_deaths, ) if state: city_data = stats.city_data_for_state(state) state_data = STATE_BY_ACRONYM[state] state_id = state_data.ibge_code state_name = state_data.name state_aggregate = make_aggregate( reports=stats.total_reports_for_state(state), confirmed=stats.total_confirmed_for_state(state), deaths=stats.total_deaths_for_state(state), affected_cities=stats.number_of_affected_cities_for_state(state), cities=stats.number_of_cities_for_state(state), affected_population=stats.affected_population_for_state(state), population=stats.total_population_for_state(state), cities_with_deaths=stats.cities_with_deaths_for_state(state), for_state=True, ) else: city_data = stats.city_data state_id = state_name = None state_aggregate = None return render( request, "covid19/dashboard.html", { "country_aggregate": country_aggregate, "state_aggregate": state_aggregate, "city_data": city_data, "state": state, "state_id": state_id, "city_slug": None, # TODO: change "state_name": state_name, "states": STATES, }, )
def import_spreadsheet_proxy(request, state): state_info = get_state_info(state) if not state_info: raise Http404 try: content = create_merged_state_spreadsheet(state) response = HttpResponse(content) response[ "Content-Type"] = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" response["Content-Disposition"] = f"attachment; filename={state}.xlsx" return response except SpreadsheetValidationErrors as e: return JsonResponse({"errors": e.error_messages}, status=400)
def states_geojson(request): state = request.GET.get("state", None) if state is not None and not get_state_info(state): raise Http404 if state: state = state.upper() data = state_geojson(high_fidelity=bool(state)) if state: state_id = STATE_BY_ACRONYM[state].ibge_code data["features"] = [ feature for feature in data["features"] if int(feature["properties"]["CD_GEOCUF"]) == state_id ] return JsonResponse(data, content_type="application/geo+json")
def cities_geojson(request): state = request.GET.get("state", None) if state is not None and not get_state_info(state): raise Http404 elif state: state = state.upper() high_fidelity = True city_data = stats.city_data(state) else: high_fidelity = False city_data = stats.city_data() city_ids = set(row["city_ibge_code"] for row in city_data) data = city_geojson(high_fidelity=high_fidelity) data["features"] = [feature for feature in data["features"] if feature["id"] in city_ids] return JsonResponse(data, content_type="application/geo+json")
def cities(request): state = request.GET.get("state", None) if state is not None and not get_state_info(state): raise Http404 brazil_city_data = stats.city_data() if state: city_data = stats.city_data(state) total_row = stats.state_row(state) else: city_data = brazil_city_data total_row = stats.country_row result = { "cities": {row["city_ibge_code"]: row for row in city_data}, "max": max_values(brazil_city_data), "total": total_row, } return JsonResponse(result)
def historical_data(request, period): state = request.GET.get("state", None) if period not in ("daily", "weekly"): raise Http404 elif state is not None and not get_state_info(state): raise Http404 if period == "daily": from_states = stats.historical_case_data_for_state_per_day(state) from_registries = stats.historical_registry_data_for_state_per_day(state) from_registries_excess = stats.excess_deaths_registry_data_for_state_per_day(state) elif period == "weekly": from_states = stats.historical_case_data_for_state_per_epiweek(state) from_registries = stats.historical_registry_data_for_state_per_epiweek(state) from_registries_excess = stats.excess_deaths_registry_data_for_state_per_epiweek(state) # Remove last period since it won't be complete diff_days = -14 if timezone.now().year == 2021: # TODO: remove this hack with up-to-date data diff_days -= ( timezone.now() - datetime.datetime(2021, 1, 1, 0, 0, 0, tzinfo=timezone.get_current_timezone()) ).days if period == "daily": from_states = clean_daily_data(from_states, skip=0, diff=-1) from_registries = clean_daily_data(from_registries, skip=7, diff=diff_days) from_registries_excess = clean_daily_data(from_registries_excess, skip=7, diff=diff_days) if period == "weekly": from_states = clean_weekly_data(from_states, diff_days=-7) from_registries = clean_weekly_data(from_registries, skip=1, diff_days=diff_days) from_registries_excess = clean_weekly_data(from_registries_excess, skip=1, diff_days=diff_days) state_data = row_to_column(from_states) registry_data = row_to_column(from_registries) registry_excess_data = row_to_column(from_registries_excess) data = { "from_states": state_data, "from_registries": registry_data, "from_registries_excess": registry_excess_data, } return JsonResponse(data)
def _parse_city_data(city, confirmed, deaths, date, state): data = { "city": city, "confirmed": confirmed, "date": date.isoformat(), "deaths": deaths, "place_type": "city", "state": state, } if city == TOTAL_LINE_DISPLAY: data['city_ibge_code'] = get_state_info(state).state_ibge_code data['place_type'] = 'state' data['city'] = None elif city == UNDEFINED_DISPLAY: data['city_ibge_code'] = None else: city_info = get_city_info(city, state) data['city_ibge_code'] = getattr(city_info, 'city_ibge_code', INVALID_CITY_CODE) data['city'] = getattr(city_info, 'city', INVALID_CITY_CODE) return data
def test_no_state_if_unexisting_uf(): info = get_state_info("XX") assert info is None
def test_get_state_info(): info = get_state_info("SP") assert 35 == info.state_ibge_code assert "SP" == info.state assert info == get_state_info("sp") assert getattr(info, "city_ibge_code", None) is None
def test_get_state_info(): info = get_state_info('SP') assert 35 == info.state_ibge_code assert 'SP' == info.state assert info == get_state_info('sp') assert getattr(info, 'city_ibge_code', None) is None