CSV取込機能を実装し、Expenseモデルと関連するStore・ExpenseCategoryモデルを追加。CSVパーサーを作成し、出光CSVに対応。明細編集画面のAJAX保存機能を実装し、取込結果を表示する機能を追加。作業ログをdiary.mdに追記。
This commit is contained in:
36
expenses/services.py
Normal file
36
expenses/services.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Iterable
|
||||
|
||||
from .csv_parsers import get_parser
|
||||
from .models import Expense
|
||||
|
||||
|
||||
class CSVImportResult:
|
||||
def __init__(self, imported: int, duplicated: int) -> None:
|
||||
self.imported = imported
|
||||
self.duplicated = duplicated
|
||||
|
||||
|
||||
def import_csv_lines(lines: Iterable[str]) -> CSVImportResult:
|
||||
buffered_lines = list(lines)
|
||||
parser = get_parser(buffered_lines)
|
||||
rows = parser.parse(buffered_lines)
|
||||
imported = 0
|
||||
duplicated = 0
|
||||
for row in rows:
|
||||
expense, created = Expense.objects.get_or_create(
|
||||
source=row.source,
|
||||
source_hash=row.source_hash,
|
||||
defaults={
|
||||
'use_date': row.use_date,
|
||||
'description': row.description,
|
||||
'amount': row.amount,
|
||||
'note': row.note,
|
||||
},
|
||||
)
|
||||
if created:
|
||||
imported += 1
|
||||
else:
|
||||
duplicated += 1
|
||||
return CSVImportResult(imported=imported, duplicated=duplicated)
|
||||
Reference in New Issue
Block a user