From 467b09201e2216892ac649034a6ea29b4db808f7 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Wed, 23 Dec 2015 07:49:59 +0100 Subject: Custom command to manage imports with CLI --- ishtar_common/management/commands/ishtar_import.py | 38 ++++++++++++++++++++++ ishtar_common/models.py | 4 +-- 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 ishtar_common/management/commands/ishtar_import.py (limited to 'ishtar_common') diff --git a/ishtar_common/management/commands/ishtar_import.py b/ishtar_common/management/commands/ishtar_import.py new file mode 100644 index 000000000..49a7a0e64 --- /dev/null +++ b/ishtar_common/management/commands/ishtar_import.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from django.core.management.base import BaseCommand, CommandError + +from ishtar_common import models + + +class Command(BaseCommand): + help = "./manage.py ishtar_import \n\n"\ + "Launch the importation a configured import.\n"\ + " must be: \"analyse\", \"import\" or \"archive\"." + + def handle(self, *args, **options): + if not args or len(args) < 2: + raise CommandError(" and are mandatory") + command = args[0] + if args[0] not in ["analyse", "import", "archive"]: + raise CommandError( + " must be: \"analyse\", \"import\" or \"archive\"." + ) + try: + imp = models.Import.objects.get(pk=args[1]) + except (ValueError, models.Import.DoesNotExist): + raise CommandError("{} is not a valid import ID".format(args[0])) + if command == 'analyse': + imp.initialize() + self.stdout.write("* {} analysed\n".format(imp)) + self.stdout.flush() + elif command == 'import': + self.stdout.write("* import {}\n".format(imp)) + imp.importation(output='cli') + self.stdout.write("* {} imported\n".format(imp)) + self.stdout.flush() + elif command == 'archive': + imp.archive() + self.stdout.write("*{} archived\n".format(imp)) + self.stdout.flush() diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 20b8a6f46..c657f532d 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -1690,8 +1690,8 @@ class Import(models.Model): verbose_name_plural = _(u"Imports") def __unicode__(self): - return u"%s - %s" % (unicode(self.importer_type), - unicode(self.user)) + return u"%s - %s - %d" % (unicode(self.importer_type), + unicode(self.user), self.pk) def need_matching(self): return bool(TargetKey.objects.filter(associated_import=self, -- cgit v1.2.3