def test_totaling_with_noninteger(self):
     config_agg = ReportConfiguration(
         columns=[
             {"field": "agg_col", "aggregation": "simple", "type": "field", "calculate_total": False},
             {"field": "col_1", "aggregation": "simple", "type": "field", "calculate_total": True},
         ]
     )
     self.assertEqual(
         ["", ""],
         get_total_row(
             [{"agg_col": "agg1", "col_1": ""}, {"agg_col": "agg2", "col_1": 4}],
             config_agg.aggregation_columns,
             config_agg.report_columns,
             {},
         ),
     )
     self.assertEqual(
         ["", ""],
         get_total_row(
             [{"agg_col": "agg1", "col_1": 2}, {"agg_col": "agg2", "col_1": ""}],
             config_agg.aggregation_columns,
             config_agg.report_columns,
             {},
         ),
     )
 def test_totaling_with_noninteger(self):
     config_agg = ReportConfiguration(
         columns=[
             {
                 "field": "agg_col",
                 "aggregation": "simple",
                 "type": "field",
                 "calculate_total": False,
             },
             {
                 "field": 'col_1',
                 "aggregation": "simple",
                 "type": "field",
                 "calculate_total": True,
             },
         ],
     )
     self.assertEqual(
         ['', ''],
         get_total_row(
             [
                 {
                     'agg_col': 'agg1',
                     'col_1': '',
                 },
                 {
                     'agg_col': 'agg2',
                     'col_1': 4,
                 },
             ],
             config_agg.aggregation_columns,
             config_agg.report_columns,
             {}
         )
     )
     self.assertEqual(
         ['', ''],
         get_total_row(
             [
                 {
                     'agg_col': 'agg1',
                     'col_1': 2,
                 },
                 {
                     'agg_col': 'agg2',
                     'col_1': '',
                 },
             ],
             config_agg.aggregation_columns,
             config_agg.report_columns,
             {}
         )
     )
Example #3
0
    def export_table(self):
        try:
            data = self.data_source
            data.set_filter_values(self.filter_values)
            data.set_order_by([(o['field'], o['order']) for o in self.spec.sort_expression])
        except UserReportsError as e:
            return self.render_json_response({
                'error': e.message,
            })

        raw_rows = list(data.get_data())
        headers = [column.header for column in self.data_source.columns]

        column_id_to_expanded_column_ids = get_expanded_columns(data.column_configs, data.config)
        column_ids = []
        for column in self.spec.report_columns:
            column_ids.extend(column_id_to_expanded_column_ids.get(column.column_id, [column.column_id]))

        rows = [[raw_row[column_id] for column_id in column_ids] for raw_row in raw_rows]
        total_rows = (
            [get_total_row(
                raw_rows, data.aggregation_columns, data.column_configs,
                column_id_to_expanded_column_ids
            )]
            if data.has_total_row else []
        )
        return [
            [
                self.title,
                [headers] + rows + total_rows
            ]
        ]
Example #4
0
    def get_ajax(self, request):
        try:
            data_source = self.data_source
            if len(data_source.columns) > 50:
                raise UserReportsError(_("This report has too many columns to be displayed"))
            data_source.set_filter_values(self.filter_values)

            sort_column = request.GET.get('iSortCol_0')
            sort_order = request.GET.get('sSortDir_0', 'ASC')
            echo = int(request.GET.get('sEcho', 1))
            if sort_column and echo != 1:
                data_source.set_order_by(
                    [(data_source.column_configs[int(sort_column)].column_id, sort_order.upper())]
                )
            total_records = data_source.get_total_records()
        except UserReportsError as e:
            if settings.DEBUG:
                raise
            return self.render_json_response({
                'error': e.message,
                'aaData': [],
                'iTotalRecords': 0,
                'iTotalDisplayRecords': 0,
            })
        except TableNotFoundWarning:
            if self.spec.report_meta.created_by_builder:
                msg = _(
                    "The database table backing your report does not exist yet. "
                    "Please wait while the report is populated."
                )
            else:
                msg = _(
                    "The database table backing your report does not exist yet. "
                    "You must rebuild the data source before viewing the report."
                )
            return self.render_json_response({
                'warning': msg
            })

        # todo: this is ghetto pagination - still doing a lot of work in the database
        datatables_params = DatatablesParams.from_request_dict(request.GET)
        end = min(datatables_params.start + datatables_params.count, total_records)
        data = list(data_source.get_data())
        page = data[datatables_params.start:end]

        json_response = {
            'aaData': page,
            "sEcho": self.request_dict.get('sEcho', 0),
            "iTotalRecords": total_records,
            "iTotalDisplayRecords": total_records,
        }
        if data_source.has_total_row:
            json_response.update({
                "total_row": get_total_row(
                    data, data_source.aggregation_columns, data_source.column_configs,
                    get_expanded_columns(data_source.column_configs, data_source.config)
                ),
            })
        return self.render_json_response(json_response)
 def test_totaling_with_expanded_column(self):
     config_agg = ReportConfiguration(
         aggregation_columns=['agg_col'],
         columns=[
             {
                 "field": "agg_col",
                 "aggregation": "simple",
                 "type": "field",
                 "calculate_total": False,
             },
             {
                 "field": 'col_1',
                 "aggregation": "simple",
                 "type": "field",
                 "calculate_total": False,
             },
             {
                 "field": 'col_2',
                 "aggregation": "simple",
                 "type": "expanded",
                 "calculate_total": True,
             },
         ],
     )
     self.assertEqual(
         ['Total', '', 8, 13],
         get_total_row(
             [
                 {
                     'agg_col': 'agg1',
                     'col_1': 2,
                     'col_2-A': 3,
                     'col_2-B': 6,
                 },
                 {
                     'agg_col': 'agg2',
                     'col_1': 4,
                     'col_2-A': 5,
                     'col_2-B': 7,
                 },
             ],
             config_agg.aggregation_columns,
             config_agg.report_columns,
             {
                 "col_2": [
                     "col_2-A",
                     "col_2-B",
                 ],
             }
         )
     )
 def test_totaling_with_aggregation(self):
     config_agg = ReportConfiguration(
         aggregation_columns=["agg_col"],
         columns=[
             {"field": "agg_col", "aggregation": "simple", "type": "field", "calculate_total": False},
             {"field": "col_1", "aggregation": "simple", "type": "field", "calculate_total": False},
             {"field": "col_2", "aggregation": "simple", "type": "field", "calculate_total": True},
         ],
     )
     self.assertEqual(
         ["Total", "", 8],
         get_total_row(
             [{"agg_col": "agg1", "col_1": 2, "col_2": 3}, {"agg_col": "agg2", "col_1": 4, "col_2": 5}],
             config_agg.aggregation_columns,
             config_agg.report_columns,
             {},
         ),
     )
 def test_totaling_with_expanded_column(self):
     config_agg = ReportConfiguration(
         aggregation_columns=["agg_col"],
         columns=[
             {"field": "agg_col", "aggregation": "simple", "type": "field", "calculate_total": False},
             {"field": "col_1", "aggregation": "simple", "type": "field", "calculate_total": False},
             {"field": "col_2", "aggregation": "simple", "type": "expanded", "calculate_total": True},
         ],
     )
     self.assertEqual(
         ["Total", "", 8, 13],
         get_total_row(
             [
                 {"agg_col": "agg1", "col_1": 2, "col_2-A": 3, "col_2-B": 6},
                 {"agg_col": "agg2", "col_1": 4, "col_2-A": 5, "col_2-B": 7},
             ],
             config_agg.aggregation_columns,
             config_agg.report_columns,
             {"col_2": ["col_2-A", "col_2-B"]},
         ),
     )