summaryrefslogtreecommitdiff
path: root/ishtar_common/management/commands/ishtar_import.py
blob: 030c5fab897436cc45c1e5a40428d330f6aee262 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django.conf import settings
from django.core.management.base import BaseCommand, CommandError

from ishtar_common import models, models_imports
from ishtar_common.utils import BColors


class Command(BaseCommand):
    help = "./manage.py ishtar_import <command> <import_id>\n\n"\
           "Launch the importation a configured import.\n"\
           "<command> must be: \"list\", \"analyse\", \"import\" or " \
           "\"archive\"."

    def add_arguments(self, parser):
        parser.add_argument('command', choices=["list", "analyse", "import",
                                                "archive"])
        parser.add_argument('import_id', nargs='?', default=None)
        parser.add_argument(
            "--quiet", dest="quiet", action="store_true", help="Quiet output"
        )

    def handle(self, *args, **options):
        command = options['command']
        import_id = options['import_id']
        quiet = options.get("quiet", False)
        if command != "list" and not import_id:
            raise CommandError("With {} <import_id> is mandatory".format(
                command))
        if command == 'list':
            state = dict(models_imports.IMPORT_STATE)
            self.stdout.write("*" * 80 + "\n")
            self.stdout.write(
                "|  pk  |               type               |     state    "
                "|        name\n")
            self.stdout.write("*" * 80 + "\n")
            for imp in models.Import.objects.exclude(state="AC").all():
                self.stdout.write("|{: ^6}| {: ^32} | {: ^12} | {}\n".format(
                    imp.pk, str(imp.importer_type)[:32],
                    state[imp.state][:12],
                    imp.name[:128]))
            self.stdout.write("*" * 80 + "\n")
            self.stdout.flush()
            return
        try:
            imp = models.Import.objects.get(pk=import_id)
        except (ValueError, models.Import.DoesNotExist):
            raise CommandError("{} is not a valid import ID".format(args[0]))
        settings.USE_BACKGROUND_TASK = False
        if command == 'analyse':
            imp.initialize()
            self.stdout.write("* {} analysed\n".format(imp))
            self.stdout.flush()
        elif command == 'import':
            if not quiet:
                self.stdout.write(BColors.OKGREEN + f"* import {imp}{BColors.ENDC}\n")
            imp.importation(verbose=not quiet)
            if not quiet:
                self.stdout.write(BColors.OKGREEN + f"* {imp} imported{BColors.ENDC}\n")
            self.stdout.flush()
        elif command == 'archive':
            imp.archive()
            self.stdout.write("*{} archived\n".format(imp))
            self.stdout.flush()