def weekly_monthly_children_export(request, period, rtype): data = [] for district in Entity.objects.filter(type__slug='district'): nb_deaths = ChildrenMortalityReport.periods.within(period) \ .filter(death_location__in=district.get_descendants()) \ .filter(source=ChildrenMortalityReport.UNFPA) \ .count() data.append({'district': district, 'deaths': nb_deaths}) file_name = 'Rapports de children.xls' file_content = children_as_excel(data, period).getvalue() response = HttpResponse(file_content, \ content_type='application/vnd.ms-excel') response['Content-Disposition'] = 'attachment; filename="%s"' % file_name response['Content-Length'] = len(file_content) return response
def quarterly_annual_children_export(request, period, rtype): data = [] months = period.months # total deaths for all districts # /!\ UNFPA districts only all_deaths = ChildrenMortalityReport.periods.within(period) \ .filter(source=ChildrenMortalityReport.UNFPA) \ .count() # for each district for district in Entity.objects.filter(type__slug='district'): mdeaths = [] for month in months: nb_deaths = ChildrenMortalityReport.periods.within(month) \ .filter(death_location__in=district .get_descendants()) \ .filter(source=ChildrenMortalityReport.UNFPA) \ .count() mdeaths.append(nb_deaths) total = sum(mdeaths) try: percent_of_all = float(total) / all_deaths except ZeroDivisionError: percent_of_all = 0 data.append({'district': district, 'mdeaths': mdeaths, 'total': total, 'all_deaths': all_deaths, 'percent_of_all': percent_of_all}) file_name = 'Rapports de children.xls' file_content = children_as_excel(data, period).getvalue() response = HttpResponse(file_content, \ content_type='application/vnd.ms-excel') response['Content-Disposition'] = 'attachment; filename="%s"' % file_name response['Content-Length'] = len(file_content) return response