From 5e7f790f87f1e3fe398c8fd02830c60924bcef51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Wed, 8 Mar 2023 21:54:42 +0800 Subject: [PATCH] Moved the __get_csv_rows method of the Journal report to the get_csv_rows function, and revised the Search report to use it, because both of their __get_csv_rows methods are identical. --- src/accounting/report/reports/journal.py | 36 +++++++++++++----------- src/accounting/report/reports/search.py | 20 ++----------- 2 files changed, 21 insertions(+), 35 deletions(-) diff --git a/src/accounting/report/reports/journal.py b/src/accounting/report/reports/journal.py index e0d2ff3..b3b6faa 100644 --- a/src/accounting/report/reports/journal.py +++ b/src/accounting/report/reports/journal.py @@ -173,6 +173,24 @@ def populate_entries(entries: list[ReportEntry]) -> None: entry.currency = currencies[entry.entry.currency_code] +def get_csv_rows(entries: list[ReportEntry]) -> list[CSVRow]: + """Composes and returns the CSV rows from the report entries. + + :param entries: The report entries. + :return: The CSV rows. + """ + populate_entries(entries) + rows: list[CSVRow] = [CSVRow(gettext("Date"), gettext("Currency"), + gettext("Account"), gettext("Summary"), + gettext("Debit"), gettext("Credit"), + gettext("Note"))] + rows.extend([CSVRow(x.transaction.date, x.currency.code, + str(x.account).title(), x.summary, + x.debit, x.credit, x.transaction.note) + for x in entries]) + return rows + + class Journal(BaseReport): """The journal.""" @@ -208,23 +226,7 @@ class Journal(BaseReport): :return: The response of the report for download. """ filename: str = f"journal-{period_spec(self.__period)}.csv" - return csv_download(filename, self.__get_csv_rows()) - - def __get_csv_rows(self) -> list[CSVRow]: - """Composes and returns the CSV rows. - - :return: The CSV rows. - """ - populate_entries(self.__entries) - rows: list[CSVRow] = [CSVRow(gettext("Date"), gettext("Currency"), - gettext("Account"), gettext("Summary"), - gettext("Debit"), gettext("Credit"), - gettext("Note"))] - rows.extend([CSVRow(x.transaction.date, x.currency.code, - str(x.account).title(), x.summary, - x.debit, x.credit, x.transaction.note) - for x in self.__entries]) - return rows + return csv_download(filename, get_csv_rows(self.__entries)) def html(self) -> str: """Composes and returns the report as HTML. diff --git a/src/accounting/report/reports/search.py b/src/accounting/report/reports/search.py index 6643694..e5a45fc 100644 --- a/src/accounting/report/reports/search.py +++ b/src/accounting/report/reports/search.py @@ -28,7 +28,7 @@ from accounting.models import Currency, CurrencyL10n, Account, AccountL10n, \ Transaction, JournalEntry from accounting.utils.pagination import Pagination from accounting.utils.query import parse_query_keywords -from .journal import ReportEntry, CSVRow, populate_entries +from .journal import ReportEntry, populate_entries, get_csv_rows from .utils.base_page_params import BasePageParams from .utils.base_report import BaseReport from .utils.csv_export import csv_download @@ -182,23 +182,7 @@ class Search(BaseReport): :return: The response of the report for download. """ filename: str = "search-{q}.csv".format(q=request.args["q"]) - return csv_download(filename, self.__get_csv_rows()) - - def __get_csv_rows(self) -> list[CSVRow]: - """Composes and returns the CSV rows. - - :return: The CSV rows. - """ - populate_entries(self.__entries) - rows: list[CSVRow] = [CSVRow(gettext("Date"), gettext("Currency"), - gettext("Account"), gettext("Summary"), - gettext("Debit"), gettext("Credit"), - gettext("Note"))] - rows.extend([CSVRow(x.transaction.date, x.currency.code, - str(x.account).title(), x.summary, - x.debit, x.credit, x.transaction.note) - for x in self.__entries]) - return rows + return csv_download(filename, get_csv_rows(self.__entries)) def html(self) -> str: """Composes and returns the report as HTML.