Skip to content

Commit 02f0ad7

Browse files
committed
Fix CSV reader issues
1 parent 6f34df9 commit 02f0ad7

File tree

4 files changed

+61
-16
lines changed

4 files changed

+61
-16
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Generated by Django 3.2.23 on 2024-02-18 12:12
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("bankreader", "0003_transaction_account_statement_cleanup"),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name="transaction",
15+
name="recipient_description",
16+
field=models.CharField(default="", max_length=256, verbose_name="description for recipient"),
17+
),
18+
migrations.AlterField(
19+
model_name="transaction",
20+
name="remote_account_name",
21+
field=models.CharField(default="", max_length=128, verbose_name="remote account name"),
22+
),
23+
migrations.AlterField(
24+
model_name="transaction",
25+
name="remote_account_number",
26+
field=models.CharField(default="", max_length=64, verbose_name="remote account number"),
27+
),
28+
migrations.AlterField(
29+
model_name="transaction",
30+
name="sender_description",
31+
field=models.CharField(default="", max_length=256, verbose_name="description for sender"),
32+
),
33+
]

bankreader/models.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ class Transaction(models.Model):
8383
account = models.ForeignKey(Account, on_delete=models.CASCADE, verbose_name=_("account"))
8484
entry_date = models.DateField(_("entry date"))
8585
accounted_date = models.DateField(_("accounted date"))
86-
remote_account_number = models.CharField(_("remote account number"), max_length=64)
87-
remote_account_name = models.CharField(_("remote account name"), max_length=128)
86+
remote_account_number = models.CharField(_("remote account number"), default="", max_length=64)
87+
remote_account_name = models.CharField(_("remote account name"), default="", max_length=128)
8888
amount = models.DecimalField(_("amount"), decimal_places=2, max_digits=20)
8989
variable_symbol = models.BigIntegerField(_("variable symbol"), default=0)
9090
constant_symbol = models.BigIntegerField(_("constant symbol"), default=0)
9191
specific_symbol = models.BigIntegerField(_("specific symbol"), default=0)
92-
sender_description = models.CharField(_("description for sender"), max_length=256)
93-
recipient_description = models.CharField(_("description for recipient"), max_length=256)
92+
sender_description = models.CharField(_("description for sender"), default="", max_length=256)
93+
recipient_description = models.CharField(_("description for recipient"), default="", max_length=256)
9494

9595
class Meta:
9696
ordering = ("accounted_date",)
@@ -100,3 +100,10 @@ class Meta:
100100

101101
def __str__(self) -> str:
102102
return f"{self.accounted_date} {self.amount} {self.remote_account_name} {self.sender_description}"
103+
104+
def save(self, *args, **kwargs) -> None:
105+
if self.entry_date is None and self.accounted_date is not None:
106+
self.entry_date = self.accounted_date
107+
if self.accounted_date is None and self.entry_date is not None:
108+
self.accounted_date = self.entry_date
109+
return super().save(**kwargs)

bankreader/readers/csv.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,17 @@ def read_transactions(self, statemen_file: IO) -> Iterable[Transaction]:
3131
column_mapping: Dict[str, int] = {}
3232
csv_reader = csv.reader(rows, delimiter=self.delimiter, quotechar=self.quotechar)
3333
for row in csv_reader:
34+
# skip empty lines
35+
if row == []:
36+
continue
37+
# skip header until we find the column mapping
3438
if not column_mapping:
3539
try:
36-
column_mapping = {key: row.index(csv_key) for csv_key, key in self.column_mapping.items()}
40+
column_mapping = {key: row.index(csv_key) for key, csv_key in self.column_mapping.items()}
3741
except ValueError:
3842
pass
3943
continue
44+
# read individual transactions
4045
try:
4146
data = {key: self.get_value(key, row[column_mapping[key]]) for key in column_mapping}
4247
except IndexError:

bankreader_demo/demoapp/readers.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ class KbCsvReader(CsvReader):
1414
encoding = "cp1250"
1515
delimiter = ";"
1616
column_mapping = {
17-
"Datum splatnosti": "accounted_date",
18-
"Datum odepsani JB": "entry_date",
19-
"Protiucet/Kod banky": "remote_account_number",
20-
"Nazev protiuctu": "remote_account_name",
21-
"Castka": "amount",
22-
"VS": "variable_symbol",
23-
"KS": "constant_symbol",
24-
"SS": "specific_symbol",
25-
"Identifikace transakce": "transaction_id",
26-
"Popis prikazce": "sender_description",
27-
"Popis pro prijemce": "recipient_description",
17+
"accounted_date": "Datum splatnosti",
18+
"entry_date": "Datum odepsani JB",
19+
"remote_account_number": "Protiucet/Kod banky",
20+
"remote_account_name": "Nazev protiuctu",
21+
"amount": "Castka",
22+
"variable_symbol": "VS",
23+
"constant_symbol": "KS",
24+
"specific_symbol": "SS",
25+
"transaction_id": "Identifikace transakce",
26+
"sender_description": "Popis prikazce",
27+
"recipient_description": "Popis pro prijemce",
2828
}
2929
date_format = "%Y-%m-%d"
3030
decimal_separator = ","

0 commit comments

Comments
 (0)