Esempio n. 1
0
	def test_xlsx_data_with_multiple_datatypes(self):
		"""Test exporting report using rows with multiple datatypes (list, dict)"""

		# Describe the columns
		columns = {
			0: {"label": "Column A", "fieldname": "column_a"},
			1: {"label": "Column B", "fieldname": "column_b"},
			2: {"label": "Column C", "fieldname": "column_c"}
		}

		# Create mock data
		data = frappe._dict()
		data.columns = ["column_a", "column_b", "column_c"]
		data.result = [
			[1.0, 3.0, 5.5],
			{"column_a": 22.1, "column_b": 21.8, "column_c": 30.2},
			{"column_b": 5.1, "column_c": 9.5, "column_a": 11.1},
			[3.0, 1.5, 7.5],
		]

		# Define the visible rows
		visible_idx = [0, 2, 3]

		# Build the result
		xlsx_data = build_xlsx_data(columns, data, visible_idx, include_indentation=0)

		self.assertEqual(type(xlsx_data), list)
		self.assertEqual(len(xlsx_data), 4)  # columns + data

		for row in xlsx_data:
			self.assertEqual(type(row), list)
Esempio n. 2
0
	def test_xlsx_data_with_multiple_datatypes(self):
		"""Test exporting report using rows with multiple datatypes (list, dict)"""

		# Describe the columns
		columns = {
			0: {"label": "Column A", "fieldname": "column_a"},
			1: {"label": "Column B", "fieldname": "column_b"},
			2: {"label": "Column C", "fieldname": "column_c"}
		}

		# Create mock data
		data = frappe._dict()
		data.columns = ["column_a", "column_b", "column_c"]
		data.result = [
			[1.0, 3.0, 5.5],
			{"column_a": 22.1, "column_b": 21.8, "column_c": 30.2},
			{"column_b": 5.1, "column_c": 9.5, "column_a": 11.1},
			[3.0, 1.5, 7.5],
		]

		# Define the visible rows
		visible_idx = [0, 2, 3]

		# Build the result
		xlsx_data = build_xlsx_data(columns, data, visible_idx)

		self.assertEqual(type(xlsx_data), list)
		self.assertEqual(len(xlsx_data), 4)  # columns + data

		for row in xlsx_data:
			self.assertEqual(type(row), list)
Esempio n. 3
0
    def wrapper(data):
        report_name = data.get('report_name')
        include_indentation = data.get('include_indentation')

        filters = json.loads(data.get('filters'))
        visible_idx = json.loads(data.get('visible_idx'))

        report_data = frappe._dict(run(report_name, filters))
        columns = get_columns_dict(report_data.columns)

        xlsx_data = build_xlsx_data(columns, report_data, visible_idx,
                                    include_indentation)

        row_length = len(xlsx_data[0])

        build = partial(build_xlsx, report_name, xlsx_data)

        return func(build, filters, row_length)
Esempio n. 4
0
    def get_report_content(self):
        """Returns file in for the report in given format"""
        report = frappe.get_doc("Report", self.report)

        self.filters = frappe.parse_json(self.filters) if self.filters else {}

        if self.report_type == "Report Builder" and self.data_modified_till:
            self.filters["modified"] = (
                ">", now_datetime() - timedelta(hours=self.data_modified_till))

        if self.report_type != "Report Builder" and self.dynamic_date_filters_set(
        ):
            self.prepare_dynamic_filters()

        columns, data = report.get_data(
            limit=self.no_of_rows or 100,
            user=self.user,
            filters=self.filters,
            as_dict=True,
            ignore_prepared_report=True,
        )

        # add serial numbers
        columns.insert(0, frappe._dict(fieldname="idx", label="",
                                       width="30px"))
        for i in range(len(data)):
            data[i]["idx"] = i + 1

        if len(data) == 0 and self.send_if_data:
            return None

        if self.format == "HTML":
            columns, data = make_links(columns, data)
            columns = update_field_types(columns)
            return self.get_html_table(columns, data)

        elif self.format == "XLSX":
            report_data = frappe._dict()
            report_data["columns"] = columns
            report_data["result"] = data

            xlsx_data, column_widths = build_xlsx_data(columns,
                                                       report_data, [],
                                                       1,
                                                       ignore_visible_idx=True)
            xlsx_file = make_xlsx(xlsx_data,
                                  "Auto Email Report",
                                  column_widths=column_widths)
            return xlsx_file.getvalue()

        elif self.format == "CSV":
            report_data = frappe._dict()
            report_data["columns"] = columns
            report_data["result"] = data

            xlsx_data, column_widths = build_xlsx_data(columns,
                                                       report_data, [],
                                                       1,
                                                       ignore_visible_idx=True)
            return to_csv(xlsx_data)

        else:
            frappe.throw(_("Invalid Output Format"))
    def get_report_content(self):
        '''Returns file in for the report in given format'''
        report = frappe.get_doc('Report', self.report)

        self.filters = frappe.parse_json(self.filters) if self.filters else {}

        if self.report_type == 'Report Builder' and self.data_modified_till:
            self.filters['modified'] = (
                '>', now_datetime() - timedelta(hours=self.data_modified_till))

        if self.report_type != 'Report Builder' and self.dynamic_date_filters_set(
        ):
            self.prepare_dynamic_filters()

        columns, data = report.get_data(limit=self.no_of_rows or 100,
                                        user=self.user,
                                        filters=self.filters,
                                        as_dict=True,
                                        ignore_prepared_report=True)

        # add serial numbers
        columns.insert(0, frappe._dict(fieldname='idx', label='',
                                       width='30px'))
        for i in range(len(data)):
            data[i]['idx'] = i + 1

        if len(data) == 0 and self.send_if_data:
            return None

        if self.format == 'HTML':
            columns, data = make_links(columns, data)
            columns = update_field_types(columns)
            return self.get_html_table(columns, data)

        elif self.format == 'XLSX':
            report_data = frappe._dict()
            report_data['columns'] = columns
            report_data['result'] = data

            xlsx_data, column_widths = build_xlsx_data(columns,
                                                       report_data, [],
                                                       1,
                                                       ignore_visible_idx=True)
            xlsx_file = make_xlsx(xlsx_data,
                                  "Auto Email Report",
                                  column_widths=column_widths)
            return xlsx_file.getvalue()

        elif self.format == 'CSV':
            report_data = frappe._dict()
            report_data['columns'] = columns
            report_data['result'] = data

            xlsx_data, column_widths = build_xlsx_data(columns,
                                                       report_data, [],
                                                       1,
                                                       ignore_visible_idx=True)
            return to_csv(xlsx_data)

        else:
            frappe.throw(_('Invalid Output Format'))
Esempio n. 6
0
    def test_xlsx_data_with_multiple_datatypes(self):
        """Test exporting report using rows with multiple datatypes (list, dict)"""

        # Describe the columns
        columns = {
            0: {
                "label": "Column A",
                "fieldname": "column_a"
            },
            1: {
                "label": "Column B",
                "fieldname": "column_b"
            },
            2: {
                "label": "Column C",
                "fieldname": "column_c"
            },
        }

        # Create mock data
        data = frappe._dict()
        data.columns = [
            {
                "label": "Column A",
                "fieldname": "column_a"
            },
            {
                "label": "Column B",
                "fieldname": "column_b",
                "width": 150
            },
            {
                "label": "Column C",
                "fieldname": "column_c",
                "width": 100
            },
        ]
        data.result = [
            [1.0, 3.0, 5.5],
            {
                "column_a": 22.1,
                "column_b": 21.8,
                "column_c": 30.2
            },
            {
                "column_b": 5.1,
                "column_c": 9.5,
                "column_a": 11.1
            },
            [3.0, 1.5, 7.5],
        ]

        # Define the visible rows
        visible_idx = [0, 2, 3]

        # Build the result
        xlsx_data, column_widths = build_xlsx_data(columns,
                                                   data,
                                                   visible_idx,
                                                   include_indentation=0)

        self.assertEqual(type(xlsx_data), list)
        self.assertEqual(len(xlsx_data), 4)  # columns + data
        # column widths are divided by 10 to match the scale that is supported by openpyxl
        self.assertListEqual(column_widths, [0, 15, 10])

        for row in xlsx_data:
            self.assertEqual(type(row), list)