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

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
2import logging
3from django.core.management.base import CommandParser
4from jbank.models import ReferencePaymentBatchFile, ReferencePaymentRecord
5from jbank.parsers import parse_svm_batches_from_file
6from jutil.command import SafeCommand
9logger = logging.getLogger(__name__)
12class Command(SafeCommand):
13 help = 'Re-parses old bank settlement .SVM (saapuvat viitemaksut) files. Used for adding missing fields.'
15 def add_arguments(self, parser: CommandParser):
16 parser.add_argument('--file', type=str)
18 def do(self, *args, **options):
19 logger.info('Re-parsing SVM files to update fields')
20 qs = ReferencePaymentBatchFile.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, ReferencePaymentBatchFile)
25 logger.info('Processing {} BEGIN'.format(file))
26 batches = parse_svm_batches_from_file(file.full_path)
27 for batch in batches:
28 for e in batch['records']:
29 # check missing line_number
30 e2 = ReferencePaymentRecord.objects.filter(batch__file=file, line_number=0, record_type=e['record_type'],
31 account_number=e['account_number'],
32 paid_date=e['paid_date'], archive_identifier=e['archive_identifier'],
33 remittance_info=e['remittance_info'], payer_name=e['payer_name'],
34 currency_identifier=e['currency_identifier'], name_source=e['name_source'],
35 correction_identifier=e['correction_identifier'],
36 delivery_method=e['delivery_method'], receipt_code=e['receipt_code']).first()
37 if e2:
38 e2.line_number = e['line_number']
39 e2.save()
40 logger.info('Updated {} line number to {}'.format(e2, e2.line_number))
41 logger.info('Processing {} END'.format(file))