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 logging 

2import os 

3from typing import Optional 

4from django.core.management.base import CommandParser 

5from django.utils.timezone import now 

6from jutil.command import SafeCommand 

7from jsanctions.services import delete_old_sanction_list_files 

8from jsanctions.models import SanctionsListFile 

9from jsanctions.un import UN_LIST_TYPE, import_un_sanctions 

10 

11logger = logging.getLogger(__name__) 

12 

13 

14class Command(SafeCommand): 

15 help = "Imports U consolidated sanction lists" 

16 

17 def add_arguments(self, parser: CommandParser): 

18 parser.add_argument("--url", type=str) 

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

20 parser.add_argument("--delete-old", action="store_true") 

21 parser.add_argument("--source", type=int) 

22 parser.add_argument("--new", action="store_true") 

23 parser.add_argument("--verbose", action="store_true") 

24 parser.add_argument("--url-defaults", action='store_true') 

25 

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

27 verbose = options["verbose"] 

28 source: Optional[SanctionsListFile] = None 

29 list_type = UN_LIST_TYPE 

30 if options["url"]: 

31 url = options['url'] 

32 filename = options["file"] if options["file"] else "OFAC-{}-{}.xml".format(os.path.basename(url)[:-4], now().date().isoformat()) 

33 source = SanctionsListFile.objects.create_from_url(url, filename, list_type=list_type) 

34 elif options["file"]: 

35 source = SanctionsListFile.objects.create_from_filename(options["file"], list_type=list_type) 

36 elif options["source"]: 

37 source = SanctionsListFile.objects.get(id=options["source"]) 

38 elif options["new"]: 

39 source = SanctionsListFile.objects.filter(imported=None).order_by("id").first() 

40 sources = [source] if source else [] 

41 if options['url_defaults']: 

42 urls = [ 

43 'https://scsanctions.un.org/resources/xml/en/consolidated.xml', 

44 ] 

45 for url in urls: 

46 filename = "UN-{}-{}.xml".format(os.path.basename(url)[:-4], now().date().isoformat()) 

47 source = SanctionsListFile.objects.create_from_url(url, filename, list_type=list_type) 

48 sources.append(source) 

49 if not sources: 

50 print("Nothing to import") 

51 return 

52 

53 assert isinstance(source, SanctionsListFile) 

54 import_un_sanctions(source, verbose=verbose) 

55 

56 if options["delete_old"]: 

57 delete_old_sanction_list_files(list_type, sources)