From 49eee77d87b09db18bf668566d1f939ebc6e36a5 Mon Sep 17 00:00:00 2001 From: Tobias Kunze Date: Sat, 13 Jan 2018 13:05:49 +0100 Subject: [PATCH] Fix transaction matching --- byro_shackspace/__init__.py | 3 +-- byro_shackspace/tests/test_bank_import.py | 1 + .../tests/test_csv_file_encoding.py | 2 +- byro_shackspace/utils.py | 25 +++++++++++-------- 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/byro_shackspace/__init__.py b/byro_shackspace/__init__.py index 75ef94c..5814daa 100644 --- a/byro_shackspace/__init__.py +++ b/byro_shackspace/__init__.py @@ -8,8 +8,7 @@ class ShackspacePluginConfig(AppConfig): name = 'shackspace' def ready(self): - print('fooooo') - from . import utils + from . import utils # noqa default_app_config = 'byro_shackspace.ShackspacePluginConfig' diff --git a/byro_shackspace/tests/test_bank_import.py b/byro_shackspace/tests/test_bank_import.py index 16e8cb0..c5b6d7d 100644 --- a/byro_shackspace/tests/test_bank_import.py +++ b/byro_shackspace/tests/test_bank_import.py @@ -1,4 +1,5 @@ import os.path + import pytest from byro_shackspace.utils import process_bank_csv from django.core.files.uploadedfile import InMemoryUploadedFile diff --git a/byro_shackspace/tests/test_csv_file_encoding.py b/byro_shackspace/tests/test_csv_file_encoding.py index 320393e..39b7546 100644 --- a/byro_shackspace/tests/test_csv_file_encoding.py +++ b/byro_shackspace/tests/test_csv_file_encoding.py @@ -1,7 +1,7 @@ import os.path + def test_file_encoding(): filename = os.path.join(os.path.dirname(__file__), 'fixtures/transactions.csv') with open(filename, encoding='iso-8859-1') as fp: assert len(fp.read()) == 1086 - diff --git a/byro_shackspace/utils.py b/byro_shackspace/utils.py index 206051c..9bba0a4 100644 --- a/byro_shackspace/utils.py +++ b/byro_shackspace/utils.py @@ -1,19 +1,22 @@ import csv import re -from datetime import date, datetime, timedelta +from datetime import datetime from decimal import Decimal -from django.db import transaction from django.dispatch import receiver from django.utils.timezone import now -from byro.bookkeeping.models import RealTransaction, TransactionChannel -from byro.bookkeeping.signals import derive_virtual_transactions, process_bank_csv +from byro.bookkeeping.models import ( + Account, AccountCategory, RealTransaction, + TransactionChannel, VirtualTransaction, +) +from byro.bookkeeping.signals import ( + derive_virtual_transactions, process_csv_upload, +) from byro.members.models import Member -@transaction.atomic -@receiver(process_bank_csv) +@receiver(process_csv_upload) def process_bank_csv(sender, signal, **kwargs): source = sender reader = csv.DictReader(open(source.source_file.name, encoding='iso-8859-1'), delimiter=';', quotechar='"') @@ -38,13 +41,13 @@ def process_bank_csv(sender, signal, **kwargs): source=source, data=line, ) + return True -@transaction.atomic @receiver(derive_virtual_transactions) def match_transaction(sender, signal, **kwargs): transaction = sender - uid, score = reference_parser(transaction.reference) + uid, score = reference_parser(reference=transaction.purpose) member = None try: member = Member.objects.get(number=uid) @@ -54,8 +57,8 @@ def match_transaction(sender, signal, **kwargs): account = Account.objects.get(account_category=AccountCategory.MEMBER_FEES) data = { 'amount': transaction.amount, - 'destination': account, - 'value_datetime': value_datetime, + 'destination_account': account, + 'value_datetime': transaction.value_datetime, 'member': member, } virtual_transaction = VirtualTransaction.objects.filter(**data).first() @@ -67,7 +70,7 @@ def match_transaction(sender, signal, **kwargs): return [virtual_transaction] -def reference_parser(self, reference): +def reference_parser(reference): reference = reference.lower() regexes = (