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

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

## 44 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 base64

2 import logging

3 import pytz

4 from django.core.management.base import CommandParser

5 from jutil.format import get_media_full_path

6 from jutil.command import SafeCommand

7 from jbank.helpers import parse_start_and_end_date

8 from jbank.models import WsEdiConnection

9 from jbank.wsedi import wsedi_execute

10 from xml.etree import ElementTree

11

12 logger = logging.getLogger(__name__)

13

14

15 class Command(SafeCommand):

16 help = """

17 Executes WS-EDI command using direct bank connection.

18 """

19

20 def add_arguments(self, parser: CommandParser):

21 parser.add_argument("--ws", type=int, default=1)

22 parser.add_argument("--cmd", type=str, default="DownloadFileList")

23 parser.add_argument("--file-reference", type=str)

24 parser.add_argument("--file-type", type=str)

25 parser.add_argument("--start-date", type=str)

26 parser.add_argument("--end-date", type=str)

27 parser.add_argument("--status", type=str)

28

29 def do(self, *args, **options): # pylint: disable=too-many-locals

30 ws = WsEdiConnection.objects.get(id=options["ws"])

31 assert isinstance(ws, WsEdiConnection)

32 if ws and not ws.enabled:

33 logger.info("WS connection %s not enabled, exiting", ws)

34 return

35

36 start_date, end_date = parse_start_and_end_date(pytz.timezone("Europe/Helsinki"), **options)

37 cmd = options["cmd"]

38 file_reference = options["file_reference"] or ""

39 file_type = options["file_type"] or ""

40 status = options["status"] or ""

41 response = wsedi_execute(

42 ws,

43 command=cmd,

44 file_reference=file_reference,

45 file_type=file_type,

46 status=status,

47 start_date=start_date,

48 end_date=end_date,

49 verbose=True,

50 )

51 print(response)

52 root_el = ElementTree.fromstring(response)

53 content_el = root_el.find("{http://bxd.fi/xmldata/}Content")

54 if content_el is not None:

55 content_bytes = base64.b64decode(content_el.text)

56 print(content_bytes.decode())

57 if file_reference:

58 full_path = get_media_full_path("downloads/" + file_reference + "." + file_type)

59 with open(full_path, "wb") as fp:

60 fp.write(content_bytes)

61 print(full_path, "written")