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

44 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-27 13:36 +0700

1import base64 

2import logging 

3import pytz 

4from django.core.management.base import CommandParser 

5from jutil.format import get_media_full_path 

6from jutil.command import SafeCommand 

7from jbank.helpers import parse_start_and_end_date 

8from jbank.models import WsEdiConnection 

9from jbank.wsedi import wsedi_execute 

10from xml.etree import ElementTree 

11 

12logger = logging.getLogger(__name__) 

13 

14 

15class Command(SafeCommand): 

16 help = "Executes WS-EDI command using direct bank connection." 

17 

18 def add_arguments(self, parser: CommandParser): 

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

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

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

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

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

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

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

26 

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

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

29 assert isinstance(ws, WsEdiConnection) 

30 if ws and not ws.enabled: 

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

32 return 

33 

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

35 cmd = options["cmd"] 

36 file_reference = options["file_reference"] or "" 

37 file_type = options["file_type"] or "" 

38 status = options["status"] or "" 

39 response = wsedi_execute( 

40 ws, 

41 command=cmd, 

42 file_reference=file_reference, 

43 file_type=file_type, 

44 status=status, 

45 start_date=start_date, 

46 end_date=end_date, 

47 verbose=True, 

48 ) 

49 print(response) 

50 root_el = ElementTree.fromstring(response) 

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

52 if content_el is not None: 

53 content_bytes = base64.b64decode(content_el.text) 

54 print(content_bytes.decode()) 

55 if file_reference: 

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

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

58 fp.write(content_bytes) 

59 print(full_path, "written")