Hide keyboard shortcuts

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

1import json 

2import os 

3import zipfile 

4from datetime import datetime, date 

5from django.conf import settings 

6from django.core.management.base import CommandParser 

7from jutil.command import SafeCommand 

8from jbank.models import WsEdiConnection 

9 

10 

11class Command(SafeCommand): 

12 help = 'Export WS-EDI connection' 

13 

14 def add_arguments(self, parser: CommandParser): 

15 parser.add_argument('ws', type=int) 

16 parser.add_argument('--file', type=str) 

17 

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

19 ws = WsEdiConnection.objects.all().get(id=options['ws']) 

20 assert isinstance(ws, WsEdiConnection) 

21 

22 filename = 'ws{}.zip'.format(ws.id) 

23 if options['file']: 

24 filename = options['file'] 

25 

26 files = [] 

27 ws_data = {} 

28 for k, v in ws.__dict__.items(): 

29 if not k.startswith('_') and k != 'id': 

30 if isinstance(v, datetime): 

31 v = v.isoformat() 

32 elif isinstance(v, date): 

33 v = v.isoformat() 

34 ws_data[k] = v 

35 if k.endswith('_file'): 

36 files.append(os.path.join(settings.MEDIA_ROOT, v)) 

37 

38 zf = zipfile.ZipFile(filename, 'w', zipfile.ZIP_DEFLATED) 

39 zf.writestr('ws.json', json.dumps(ws_data)) 

40 for file in files: 

41 zf.write(file, os.path.basename(file)) 

42 zf.close() 

43 print('{} written'.format(filename))