def to_native(self, obj): data = {} request = self.context.get('request') if obj is not None: dd = obj.data_dictionary() fields = dd.survey_elements if request: selected_fields = request.QUERY_PARAMS.get('fields') if isinstance(selected_fields, basestring) \ and selected_fields != 'all': fields = selected_fields.split(',') fields = [e for e in dd.survey_elements if e.name in fields] if len(fields) == 0: raise Http404( "Field %s does not not exist on the form" % fields) for field in fields: if field.name == INSTANCE_ID: continue field_data = build_chart_data_for_field(obj, field) data[field.name] = field_data return data
def test_build_chart_data_for_field_on_grouped_field(self): dd = self.xform.data_dictionary() field = find_field_by_name(dd, 'a_text') data = build_chart_data_for_field(self.xform, field) self.assertEqual(data['field_name'], 'a_group-a_text') self.assertEqual(data['field_xpath'], 'a_text') self.assertEqual(data['field_type'], 'text') self.assertEqual(data['data_type'], 'categorized')
def test_build_chart_data_for_field_on_grouped_field(self): dd = self.xform.data_dictionary() field = find_field_by_name(dd, 'a_text') data = build_chart_data_for_field(self.xform, field) self.assertEqual(data['field_name'], 'a_group-a_text') self.assertEqual(data['field_xpath'], 'a_text') self.assertEqual(data['field_type'], 'text') self.assertEqual(data['data_type'], 'categorized')
def test_build_chart_data_with_field_name_with_lengh_65(self): # make the 3rd submission that doesnt have a date path = os.path.join(os.path.dirname(__file__), "..", "..", "..", "apps", "api", "tests", "fixtures", "forms", "tutorial", "instances", "3.xml") self._make_submission(path) dd = self.xform.data_dictionary() field = find_field_by_name(dd, 'date') field.name = 'a' * 65 data = build_chart_data_for_field(self.xform, field) self.assertEqual(data['field_name'], field.name)
def test_build_chart_data_strips_none_from_dates(self): # make the 3rd submission that doesnt have a date path = os.path.join(os.path.dirname(__file__), "..", "..", "..", "apps", "api", "tests", "fixtures", "forms", "tutorial", "instances", "3.xml") self._make_submission(path) dd = self.xform.data_dictionary() field = find_field_by_name(dd, 'date') data = build_chart_data_for_field(self.xform, field) # create a list with comparisons to the dict values values = [d['date'] is not None for d in data['data']] self.assertTrue(all(values))
def test_build_chart_data_with_field_name_with_lengh_65(self): # make the 3rd submission that doesnt have a date path = os.path.join(os.path.dirname(__file__), "..", "..", "..", "apps", "api", "tests", "fixtures", "forms", "tutorial", "instances", "3.xml") self._make_submission(path) dd = self.xform.data_dictionary() field = find_field_by_name(dd, 'date') field.name = 'a' * 65 data = build_chart_data_for_field(self.xform, field) self.assertEqual(data['field_name'], field.name)
def test_build_chart_data_strips_none_from_dates(self): # make the 3rd submission that doesnt have a date path = os.path.join(os.path.dirname(__file__), "..", "..", "..", "apps", "api", "tests", "fixtures", "forms", "tutorial", "instances", "3.xml") self._make_submission(path) dd = self.xform.data_dictionary() field = find_field_by_name(dd, 'date') data = build_chart_data_for_field(self.xform, field) # create a list with comparisons to the dict values values = [d['date'] is not None for d in data['data']] self.assertTrue(all(values))
def test_build_chart_data_for_field_with_language_on_non_lang_field(self): path = os.path.join(os.path.dirname(__file__), "..", "..", "..", "apps", "main", "tests", "fixtures", "good_eats_multilang", "good_eats_multilang.xls") self._publish_xls_file_and_set_xform(path) path = os.path.join(os.path.dirname(__file__), "..", "..", "..", "apps", "main", "tests", "fixtures", "good_eats_multilang", "1.xml") self._make_submission(path) dd = self.xform.data_dictionary() field = find_field_by_name(dd, 'submit_date') data = build_chart_data_for_field(self.xform, field, language_index=1) self.assertEqual(data['field_label'], 'submit_date')
def test_build_chart_data_for_fields_with_accents(self): xls_path = os.path.join(self.this_directory, "fixtures", "sample_accent.xlsx") response = self._publish_xls_file(xls_path) self.assertEquals(response.status_code, 200) xform = XForm.objects.all()[0] self.assertEqual(xform.title, "sample_accent") dd = xform.data_dictionary() field = find_field_by_name(dd, u'tête') data = build_chart_data_for_field(self.xform, field) self.assertEqual(data['field_name'], u'words_with_accents-tête') field = find_field_by_name(dd, u'té') data = build_chart_data_for_field(self.xform, field) self.assertEqual(data['field_name'], u'words_with_accents-té') field = find_field_by_name(dd, u'père') data = build_chart_data_for_field(self.xform, field) self.assertEqual(data['field_name'], u'words_with_accents-père')
def test_build_chart_data_for_field_with_language_on_non_lang_field(self): path = os.path.join(os.path.dirname(__file__), "..", "..", "..", "apps", "main", "tests", "fixtures", "good_eats_multilang", "good_eats_multilang.xls") self._publish_xls_file_and_set_xform(path) path = os.path.join(os.path.dirname(__file__), "..", "..", "..", "apps", "main", "tests", "fixtures", "good_eats_multilang", "1.xml") self._make_submission(path) dd = self.xform.data_dictionary() field = find_field_by_name(dd, 'submit_date') data = build_chart_data_for_field(self.xform, field, language_index=1) self.assertEqual(data['field_label'], 'submit_date')
def test_build_chart_data_for_field_on_select_one(self): field_name = 'gender' dd = self.xform.data_dictionary() field = find_field_by_name(dd, field_name) data = build_chart_data_for_field(self.xform, field) self.assertEqual(data['field_name'], field_name) self.assertEqual(data['field_type'], 'select one') self.assertEqual(data['data_type'], 'categorized') # map the list to a dict for d in data['data']: genders = d[field_name] count = d['count'] self.assertEqual(type(genders), list) self.assertEqual(count, 1)
def test_build_chart_data_for_fields_with_accents(self): xls_path = os.path.join( self.this_directory, "fixtures", "sample_accent.xlsx") response = self._publish_xls_file(xls_path) self.assertEquals(response.status_code, 200) xform = XForm.objects.all()[0] self.assertEqual(xform.title, "sample_accent") dd = xform.data_dictionary() field = find_field_by_name(dd, u'tête') data = build_chart_data_for_field(self.xform, field) self.assertEqual(data['field_name'], u'words_with_accents-tête') field = find_field_by_name(dd, u'té') data = build_chart_data_for_field(self.xform, field) self.assertEqual(data['field_name'], u'words_with_accents-té') field = find_field_by_name(dd, u'père') data = build_chart_data_for_field(self.xform, field) self.assertEqual(data['field_name'], u'words_with_accents-père')
def test_build_chart_data_for_field_on_select_one(self): field_name = 'gender' dd = self.xform.data_dictionary() field = find_field_by_name(dd, field_name) data = build_chart_data_for_field(self.xform, field) self.assertEqual(data['field_name'], field_name) self.assertEqual(data['field_type'], 'select one') self.assertEqual(data['data_type'], 'categorized') # map the list to a dict for d in data['data']: genders = d[field_name] count = d['count'] self.assertEqual(type(genders), list) self.assertEqual(count, 1)
def retrieve(self, request, *args, **kwargs): xform = self.get_object() serializer = self.get_serializer(xform) dd = xform.data_dictionary() field_name = request.QUERY_PARAMS.get('field_name') fields = request.QUERY_PARAMS.get('fields') fmt = kwargs.get('format') if fields: if fmt is not None and fmt != 'json': raise ParseError("Error: only JSON format supported.") xform = self.get_object() context = self.get_serializer_context() serializer = FieldsChartSerializer(instance=xform, context=context) return Response(serializer.data) if field_name: # check if its the special _submission_time META if field_name == common_tags.SUBMISSION_TIME: field = common_tags.SUBMISSION_TIME else: # use specified field to get summary fields = filter( lambda f: f.name == field_name, [e for e in dd.survey_elements]) if len(fields) == 0: raise Http404( "Field %s does not not exist on the form" % field_name) field = fields[0] choices = dd.survey.get('choices') if choices: choices = choices.get(field_name) data = build_chart_data_for_field(xform, field, choices=choices) if request.accepted_renderer.format == 'json': xform = xform.pk elif request.accepted_renderer.format == 'html' and 'data' in data: for item in data['data']: if isinstance(item[field_name], list): item[field_name] = u', '.join(item[field_name]) data.update({ 'xform': xform }) return Response(data, template_name='chart_detail.html') if fmt != 'json' and field_name is None: raise ParseError("Not supported") data = serializer.data data["fields"] = {} for field in dd.survey_elements: field_url = get_form_field_chart_url(data["url"], field.name) data["fields"][field.name] = field_url return Response(data)
def retrieve(self, request, *args, **kwargs): xform = self.get_object() serializer = self.get_serializer(xform) dd = xform.data_dictionary() field_name = request.QUERY_PARAMS.get('field_name') fields = request.QUERY_PARAMS.get('fields') fmt = kwargs.get('format') if fields: if fmt is not None and fmt != 'json': raise ParseError("Error: only JSON format supported.") xform = self.get_object() context = self.get_serializer_context() serializer = FieldsChartSerializer(instance=xform, context=context) return Response(serializer.data) if field_name: # check if its the special _submission_time META if field_name == common_tags.SUBMISSION_TIME: field = common_tags.SUBMISSION_TIME else: # use specified field to get summary fields = filter(lambda f: f.name == field_name, [e for e in dd.survey_elements]) if len(fields) == 0: raise Http404("Field %s does not not exist on the form" % field_name) field = fields[0] choices = dd.survey.get('choices') if choices: choices = choices.get(field_name) data = build_chart_data_for_field(xform, field, choices=choices) if request.accepted_renderer.format == 'json': xform = xform.pk elif request.accepted_renderer.format == 'html' and 'data' in data: for item in data['data']: if isinstance(item[field_name], list): item[field_name] = u', '.join(item[field_name]) data.update({'xform': xform}) return Response(data, template_name='chart_detail.html') if fmt != 'json' and field_name is None: raise ParseError("Not supported") data = serializer.data data["fields"] = {} for field in dd.survey_elements: field_url = get_form_field_chart_url(data["url"], field.name) data["fields"][field.name] = field_url return Response(data)
def test_build_chart_data_for_field_on_submission_time(self): data = build_chart_data_for_field(self.xform, '_submission_time') self.assertEqual(data['field_name'], '_submission_time') self.assertEqual(data['field_type'], 'datetime') self.assertEqual(data['data_type'], 'time_based')
def test_build_chart_data_for_field_on_submission_time(self): data = build_chart_data_for_field(self.xform, '_submission_time') self.assertEqual(data['field_name'], '_submission_time') self.assertEqual(data['field_type'], 'datetime') self.assertEqual(data['data_type'], 'time_based')