Skip to content

Commit 7170804

Browse files
committed
Use new Cora Credit Cart importer
1 parent a35173a commit 7170804

File tree

3 files changed

+60
-14
lines changed

3 files changed

+60
-14
lines changed

thebook/bookkeeping/importers/__init__.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from thebook.bookkeeping.importers.csv import CSVCoraCreditCardImporter, CSVImporter
66
from thebook.bookkeeping.importers.ofx import OFXImporter
7+
from thebook.bookkeeping.models import Transaction
8+
from thebook.integrations.cora.credit_card_invoice import CoraCreditCardInvoiceImporter
79

810
logger = logging.getLogger(__name__)
911

@@ -23,7 +25,7 @@ def import_transactions(
2325
):
2426
importers = {
2527
"csv": CSVImporter,
26-
"csv_cora_credit_card": CSVCoraCreditCardImporter,
28+
"csv_cora_credit_card": CoraCreditCardInvoiceImporter,
2729
"ofx": OFXImporter,
2830
}
2931
importer = importers.get(file_type) or None
@@ -33,7 +35,19 @@ def import_transactions(
3335
)
3436

3537
try:
36-
importer(transactions_file, bank_account, user).run(start_date, end_date)
38+
if file_type == "csv_cora_credit_card":
39+
csv_importer = importer(transactions_file)
40+
transactions = csv_importer.get_transactions(
41+
start_date, end_date, exclude_existing=True
42+
)
43+
Transaction.objects.bulk_create(
44+
transactions,
45+
update_conflicts=True,
46+
update_fields=["description", "amount"],
47+
unique_fields=["reference"],
48+
)
49+
else:
50+
importer(transactions_file, bank_account, user).run(start_date, end_date)
3751
except Exception as err:
3852
logger.exception(err)
3953
raise ImportTransactionsError(_("Something wrong happened during file import."))

thebook/integrations/cora/credit_card_invoice.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def get_transactions(
5959
) -> list[Transaction]:
6060
transactions = []
6161

62-
csv_content = self.invoice_file.read()
62+
csv_content = self.invoice_file.read().decode()
6363
reader = csv.DictReader(io.StringIO(csv_content))
6464

6565
if set(reader.fieldnames) != self._expected_field_names:
@@ -82,7 +82,10 @@ def get_transactions(
8282
if (
8383
exclude_existing
8484
and Transaction.objects.filter(
85-
date=transaction_date, description=description, amount=amount
85+
date=transaction_date,
86+
description=description,
87+
amount=amount,
88+
bank_account=self.bank_account,
8689
).exists()
8790
):
8891
continue

thebook/integrations/tests/cora/test_credit_card_invoice.py

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_valid_but_empty_invoice_file(
4242
invoice_file_path = (
4343
request.path.parent / "data" / "empty-cora-credit-card-invoice.csv"
4444
)
45-
with open(invoice_file_path, "r") as invoice_file:
45+
with open(invoice_file_path, "rb") as invoice_file:
4646
importer = CoraCreditCardInvoiceImporter(invoice_file)
4747
transactions = importer.get_transactions()
4848
assert transactions == []
@@ -51,7 +51,7 @@ def test_invalid_invoice_file(self, db, request):
5151
invoice_file_path = (
5252
request.path.parent / "data" / "invalid-cora-credit-card-invoice.csv"
5353
)
54-
with open(invoice_file_path, "r") as invoice_file:
54+
with open(invoice_file_path, "rb") as invoice_file:
5555
importer = CoraCreditCardInvoiceImporter(invoice_file)
5656

5757
with pytest.raises(InvalidCoraCreditCardInvoice):
@@ -65,7 +65,7 @@ def test_one_transaction_excluding_existing_false(
6565
/ "data"
6666
/ "cora-credit-card-invoice-one-transaction.csv"
6767
)
68-
with open(invoice_file_path, "r") as invoice_file:
68+
with open(invoice_file_path, "rb") as invoice_file:
6969
importer = CoraCreditCardInvoiceImporter(invoice_file)
7070
transactions = importer.get_transactions()
7171

@@ -81,58 +81,87 @@ def test_one_transaction_excluding_existing_false(
8181
assert transactions[0].source == "cora-credit-card-invoice-importer"
8282
assert transactions[0].created_by == user
8383

84-
def test_one_transaction_excluding_existing_true(self, db, request):
84+
def test_one_transaction_excluding_existing_true(
85+
self, db, request, cora_credit_card_bank_account
86+
):
8587
baker.make(
8688
Transaction,
8789
date=datetime.date(2026, 2, 21),
8890
description="BACKBLAZE INC",
8991
amount=decimal.Decimal("-8.91"),
92+
bank_account=cora_credit_card_bank_account,
9093
)
9194

9295
invoice_file_path = (
9396
request.path.parent
9497
/ "data"
9598
/ "cora-credit-card-invoice-one-transaction.csv"
9699
)
97-
with open(invoice_file_path, "r") as invoice_file:
100+
with open(invoice_file_path, "rb") as invoice_file:
98101
importer = CoraCreditCardInvoiceImporter(invoice_file)
99102
transactions = importer.get_transactions(exclude_existing=True)
100103
assert len(transactions) == 0
101104

102-
def test_two_transactions_excluding_existing_true(self, db, request):
105+
def test_one_transaction_from_different_bank_account(
106+
self, db, request, cora_credit_card_bank_account
107+
):
108+
baker.make(
109+
Transaction,
110+
date=datetime.date(2026, 2, 21),
111+
description="BACKBLAZE INC",
112+
amount=decimal.Decimal("-8.91"),
113+
)
114+
115+
invoice_file_path = (
116+
request.path.parent
117+
/ "data"
118+
/ "cora-credit-card-invoice-one-transaction.csv"
119+
)
120+
with open(invoice_file_path, "rb") as invoice_file:
121+
importer = CoraCreditCardInvoiceImporter(invoice_file)
122+
transactions = importer.get_transactions(exclude_existing=True)
123+
assert len(transactions) == 1
124+
125+
def test_two_transactions_excluding_existing_true(
126+
self, db, request, cora_credit_card_bank_account
127+
):
103128
baker.make(
104129
Transaction,
105130
date=datetime.date(2026, 2, 21),
106131
description="BACKBLAZE INC",
107132
amount=decimal.Decimal("-8.91"),
133+
bank_account=cora_credit_card_bank_account,
108134
)
109135

110136
invoice_file_path = (
111137
request.path.parent
112138
/ "data"
113139
/ "cora-credit-card-invoice-two-transactions.csv"
114140
)
115-
with open(invoice_file_path, "r") as invoice_file:
141+
with open(invoice_file_path, "rb") as invoice_file:
116142
importer = CoraCreditCardInvoiceImporter(invoice_file)
117143
transactions = importer.get_transactions(exclude_existing=True)
118144

119145
assert len(transactions) == 1
120146
assert transactions[0].description == "VULTR BY CONSTANT"
121147

122-
def test_two_transactions_excluding_existing_false(self, db, request):
148+
def test_two_transactions_excluding_existing_false(
149+
self, db, request, cora_credit_card_bank_account
150+
):
123151
baker.make(
124152
Transaction,
125153
date=datetime.date(2026, 2, 21),
126154
description="BACKBLAZE INC",
127155
amount=decimal.Decimal("-8.91"),
156+
bank_account=cora_credit_card_bank_account,
128157
)
129158

130159
invoice_file_path = (
131160
request.path.parent
132161
/ "data"
133162
/ "cora-credit-card-invoice-two-transactions.csv"
134163
)
135-
with open(invoice_file_path, "r") as invoice_file:
164+
with open(invoice_file_path, "rb") as invoice_file:
136165
importer = CoraCreditCardInvoiceImporter(invoice_file)
137166
transactions = importer.get_transactions(exclude_existing=False)
138167

@@ -173,7 +202,7 @@ def test_transactions_filtered_by_date_range(
173202
/ "data"
174203
/ "cora-credit-card-invoice-multiple-transactions.csv"
175204
)
176-
with open(invoice_file_path, "r") as invoice_file:
205+
with open(invoice_file_path, "rb") as invoice_file:
177206
importer = CoraCreditCardInvoiceImporter(invoice_file)
178207
transactions = importer.get_transactions(
179208
start_date=start_date, end_date=end_date

0 commit comments

Comments
 (0)