Added the "accounting.report.reports.csv_export" module to handle the CSV export in one place.

This commit is contained in:
2023-03-07 22:45:29 +08:00
parent f838e7f893
commit 9f1e724875
7 changed files with 72 additions and 72 deletions

View File

@ -17,10 +17,8 @@
"""The ledger.
"""
import csv
from datetime import date
from decimal import Decimal
from io import StringIO
import sqlalchemy as sa
from flask import url_for, render_template, Response
@ -30,6 +28,7 @@ from accounting.locale import gettext
from accounting.models import Currency, Account, Transaction, JournalEntry
from accounting.report.period import Period
from accounting.utils.pagination import Pagination
from .utils.csv_export import BaseCSVRow, csv_download
from .utils.option_link import OptionLink
from .utils.page_params import PageParams
from .utils.period_choosers import LedgerPeriodChooser
@ -180,7 +179,7 @@ class EntryCollector:
entry.balance = balance
class CSVRow:
class CSVRow(BaseCSVRow):
"""A row in the CSV ledger."""
def __init__(self, txn_date: date | str | None,
@ -374,15 +373,7 @@ class Ledger:
filename: str = "ledger-{currency}-{account}-{period}.csv"\
.format(currency=self.__currency.code, account=self.__account.code,
period=self.__period.spec)
rows: list[CSVRow] = self.__get_csv_rows()
with StringIO() as fp:
writer = csv.writer(fp)
writer.writerows([x.values for x in rows])
fp.seek(0)
response: Response = Response(fp.read(), mimetype="text/csv")
response.headers["Content-Disposition"] \
= f"attachment; filename={filename}"
return response
return csv_download(filename, self.__get_csv_rows())
def __get_csv_rows(self) -> list[CSVRow]:
"""Composes and returns the CSV rows.