def survey_completion_summary(request, household_id, batch_id): household = get_object_or_404(Household, pk=household_id) batch = get_object_or_404(Batch, pk=batch_id) survey = batch.survey ea = household.listing.ea allocations = SurveyAllocation.objects.filter(allocation_ea=ea, survey=batch.survey) context = {} if allocations.exists(): completion_rates = BatchLocationCompletionRates(batch, ea=ea, specific_households=[household_id, ]) result_service = ResultsDownloadService(batch=batch, specific_households=[household_id, ]) reports = result_service.generate_interview_reports() reports_headers = reports.pop(0) context.update({ 'household' : household, 'batch': batch, 'reports_headers': reports_headers, 'reports': reports, 'completion_rates': completion_rates, 'interviewer': allocations[0].interviewer }) request.breadcrumbs([ ('Completion Rates', reverse('survey_completion_rates', )), ('EA Completion', reverse('ea_completion_summary', args=(ea.pk, batch.pk))), ]) return render(request, 'aggregates/household_completion_report.html', context)
def download_data(request, qset_id): qset = QuestionSet.get(pk=qset_id) params = request.GET if request.method == 'GET' else request.POST survey_filter = QuestionSetResultsFilterForm(qset, data=params) locations_filter = LocationsFilterForm(data=request.GET, include_ea=True) interviews = survey_filter.get_interviews() if locations_filter.is_valid(): interviews = interviews.filter( ea__in=locations_filter.get_enumerations()).order_by('created') last_selected_loc = locations_filter.last_location_selected download_service = ResultsDownloadService(qset, interviews=interviews) file_name = '%s%s' % ('%s-%s-' % (last_selected_loc.type.name, last_selected_loc.name) if last_selected_loc else '', qset.name) reports_df = download_service.generate_interview_reports() response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment;\ filename="%s.csv"' % file_name reports_df.to_csv(response, date_format='%Y-%m-%d %H:%M:%S', encoding='utf-8') # exclude interview id return response
def download_data(request, qset_id): qset = QuestionSet.get(pk=qset_id) params = request.GET if request.method == 'GET' else request.POST survey_filter = QuestionSetResultsFilterForm(qset, data=params) locations_filter = LocationsFilterForm(data=request.GET, include_ea=True) interviews = survey_filter.get_interviews() if locations_filter.is_valid(): interviews = interviews.filter( ea__in=locations_filter.get_enumerations()).order_by('created') last_selected_loc = locations_filter.last_location_selected download_service = ResultsDownloadService(qset, interviews=interviews) file_name = '%s%s' % ('%s-%s-' % ( last_selected_loc.type.name, last_selected_loc.name) if last_selected_loc else '', qset.name) reports_df = download_service.generate_interview_reports() response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment;\ filename="%s.csv"' % file_name reports_df.to_csv( response, date_format='%Y-%m-%d %H:%M:%S', encoding='utf-8') # exclude interview id return response
def test_reference_interview_report(self): listing_form = mommy.make(ListingTemplate) mommy.make(QuestionSetChannel, qset=listing_form, channel=self.access_channel.choice_name()) self.survey.has_sampling = True self.survey.listing_form = listing_form self.survey.save() interviews = self._prep_answers(listing_form) rs = ResultsDownloadService(listing_form, survey=self.survey, follow_ref=True) reports_df = rs.generate_interview_reports() for idx, interview in enumerate(interviews): for answer in interview.answer.all(): # since this ref_interview only captured one row of information, so this is valid self.assertEquals(answer.as_text, reports_df[answer.question.identifier][idx]) self.assertEquals(reports_df['EA'][0], self.ea.name) for location in self.ea.locations.first().get_ancestors().exclude( id=Location.country().id): self.assertEquals(reports_df[location.type.name][0], location.name) ref_interview = Interview.objects.last() self._create_ussd_non_group_questions(self.qset) n_quest = Question.objects.filter( answer_type=NumericalAnswer.choice_name(), qset=self.qset).last() t_quest = Question.objects.filter(answer_type=TextAnswer.choice_name(), qset=self.qset).last() m_quest = Question.objects.filter( answer_type=MultiChoiceAnswer.choice_name(), qset=self.qset).last() answers = [ { n_quest.id: 27, t_quest.id: 'Test Ref', m_quest.id: 'N' }, ] question_map = { n_quest.id: n_quest, t_quest.id: t_quest, m_quest.id: m_quest } Interview.objects.filter(question_set=self.qset).delete() interviews = Interview.save_answers(self.qset, self.survey, self.ea, self.access_channel, question_map, answers, reference_interview=ref_interview) rs = ResultsDownloadService(self.qset, survey=self.survey, follow_ref=True) reports_df = rs.generate_interview_reports() for answer in ref_interview.answer.all(): # since this ref_interview only captured one row of information, so this is valid self.assertEquals(answer.as_text, reports_df[answer.question.identifier][0]) for answer in interviews[0].answer.all(): # since this ref_interview only captured one row of information, so this is valid self.assertEquals(answer.as_text, reports_df[answer.question.identifier][0]) self.assertEquals(reports_df['EA'][0], self.ea.name) for location in self.ea.locations.first().get_ancestors().exclude( id=Location.country().id): self.assertEquals(reports_df[location.type.name][0], location.name)
def download(request): request_data = request.GET if request.method == 'GET' else request.POST survey_batch_filter_form = SurveyBatchFilterForm(data=request_data) locations_filter = LocationsFilterForm(data=request_data) last_selected_loc = locations_filter.last_location_selected if request_data and request_data.get('action'): survey_batch_filter_form = SurveyBatchFilterForm(data=request_data) if survey_batch_filter_form.is_valid(): batch = survey_batch_filter_form.cleaned_data['batch'] survey = survey_batch_filter_form.cleaned_data['survey'] multi_option = \ survey_batch_filter_form.cleaned_data['multi_option'] restricted_to = None if last_selected_loc: restricted_to = [ last_selected_loc, ] if request_data.get('action') == 'Email Spreadsheet': composer = ResultComposer( request.user, ResultsDownloadService(batch, survey=survey, restrict_to=restricted_to, multi_display=multi_option)) send_mail.delay(composer) messages.warning( request, "Email would be sent to\ you shortly. This could take a while.") else: download_service = ResultsDownloadService( batch, survey=survey, restrict_to=restricted_to, multi_display=multi_option) file_name = '%s%s' % ( '%s-%s-' % (last_selected_loc.type.name, last_selected_loc.name) if last_selected_loc else '', batch.name if batch else survey.name) reports_df = download_service.generate_interview_reports() response = HttpResponse(content_type='application/zip') string_buf = StringIO() reports_df.to_csv(string_buf, columns=reports_df.columns[1:]) string_buf.seek(0) file_contents = string_buf.read() string_buf.close() zip_file = InMemoryZip() zip_file = zip_file.append("%s.csv" % file_name, file_contents) response['Content-Disposition'] = 'attachment;\ filename=%s.zip' % file_name response.write(zip_file.read()) # exclude interview id if not request.is_ajax(): messages.info(request, "Download successfully downloaded") return response loc_types = LocationType.in_between() return render( request, 'aggregates/download_excel.html', { 'survey_batch_filter_form': survey_batch_filter_form, 'locations_filter': locations_filter, 'export_url': '%s?%s' % (reverse('excel_report'), request.META['QUERY_STRING']), 'location_filter_types': loc_types })
def download(request): request_data = request.GET if request.method == 'GET' else request.POST survey_batch_filter_form = SurveyBatchFilterForm(data=request_data) locations_filter = LocationsFilterForm(data=request_data) last_selected_loc = locations_filter.last_location_selected if request_data and request_data.get('action'): survey_batch_filter_form = SurveyBatchFilterForm(data=request_data) if survey_batch_filter_form.is_valid(): batch = survey_batch_filter_form.cleaned_data['batch'] survey = survey_batch_filter_form.cleaned_data['survey'] multi_option = \ survey_batch_filter_form.cleaned_data['multi_option'] restricted_to = None if last_selected_loc: restricted_to = [last_selected_loc, ] if request_data.get('action') == 'Email Spreadsheet': composer = ResultComposer( request.user, ResultsDownloadService( batch, survey=survey, restrict_to=restricted_to, multi_display=multi_option)) send_mail.delay(composer) messages.warning( request, "Email would be sent to\ you shortly. This could take a while.") else: download_service = ResultsDownloadService( batch, survey=survey, restrict_to=restricted_to, multi_display=multi_option) file_name = '%s%s' % ('%s-%s-' % ( last_selected_loc.type.name, last_selected_loc.name) if last_selected_loc else '', batch.name if batch else survey.name) reports_df = download_service.generate_interview_reports() response = HttpResponse(content_type='application/zip') string_buf = StringIO() reports_df.to_csv(string_buf, columns=reports_df.columns[1:]) string_buf.seek(0) file_contents = string_buf.read() string_buf.close() zip_file = InMemoryZip() zip_file = zip_file.append("%s.csv" % file_name, file_contents) response['Content-Disposition'] = 'attachment;\ filename=%s.zip' % file_name response.write(zip_file.read()) # exclude interview id if not request.is_ajax(): messages.info(request, "Download successfully downloaded") return response loc_types = LocationType.in_between() return render(request, 'aggregates/download_excel.html', {'survey_batch_filter_form': survey_batch_filter_form, 'locations_filter': locations_filter, 'export_url': '%s?%s' % (reverse('excel_report'), request.META['QUERY_STRING']), 'location_filter_types': loc_types})