Example #1
0
    def test_select_one_choices(self):
        path = os.path.join(os.path.dirname(__file__), "..", "..", "..",
                            "apps", "api", "tests", "fixtures", "forms",
                            "select_one_choices_test.xlsx")

        self._publish_xls_file_and_set_xform(path)

        path = os.path.join(os.path.dirname(__file__), "..", "..", "..",
                            "apps", "api", "tests", "fixtures",
                            "select_one_choices_instance.xml")

        self._make_submission(path)

        field = find_field_by_name(self.xform, 'name_I')

        choices = get_field_choices(field, self.xform)
        data = build_chart_data_for_field(self.xform, field, choices=choices)

        expected_data = {
            'field_type': u'select one',
            'data_type': 'categorized',
            'field_xpath': u'name_I',
            'data': [{
                'count': 1L,
                'name_I': [u'Aynalem Tenaw']
            }],
            'grouped_by': None,
            'field_label': u'Name of interviewer',
            'field_name': u'name_I'
        }

        self.assertEqual(data, expected_data)
Example #2
0
    def test_select_one_choices(self):
        path = os.path.join(
            os.path.dirname(__file__), "..", "..", "..", "apps", "api",
            "tests", "fixtures", "forms", "select_one_choices_test.xlsx")

        self._publish_xls_file_and_set_xform(path)

        path = os.path.join(
            os.path.dirname(__file__), "..", "..", "..", "apps", "api",
            "tests", "fixtures", "select_one_choices_instance.xml")

        self._make_submission(path)

        field = find_field_by_name(self.xform, 'name_I')

        choices = get_field_choices(field, self.xform)
        data = build_chart_data_for_field(self.xform, field, choices=choices)

        expected_data = {
            'field_type': 'select one',
            'data_type': 'categorized',
            'field_xpath': 'name_I',
            'data': [{
                'count': 1,
                'name_I': ['Aynalem Tenaw']
            }],
            'grouped_by': None,
            'field_label': 'Name of interviewer',
            'field_name': 'name_I'
        }

        self.assertEqual(data, expected_data)
Example #3
0
    def test_select_one_choices_group_by(self):
        path = os.path.join(os.path.dirname(__file__), "..", "..", "..",
                            "apps", "api", "tests", "fixtures", "forms",
                            "select_one_choices_test.xlsx")

        self._publish_xls_file_and_set_xform(path)

        path = os.path.join(os.path.dirname(__file__), "..", "..", "..",
                            "apps", "api", "tests", "fixtures",
                            "select_one_choices_instance.xml")

        self._make_submission(path)

        group_by = find_field_by_name(self.xform, 'name_I')
        field = find_field_by_name(self.xform, 'toexppc')

        choices = get_field_choices(field, self.xform)

        data = build_chart_data_for_field(self.xform,
                                          field,
                                          choices=choices,
                                          group_by=group_by)

        expected_data = {
            'field_type':
            u'calculate',
            'data_type':
            'numeric',
            'field_xpath':
            u'toexppc',
            'data': [{
                'count': 1,
                'sum': Decimal('3.357142857142857'),
                'name_I': [u'Aynalem Tenaw'],
                'mean': Decimal('3.3571428571428570')
            }],
            'grouped_by':
            u'name_I',
            'field_label':
            u'Total expenditure per capita',
            'field_name':
            u'toexppc'
        }

        self.assertEqual(data, expected_data)
Example #4
0
    def test_select_one_choices_group_by(self):
        path = os.path.join(
            os.path.dirname(__file__), "..", "..", "..", "apps", "api",
            "tests", "fixtures", "forms", "select_one_choices_test.xlsx")

        self._publish_xls_file_and_set_xform(path)

        path = os.path.join(
            os.path.dirname(__file__), "..", "..", "..", "apps", "api",
            "tests", "fixtures", "select_one_choices_instance.xml")

        self._make_submission(path)

        group_by = find_field_by_name(self.xform, 'name_I')
        field = find_field_by_name(self.xform, 'toexppc')

        choices = get_field_choices(field, self.xform)

        data = build_chart_data_for_field(
            self.xform, field, choices=choices, group_by=group_by)

        expected_data = {
            'field_type':
            'calculate',
            'data_type':
            'numeric',
            'field_xpath':
            'toexppc',
            'data': [{
                'count': 1,
                'sum': Decimal('3.357142857142857'),
                'name_I': ['Aynalem Tenaw'],
                'mean': Decimal('3.3571428571428570')
            }],
            'grouped_by':
            'name_I',
            'field_label':
            'Total expenditure per capita',
            'field_name':
            'toexppc'
        }

        self.assertEqual(data, expected_data)
Example #5
0
    def query_data(cls, widget):
        # get the columns needed
        column = widget.column
        group_by = widget.group_by if widget.group_by else None

        if isinstance(widget.content_object, XForm):
            xform = widget.content_object
        elif isinstance(widget.content_object, DataView):
            xform = widget.content_object.xform

        field = get_field_from_field_xpath(column, xform)

        if isinstance(field, basestring) and field == SUBMISSION_TIME:
            field_label = 'Submission Time'
            field_xpath = '_submission_time'
            field_type = 'datetime'
            data_type = DATA_TYPE_MAP.get(field_type, 'categorized')
        else:
            field_type = field.type
            data_type = DATA_TYPE_MAP.get(field.type, 'categorized')
            field_xpath = field.get_abbreviated_xpath()
            field_label = get_field_label(field)

        columns = [
            SimpleField(field="json->>'%s'" % unicode(column),
                        alias='"{}"'.format(column)),
            CountField(field="json->>'%s'" % unicode(column), alias='"count"')
        ]
        if group_by:
            if field_type in NUMERIC_LIST:
                column_field = SimpleField(field="json->>'%s'" %
                                           unicode(column),
                                           cast="float",
                                           alias=column)
            else:
                column_field = SimpleField(field="json->>'%s'" %
                                           unicode(column),
                                           alias=column)

            # build inner query
            inner_query_columns = \
                [column_field,
                 SimpleField(field="json->>'%s'" % unicode(group_by),
                             alias=group_by),
                 SimpleField(field="xform_id"),
                 SimpleField(field="deleted_at")]
            inner_query = Query().from_table(Instance, inner_query_columns)

            # build group-by query
            if field_type in NUMERIC_LIST:
                columns = [
                    SimpleField(field=group_by, alias='"%s"' % group_by),
                    SumField(field=column, alias="sum"),
                    AvgField(field=column, alias="mean")
                ]
            elif field_type == SELECT_ONE:
                columns = [
                    SimpleField(field=column, alias='"%s"' % column),
                    SimpleField(field=group_by, alias='"%s"' % group_by),
                    CountField(field="*", alias='"count"')
                ]

            query = Query().from_table({'inner_query': inner_query}, columns).\
                where(xform_id=xform.pk, deleted_at=None)

            if field_type == SELECT_ONE:
                query.group_by(column).group_by(group_by)
            else:
                query.group_by(group_by)

        else:
            query = Query().from_table(Instance, columns).\
                where(xform_id=xform.pk, deleted_at=None)
            query.group_by("json->>'%s'" % unicode(column))

        # run query
        records = query.select()

        # flatten multiple dict if select one with group by
        if field_type == SELECT_ONE and group_by:
            records = _flatten_multiple_dict_into_one(column, group_by,
                                                      records)
        # use labels if group by
        if group_by:
            group_by_field = get_field_from_field_xpath(group_by, xform)
            choices = get_field_choices(group_by, xform)
            records = _use_labels_from_group_by_name(group_by,
                                                     group_by_field,
                                                     data_type,
                                                     records,
                                                     choices=choices)
        return {
            "field_type": field_type,
            "data_type": data_type,
            "field_xpath": field_xpath,
            "field_label": field_label,
            "grouped_by": group_by,
            "data": records
        }
Example #6
0
    def query_data(cls, widget):
        # get the columns needed
        column = widget.column
        group_by = widget.group_by if widget.group_by else None

        if isinstance(widget.content_object, XForm):
            xform = widget.content_object
        elif isinstance(widget.content_object, DataView):
            xform = widget.content_object.xform

        field = get_field_from_field_xpath(column, xform)

        if isinstance(field, basestring) and field == SUBMISSION_TIME:
            field_label = 'Submission Time'
            field_xpath = '_submission_time'
            field_type = 'datetime'
            data_type = DATA_TYPE_MAP.get(field_type, 'categorized')
        else:
            field_type = field.type
            data_type = DATA_TYPE_MAP.get(field.type, 'categorized')
            field_xpath = field.get_abbreviated_xpath()
            field_label = get_field_label(field)

        columns = [
            SimpleField(
                field="json->>'%s'" % text(column),
                alias='{}'.format(column)),
            CountField(
                field="json->>'%s'" % text(column),
                alias='count')
        ]

        if group_by:
            if field_type in NUMERIC_LIST:
                column_field = SimpleField(
                    field="json->>'%s'" % text(column),
                    cast="float",
                    alias=column)
            else:
                column_field = SimpleField(
                    field="json->>'%s'" % text(column), alias=column)

            # build inner query
            inner_query_columns = \
                [column_field,
                 SimpleField(field="json->>'%s'" % text(group_by),
                             alias=group_by),
                 SimpleField(field="xform_id"),
                 SimpleField(field="deleted_at")]
            inner_query = Query().from_table(Instance, inner_query_columns)

            # build group-by query
            if field_type in NUMERIC_LIST:
                columns = [
                    SimpleField(field=group_by, alias='%s' % group_by),
                    SumField(field=column, alias="sum"),
                    AvgField(field=column, alias="mean")
                ]
            elif field_type == SELECT_ONE:
                columns = [
                    SimpleField(field=column, alias='%s' % column),
                    SimpleField(field=group_by, alias='%s' % group_by),
                    CountField(field="*", alias='count')
                ]

            query = Query().from_table({'inner_query': inner_query}, columns).\
                where(xform_id=xform.pk, deleted_at=None)

            if field_type == SELECT_ONE:
                query.group_by(column).group_by(group_by)
            else:
                query.group_by(group_by)

        else:
            query = Query().from_table(Instance, columns).\
                where(xform_id=xform.pk, deleted_at=None)
            query.group_by("json->>'%s'" % text(column))

        # run query
        records = query.select()

        # flatten multiple dict if select one with group by
        if field_type == SELECT_ONE and group_by:
            records = _flatten_multiple_dict_into_one(column, group_by,
                                                      records)
        # use labels if group by
        if group_by:
            group_by_field = get_field_from_field_xpath(group_by, xform)
            choices = get_field_choices(group_by, xform)
            records = _use_labels_from_group_by_name(
                group_by, group_by_field, data_type, records, choices=choices)

        return {
            "field_type": field_type,
            "data_type": data_type,
            "field_xpath": field_xpath,
            "field_label": field_label,
            "grouped_by": group_by,
            "data": records
        }