Coverage for jbank/management/commands/reparse_to.py: 0%

# Coverage for jbank/management/commands/reparse_to.py : 0%

## 27 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=logging-format-interpolation

2 import logging

3 from django.core.management.base import CommandParser

4 from jbank.models import StatementFile, StatementRecord

5 from jbank.parsers import parse_tiliote_statements_from_file

6 from jutil.command import SafeCommand

7

8

9 logger = logging.getLogger(__name__)

10

11

12 class Command(SafeCommand):

13 help = 'Re-parses old bank statement .TO (tiliote) files. Used for adding missing fields.'

14

15 def add_arguments(self, parser: CommandParser):

16 parser.add_argument('--file', type=str)

17

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

19 logger.info('Re-parsing TO files to update fields')

20 qs = StatementFile.objects.all()

21 if options['file']:

22 qs = qs.filter(file=options['file'])

23 for file in qs.order_by('id'):

24 assert isinstance(file, StatementFile)

25 logger.info('Processing {} BEGIN'.format(file))

26 statements = parse_tiliote_statements_from_file(file.full_path)

27 for data in statements:

28 for e in data['records']:

29 # check missing line_number

30 e2 = StatementRecord.objects.filter(statement__file=file, line_number=0, record_number=e['record_number'],

31 archive_identifier=e['archive_identifier'], record_date=e['record_date'],

32 value_date=e['value_date'], paid_date=e['paid_date'],

33 entry_type=e['entry_type'], record_code=e['record_code'],

34 record_description=e['record_description'], receipt_code=e['receipt_code'],

35 delivery_method=e['delivery_method'], name=e['name'],

36 name_source=e['name_source'], recipient_account_number=e['recipient_account_number'],

37 recipient_account_number_changed=e['recipient_account_number_changed'],

38 remittance_info=e['remittance_info']).first()

39 if e2:

40 e2.line_number = e['line_number']

41 e2.save()

42 logger.info('Updated {} line number to {}'.format(e2, e2.line_number))

43 logger.info('Processing {} END'.format(file))