diff options
| author | Étienne Loks <etienne.loks@peacefrogs.net> | 2012-10-14 16:55:22 +0200 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@peacefrogs.net> | 2012-10-14 16:55:22 +0200 | 
| commit | ea65e5512c236b81e7f4b8757521facadae4b3b8 (patch) | |
| tree | 739cdf40fb6a89de90c4189936d695288a82849f /chimere/management/commands/chimere_import.py | |
| parent | 4b0f0777c434f5fa1366ca408c34d257d4833fad (diff) | |
| parent | a55f77a246f99764ff6289686f80825526654e2b (diff) | |
| download | Chimère-ea65e5512c236b81e7f4b8757521facadae4b3b8.tar.bz2 Chimère-ea65e5512c236b81e7f4b8757521facadae4b3b8.zip  | |
Merge branch 'master' into saclay
Conflicts:
	chimere/admin.py
	chimere/fixtures/initial_data.json
	chimere/forms.py
	chimere/locale/fr/LC_MESSAGES/django.po
	chimere/models.py
	chimere/static/chimere/css/styles.css
	chimere/templates/chimere/detail.html
	chimere/templatetags/chimere_tags.py
	chimere/views.py
	chimere/widgets.py
Diffstat (limited to 'chimere/management/commands/chimere_import.py')
| -rw-r--r-- | chimere/management/commands/chimere_import.py | 53 | 
1 files changed, 53 insertions, 0 deletions
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') +  | 
