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.

This commit is contained in:
依瑪貓 2023-03-08 21:54:42 +08:00
parent d64f354ee0
commit 5e7f790f87
2 changed files with 21 additions and 35 deletions

View File

@ -173,6 +173,24 @@ def populate_entries(entries: list[ReportEntry]) -> None:
entry.currency = currencies[entry.entry.currency_code] 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): class Journal(BaseReport):
"""The journal.""" """The journal."""
@ -208,23 +226,7 @@ class Journal(BaseReport):
:return: The response of the report for download. :return: The response of the report for download.
""" """
filename: str = f"journal-{period_spec(self.__period)}.csv" filename: str = f"journal-{period_spec(self.__period)}.csv"
return csv_download(filename, self.__get_csv_rows()) return csv_download(filename, get_csv_rows(self.__entries))
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
def html(self) -> str: def html(self) -> str:
"""Composes and returns the report as HTML. """Composes and returns the report as HTML.

View File

@ -28,7 +28,7 @@ from accounting.models import Currency, CurrencyL10n, Account, AccountL10n, \
Transaction, JournalEntry Transaction, JournalEntry
from accounting.utils.pagination import Pagination from accounting.utils.pagination import Pagination
from accounting.utils.query import parse_query_keywords 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_page_params import BasePageParams
from .utils.base_report import BaseReport from .utils.base_report import BaseReport
from .utils.csv_export import csv_download from .utils.csv_export import csv_download
@ -182,23 +182,7 @@ class Search(BaseReport):
:return: The response of the report for download. :return: The response of the report for download.
""" """
filename: str = "search-{q}.csv".format(q=request.args["q"]) filename: str = "search-{q}.csv".format(q=request.args["q"])
return csv_download(filename, self.__get_csv_rows()) return csv_download(filename, get_csv_rows(self.__entries))
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
def html(self) -> str: def html(self) -> str:
"""Composes and returns the report as HTML. """Composes and returns the report as HTML.