Added the "accounting.report.reports.csv_export" module to handle the CSV export in one place.
This commit is contained in:
@ -17,10 +17,8 @@
|
||||
"""The journal.
|
||||
|
||||
"""
|
||||
import csv
|
||||
from datetime import date
|
||||
from decimal import Decimal
|
||||
from io import StringIO
|
||||
|
||||
import sqlalchemy as sa
|
||||
from flask import 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.page_params import PageParams
|
||||
from .utils.period_choosers import JournalPeriodChooser
|
||||
from .utils.report_chooser import ReportChooser
|
||||
@ -70,7 +69,7 @@ class Entry:
|
||||
self.amount = entry.amount
|
||||
|
||||
|
||||
class CSVRow:
|
||||
class CSVRow(BaseCSVRow):
|
||||
"""A row in the CSV journal."""
|
||||
|
||||
def __init__(self, txn_date: str | date,
|
||||
@ -209,15 +208,7 @@ class Journal:
|
||||
:return: The response of the report for download.
|
||||
"""
|
||||
filename: str = f"journal-{self.__period.spec}.csv"
|
||||
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.
|
||||
|
Reference in New Issue
Block a user