def _view_qset_data(request, model_class, interviews, title, disabled_fields=[]): params = request.GET if request.method == 'GET' else request.POST survey_filter = SurveyResultsFilterForm(model_class, disabled_fields=disabled_fields, data=params) locations_filter = LocationsFilterForm(data=request.GET, include_ea=True) selected_qset = None survey = None items_per_page = int(params.get('max_display_per_page', 50)) try: page_index = int(params.get('page', 1)) - 1 except BaseException: page_index = 0 if survey_filter.is_valid(): interviews = survey_filter.get_interviews(interviews=interviews) selected_qset = survey_filter.cleaned_data['question_set'] survey = survey_filter.cleaned_data['survey'] if locations_filter.is_valid(): interviews = interviews.filter( ea__in=locations_filter.get_enumerations()) search_fields = [ 'ea__name', 'survey__name', 'question_set__name', 'answer__as_text', ] if 'q' in request.GET: interviews = get_filterset(interviews, request.GET['q'], search_fields) context = { 'survey_filter': survey_filter, 'interviews': interviews, 'locations_filter': locations_filter, 'location_filter_types': LocationType.in_between(), 'placeholder': 'Response, EA, Survey, %s' % model_class.verbose_name(), 'selected_qset': selected_qset, 'model_class': model_class, 'items_per_page': items_per_page, 'max_display_per_page': items_per_page, 'title': title } if selected_qset and survey: # page_start = page_index * items_per_page # interviews = interviews[page_start: page_start + items_per_page]() download_service = ResultsDownloadService( selected_qset, survey=survey, interviews=interviews, page_index=page_index, items_per_page=items_per_page) df = download_service.get_interview_answers() context['report'] = mark_safe( df.to_html(classes='table table-striped\ dataTable table-bordered table-hover table-sort', max_rows=items_per_page)) return render(request, 'question_set/view_all_data.html', context)
def _view_qset_data(request, model_class, interviews,title, disabled_fields=[]): params = request.GET if request.method == 'GET' else request.POST survey_filter = SurveyResultsFilterForm( model_class, disabled_fields=disabled_fields, data=params) locations_filter = LocationsFilterForm(data=request.GET, include_ea=True) selected_qset = None survey = None items_per_page = int(params.get('max_display_per_page', 50)) try: page_index = int(params.get('page', 1)) - 1 except BaseException: page_index = 0 if survey_filter.is_valid(): interviews = survey_filter.get_interviews(interviews=interviews) selected_qset = survey_filter.cleaned_data['question_set'] survey = survey_filter.cleaned_data['survey'] if locations_filter.is_valid(): interviews = interviews.filter(ea__in=locations_filter.get_enumerations()) search_fields = [ 'ea__name', 'survey__name', 'question_set__name', 'answer__as_text', ] if 'q' in request.GET: interviews = get_filterset(interviews, request.GET['q'], search_fields) context = { 'survey_filter': survey_filter, 'interviews': interviews, 'locations_filter': locations_filter, 'location_filter_types': LocationType.in_between(), 'placeholder': 'Response, EA, Survey, %s' % model_class.verbose_name(), 'selected_qset': selected_qset, 'model_class': model_class, 'items_per_page': items_per_page, 'max_display_per_page': items_per_page, 'title':title} if selected_qset and survey: # page_start = page_index * items_per_page # interviews = interviews[page_start: page_start + items_per_page]() download_service = ResultsDownloadService( selected_qset, survey=survey, interviews=interviews, page_index=page_index, items_per_page=items_per_page) df = download_service.get_interview_answers() context['report'] = mark_safe( df.to_html( classes='table table-striped\ dataTable table-bordered table-hover table-sort', max_rows=items_per_page)) return render(request, 'question_set/view_all_data.html', context)
def show_interviewer_completion_summary(request): params = request.GET selected_location = None selected_ea = None interviewers = Interviewer.objects.order_by('id') search_fields = ['name', 'ea__name'] if request.GET.has_key('q'): interviewers = get_filterset(interviewers, request.GET['q'], search_fields) if params.has_key('status'): interviewers = interviewers.intervieweraccess.filter(is_active=ast.literal_eval(params['status'])) locations_filter = LocFilterForm(data=request.GET, include_ea=True) if locations_filter.is_valid(): print 'locations count: ', locations_filter.get_enumerations().count() interviewers = interviewers.filter(ea__in=locations_filter.get_enumerations()).order_by('name') return render(request, 'aggregates/interviewers_summary.html', {'interviewers': interviewers, 'locations_filter' : locations_filter, 'request': request})
def show_interviewer_completion_summary(request): params = request.GET selected_location = None selected_ea = None interviewers = Interviewer.objects.order_by('id') search_fields = ['name', 'ea__name'] if request.GET.has_key('q'): interviewers = get_filterset(interviewers, request.GET['q'], search_fields) if params.has_key('status'): interviewers = interviewers.intervieweraccess.filter(is_active=ast.literal_eval(params['status'])) locations_filter = LocFilterForm(data=request.GET, include_ea=True) if locations_filter.is_valid(): print 'locations count: ', locations_filter.get_enumerations().count() interviewers = interviewers.filter(ea__in=locations_filter.get_enumerations()).order_by('name') return render(request, 'aggregates/interviewers_summary.html', {'interviewers': interviewers, 'locations_filter' : locations_filter, 'request': request})
def list_interviewers(request): params = request.GET locations_filter = LocationsFilterForm(data=request.GET, include_ea=True) if locations_filter.is_valid(): interviewers = Interviewer.objects.filter(ea__in=locations_filter.get_enumerations()).order_by('name') else: interviewers = Interviewer.objects.all() search_fields = ['name', 'intervieweraccess__user_identifier'] if request.GET.has_key('q'): interviewers = get_filterset(interviewers, request.GET['q'], search_fields) if params.has_key('status'): interviewers = interviewers.filter(is_blocked=ast.literal_eval(params['status'])) loc_types = LocationType.in_between() return render(request, 'interviewers/index.html', {'interviewers': interviewers, 'locations_filter' : locations_filter, 'location_filter_types' : loc_types, 'placeholder': 'name, mobile numbers, odk id', 'request': request})
def list_interviewers(request): params = request.GET locations_filter = LocationsFilterForm(data=request.GET, include_ea=True) if locations_filter.is_valid(): interviewers = Interviewer.objects.filter( ea__in=locations_filter.get_enumerations()) else: interviewers = Interviewer.objects.all() search_fields = ['name', 'intervieweraccess__user_identifier'] if 'q' in request.GET: interviewers = get_filterset( interviewers, request.GET['q'], search_fields) if 'status' in params: interviewers = interviewers.filter( is_blocked=ast.literal_eval(params['status'])) loc_types = LocationType.in_between() return render(request, 'interviewers/index.html', {'interviewers': interviewers.order_by('name'), 'locations_filter': locations_filter, 'location_filter_types': loc_types, 'placeholder': 'name, mobile numbers, odk id', 'request': request})
def test_locations_filter_form(self): ea = EnumerationArea.objects.last() location = ea.locations.first() ea_locations = location.get_ancestors(include_self=True) data = {location.type.name: location.id for location in ea_locations} location_filter = LocationsFilterForm(data=data) self.assertNotIn('enumeration_area', location_filter.fields) eas = location_filter.get_enumerations() # eas basically returns all EAs as per the immediate parent to the smallest unit self.assertEquals( eas.count(), EnumerationArea.objects.filter( locations__parent__in=ea_locations).distinct().count()) self.assertEquals(eas.filter(id=ea.id).count(), 1) location_filter = LocationsFilterForm(data=data, include_ea=True) self.assertEquals(eas.filter(id=ea.id).count(), 1) self.assertIn('enumeration_area', location_filter.fields) data['enumeration_area'] = ea.id location_filter = LocationsFilterForm(data=data, include_ea=True) self.assertTrue(location_filter.is_valid()) eas = location_filter.get_enumerations() self.assertEquals(eas.count(), 1)
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