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

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

## 61 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

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_statement, get_or_create_bank_account

9 from jbank.files import list_dir_files

10 from jbank.models import Statement, StatementFile

11 from jbank.parsers import parse_tiliote_statements_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 account statement .TO (tiliote) 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 for filename in files:

33 plain_filename = os.path.basename(filename)

34

35 if options['resolve_original_filenames']:

36 found = StatementFile.objects.filter(statement__name=plain_filename).first()

37 if found and not found.original_filename:

38 assert isinstance(found, StatementFile)

39 found.original_filename = filename

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

41 logger.info('Original TO statement filename of %s resolved to %s', found, filename)

42

43 if options['delete_old']:

44 Statement.objects.filter(name=plain_filename).delete()

45

46 if options['test']:

47 statements = parse_tiliote_statements_from_file(filename)

48 pprint(statements)

49 continue

50

51 if not Statement.objects.filter(name=plain_filename).first():

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

53

54 statements = parse_tiliote_statements_from_file(filename)

55 if options['verbose']:

56 pprint(statements)

57

58 with transaction.atomic():

59 if not Statement.objects.filter(name=plain_filename).first():

60 file = StatementFile(original_filename=filename, tag=options['tag'])

61 file.save()

62

63 if is_media_full_path(filename):

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

65 file.save()

66 else:

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

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

69

70 for data in statements:

71 if options['auto_create_accounts']:

72 account_number = data.get('header', {}).get('account_number')

73 if account_number:

74 get_or_create_bank_account(account_number)

75

76 create_statement(data, name=plain_filename, file=file) # pytype: disable=not-callable

77 else:

78 print('Skipping statement file {}'.format(filename))