summaryrefslogtreecommitdiff
path: root/chimere/management/commands
diff options
context:
space:
mode:
Diffstat (limited to 'chimere/management/commands')
-rw-r--r--chimere/management/commands/__init__.py0
-rw-r--r--chimere/management/commands/chimere_export.py86
-rw-r--r--chimere/management/commands/chimere_import.py53
3 files changed, 139 insertions, 0 deletions
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_export.py b/chimere/management/commands/chimere_export.py
new file mode 100644
index 0000000..4e91956
--- /dev/null
+++ b/chimere/management/commands/chimere_export.py
@@ -0,0 +1,86 @@
+#!/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 import tasks
+from chimere.admin import export_to_csv, export_to_kml, export_to_shapefile
+from chimere.models import Category, SubCategory, Marker, Route
+
+EXPORTER = {'CSV':export_to_csv,
+ 'KML':export_to_kml,
+ 'SHP':export_to_shapefile}
+
+class Command(BaseCommand):
+ args = '<subcategory_id> <CSV|KML|SHP> <marker|route> <filename>'
+ help = "Export items from a subcategory in CSV, KML or ShapeFile"
+
+ def handle(self, *args, **options):
+ subcat = None
+ if args and args[0]:
+ try:
+ subcat = SubCategory.objects.get(pk=int(args[0]))
+ except (ValueError, ObjectDoesNotExist) as e:
+ raise CommandError("Sub-category with ID '%s' doesn't exist." %\
+ args[0])
+ while not subcat:
+ self.stdout.write('Choose a sub-category:\n')
+ for cat in Category.objects.order_by('order'):
+ self.stdout.write('* %s\n' % cat.name)
+ for subcat in cat.subcategories.order_by('order').all():
+ self.stdout.write(' %d - %s\n' % (subcat.pk, subcat.name))
+ self.stdout.write('\nSub-category ID: ')
+ self.stdout.flush()
+ v = raw_input()
+ try:
+ subcat = SubCategory.objects.get(pk=v)
+ except (ValueError, ObjectDoesNotExist) as e:
+ subcat = None
+ self.stdout.write("Sub-category with ID '%s' doesn't exist.\n\n"
+ % v)
+ frmat = None
+ if args and args[1]:
+ try:
+ assert(args[1] in ('CSV', 'KML', 'SHP'))
+ except AssertionError:
+ raise CommandError("'%s' format is not managed." % args[1])
+ frmat = args[1]
+ while frmat not in ('CSV', 'KML', 'SHP'):
+ self.stdout.write('Choose a format (CSV, KML or SHP): ')
+ frmat = raw_input().replace('\n', '')
+ exporter = EXPORTER[frmat]
+ cls = None
+ if args and args[2]:
+ try:
+ assert(args[2] in ('marker', 'route'))
+ except AssertionError:
+ raise CommandError("Exported item must be 'marker' or 'route'."
+ % args[1])
+ if args[2] == 'marker':
+ cls = Marker
+ elif args[2] == 'route':
+ cls = Route
+ while not cls:
+ self.stdout.write('Choose an item type:\n 1 - marker\n 2 - route\n')
+ self.stdout.write('Number: ')
+ v = raw_input()
+ if v == '1':
+ cls = Marker
+ elif v == '2':
+ cls = Route
+ filename = ''
+ if args and args[3]:
+ filename = args[3]
+ else:
+ self.stdout.write('Filename: ')
+ filename = raw_input()
+ response = exporter(None, None, cls.objects.filter(categories=subcat))
+ try:
+ with open(filename, 'w+') as fl:
+ fl.write(response.content)
+ except IOError as e:
+ raise CommandError(e)
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')
+