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(

40 obj.id, full_path

41 )

42 if not options["ignore_errors"]:

43 raise Exception(msg)

44 logger.error(msg)

45 continue

46 file_path = strip_media_root(full_path)

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

48 if not options["test"]:

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

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

51 print("Done")

52

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

54 if options["set_default_paths"]:

55 self._set_default_paths(options)

56 return

57

58 files = list_dir_files(options["path"], "." + options["suffix"])

59 for f in files:

60 if PayoutStatus.objects.is_file_processed(f):

61 if options["verbose"]:

62 print("Skipping processed payment status file", f)

63 continue

64 if options["verbose"]:

65 print("Importing payment status file", f)

66 try:

67 with open(f, "rb") as fp:

68 process_pain002_file_content(fp.read(), f)

69 except Exception:

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

71 if not options["ignore_errors"]:

72 raise