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

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

## 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 import logging

2 import os

3 import traceback

4 from django.core.management.base import CommandParser

5 from jutil.admin import admin_log

6 from jutil.format import strip_media_root

7 from jbank.files import list_dir_files

8 from jbank.helpers import process_pain002_file_content

9 from jbank.models import PayoutStatus

10 from jutil.command import SafeCommand

11

12

13 logger = logging.getLogger(__name__)

14

15

16 class Command(SafeCommand):

17 help = 'Parses pain.002 payment response .XP files and updates Payout status'

18

19 def add_arguments(self, parser: CommandParser):

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

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

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

23 parser.add_argument('--suffix', type=str, default='XP')

24 parser.add_argument('--set-default-paths', action='store_true')

25 parser.add_argument('--ignore-errors', action='store_true')

26 parser.add_argument('--ws', type=int)

27

28 def _set_default_paths(self, options: dict):

29 default_path = os.path.abspath(options['path'])

30 qs = PayoutStatus.objects.all().filter(file_path='')

31 if options['ws']:

32 qs = qs.filter(payout__connection_id=options['ws'])

33 objs = list(qs)

34 print('Setting default path of {} status updates to {}'.format(len(objs), strip_media_root(default_path)))

35 for obj in objs:

36 assert isinstance(obj, PayoutStatus)

37 full_path = os.path.join(default_path, obj.file_name)

38 if not os.path.isfile(full_path):

39 msg = 'Error while updating file path of PayoutStatus id={}: File {} not found'.format(obj.id,

40 full_path)

41 if not options['ignore_errors']:

42 raise Exception(msg)

43 logger.error(msg)

44 continue

45 file_path = strip_media_root(full_path)

46 logger.info('PayoutStatus.objects.filter(id=%s).update(file_path="%s")', obj.id, file_path)

47 if not options['test']:

48 PayoutStatus.objects.filter(id=obj.id).update(file_path=file_path)

49 admin_log([obj], 'File path set as "{}" from terminal (parse_xp)'.format(full_path))

50 print('Done')

51

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

53 if options['set_default_paths']:

54 self._set_default_paths(options)

55 return

56

57 files = list_dir_files(options['path'], '.' + options['suffix'])

58 for f in files:

59 if PayoutStatus.objects.is_file_processed(f):

60 if options['verbose']:

61 print('Skipping processed payment status file', f)

62 continue

63 if options['verbose']:

64 print('Importing payment status file', f)

65 try:

66 with open(f, 'rb') as fp:

67 process_pain002_file_content(fp.read(), f)

68 except Exception:

69 logger.error('Error while processing PayoutStatus id=%s: %s', f.id, traceback.format_exc()) # type: ignore

70 if not options['ignore_errors']:

71 raise