diff options
author | Étienne Loks <etienne.loks@peacefrogs.net> | 2012-10-05 13:17:30 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@peacefrogs.net> | 2012-10-05 13:17:30 +0200 |
commit | 0bec9d3b303212d9a6c66117e759999cd7443146 (patch) | |
tree | db6d7d1c26a789e5943c8296961405faaa7821d2 | |
parent | e784740dd6356417bfb6627c68e3e68bbdfb8916 (diff) | |
download | Chimère-0bec9d3b303212d9a6c66117e759999cd7443146.tar.bz2 Chimère-0bec9d3b303212d9a6c66117e759999cd7443146.zip |
Import: add a command to launch import with the CLI
-rw-r--r-- | chimere/management/__init__.py | 0 | ||||
-rw-r--r-- | chimere/management/commands/__init__.py | 0 | ||||
-rw-r--r-- | chimere/management/commands/chimere_import.py | 53 | ||||
-rw-r--r-- | chimere/models.py | 10 | ||||
-rw-r--r-- | chimere/utils.py | 2 |
5 files changed, 64 insertions, 1 deletions
diff --git a/chimere/management/__init__.py b/chimere/management/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/chimere/management/__init__.py diff --git a/chimere/management/commands/__init__.py b/chimere/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/chimere/management/commands/__init__.py diff --git a/chimere/management/commands/chimere_import.py b/chimere/management/commands/chimere_import.py new file mode 100644 index 0000000..61ccd60 --- /dev/null +++ b/chimere/management/commands/chimere_import.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import csv +import datetime, time + +from django.core.management.base import BaseCommand, CommandError +from django.core.exceptions import ObjectDoesNotExist + +from chimere.models import Importer +from chimere import tasks + +class Command(BaseCommand): + args = '<import_id>' + help = "Launch import from an external source. Import configuration must "\ + "be beforehand inserted in the database with the web admin." + + def handle(self, *args, **options): + imports = dict([(imp.pk, imp) + for imp in Importer.objects.order_by('pk').all()]) + imprt = None + if args and args[0]: + try: + imprt = Importer.objects.get(pk=int(args[0])) + except (ValueError, ObjectDoesNotExist): + raise CommandError("Import with ID '%s' doesn't exist." % \ + args[0]) + if not args: + while not imprt: + self.stdout.write('* Choose the import: \n') + for k in imports: + self.stdout.write(' %s\n' % unicode(imports[k]).encode( + 'ascii', 'ignore')) + self.stdout.flush() + self.stdout.write('\nImport ID: ') + v = raw_input() + try: + imprt = Importer.objects.get(pk=int(v)) + except (ValueError, ObjectDoesNotExist): + self.stdout.write("Import with ID '%s' doesn't exist.\n\n" \ + % v) + pending_state = unicode(tasks.IMPORT_MESSAGES['import_pending'][0]) + imprt.state = pending_state + imprt.save() + tasks.importing(imprt.pk) + self.stdout.write("Import '%d' in progress...\n" % imprt.pk) + self.stdout.flush() + state = pending_state + while state == pending_state: + time.sleep(1) + state = Importer.objects.get(pk=int(imprt.pk)).state + self.stdout.write(state + '\n') + diff --git a/chimere/models.py b/chimere/models.py index 8b27dff..ee2d43e 100644 --- a/chimere/models.py +++ b/chimere/models.py @@ -212,6 +212,8 @@ IMPORTER_CHOICES = (('KML', 'KML'), ('SHP', 'Shapefile'), ) +IMPORTER_CHOICES_DICT = dict(IMPORTER_CHOICES) + class Importer(models.Model): ''' Data importer for a specific subcategory @@ -241,6 +243,14 @@ class Importer(models.Model): class Meta: verbose_name = _(u"Importer") + def __unicode__(self): + vals = [IMPORTER_CHOICES_DICT[self.importer_type], + self.source, self.source_file.name, + u", ".join([unicode(cat) for cat in self.categories.all()]), + self.default_name] + return u' %d: %s' % (self.pk, u" - ".join([unicode(v) + for v in vals if v])) + @property def manager(self): return IMPORTERS[self.importer_type](self) diff --git a/chimere/utils.py b/chimere/utils.py index 537562a..9398488 100644 --- a/chimere/utils.py +++ b/chimere/utils.py @@ -158,7 +158,7 @@ class ImportManager: source = open(source) except IOError, msg: return (None, msg) - except urllib2.URLError as error: + except (urllib2.URLError, AttributeError) as error: return (None, error.message) if self.importer_instance.zipped: try: |