Coverage for jbank/management/commands/parse_svm.py: 77%

# Coverage for jbank/management/commands/parse_svm.py : 77%

## 62 statements

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1#pylint: disable=too-many-branches,logging-format-interpolation

2 import logging

3 import os

4 from pprint import pprint

5 from django.core.files import File

6 from django.core.management.base import CommandParser

7 from django.db import transaction

8 from jbank.helpers import create_reference_payment_batch, get_or_create_bank_account

9 from jbank.files import list_dir_files

10 from jbank.models import ReferencePaymentBatch, ReferencePaymentBatchFile

11 from jbank.parsers import parse_svm_batches_from_file

12 from jutil.command import SafeCommand

13 from jutil.format import strip_media_root, is_media_full_path

14

15 logger = logging.getLogger(__name__)

16

17

18 class Command(SafeCommand):

19 help = 'Parses bank settlement .SVM (saapuvat viitemaksut) files'

20

21 def add_arguments(self, parser: CommandParser):

22 parser.add_argument('path', type=str)

23 parser.add_argument('--verbose', action='store_true')

24 parser.add_argument('--test', action='store_true')

25 parser.add_argument('--delete-old', action='store_true')

26 parser.add_argument('--auto-create-accounts', action='store_true')

27 parser.add_argument('--resolve-original-filenames', action='store_true')

28 parser.add_argument('--tag', type=str, default='')

29

30 def do(self, *args, **options):

31 files = list_dir_files(options['path'])

32 # pprint(files)

33 for filename in files:

34 plain_filename = os.path.basename(filename)

35

36 if options['resolve_original_filenames']:

37 found = ReferencePaymentBatchFile.objects.filter(referencepaymentbatch__name=plain_filename).first()

38 if found and not found.original_filename:

39 assert isinstance(found, ReferencePaymentBatchFile)

40 found.original_filename = filename

41 found.save(update_fields=['original_filename'])

42 logger.info('Original SVM reference payment batch filename of %s resolved to %s', found, filename)

43

44 if options['delete_old']:

45 ReferencePaymentBatch.objects.filter(name=plain_filename).delete()

46

47 if options['test']:

48 batches = parse_svm_batches_from_file(filename)

49 pprint(batches)

50 continue

51

52 if not ReferencePaymentBatch.objects.filter(name=plain_filename).first():

53 print('Importing statement file {}'.format(filename))

54

55 batches = parse_svm_batches_from_file(filename)

56 if options['verbose']:

57 pprint(batches)

58

59 with transaction.atomic():

60 if not ReferencePaymentBatch.objects.filter(name=plain_filename).first():

61 file = ReferencePaymentBatchFile(original_filename=filename, tag=options['tag'])

62 file.save()

63

64 if is_media_full_path(filename):

65 file.file.name = strip_media_root(filename) # type: ignore

66 file.save()

67 else:

68 with open(filename, 'rb') as fp:

69 file.file.save(plain_filename, File(fp))

70

71 for data in batches:

72 if options['auto_create_accounts']:

73 for rec_data in data['records']:

74 account_number = rec_data.get('account_number')

75 if account_number:

76 get_or_create_bank_account(account_number)

77

78 create_reference_payment_batch(data, name=plain_filename, file=file) # pytype: disable=not-callable

79 else:

80 print('Skipping reference payment file {}'.format(filename))