Coverage for jsanctions/management/commands/import_eu_sanctions.py: 0%

# Coverage for jsanctions/management/commands/import_eu_sanctions.py : 0%

## 39 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 from typing import Optional

3 from django.core.management.base import CommandParser

4 from django.utils.timezone import now

5 from jsanctions.eu import import_eu_sanctions, EU_LIST_TYPE

6 from jutil.command import SafeCommand

7 from jsanctions.services import delete_old_sanction_list_files

8 from jsanctions.models import SanctionsListFile

9

10 logger = logging.getLogger(__name__)

11

12

13 class Command(SafeCommand):

14 help = "Imports EU consolidated sanction lists"

15

16 def add_arguments(self, parser: CommandParser):

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

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

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

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

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

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

23

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

25 list_type = EU_LIST_TYPE

26 verbose = options["verbose"]

27 source: Optional[SanctionsListFile] = None

28 if options["url"]:

29 url = options['url']

30 filename = options["file"] if options["file"] else "EU-consolidated-{}.xml".format(now().date().isoformat())

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

32 elif options["file"]:

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

34 elif options["source"]:

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

36 elif options["new"]:

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

38 if not source:

39 print("Nothing to import")

40 return

41

42 assert isinstance(source, SanctionsListFile)

43 import_eu_sanctions(source, verbose=verbose)

44

45 if options["delete_old"]:

46 delete_old_sanction_list_files(list_type, [source])