103f55f2fd866f9ae6e426c836a31bc7f57119a6
[auf_rh_dae.git] / project / dae / management / commands / dae.py
1 # -*- encoding: utf-8 -*-
2
3 from django.core.management.base import BaseCommand
4
5 from project.dae import models as dae
6 from project.dae import exporter
7
8 class Command(BaseCommand):
9
10
11 def handle(self, *args, **options):
12
13 if len(args) < 2:
14 print len(args)
15 self.stdout.write("dae <modele> <id> <run>\n")
16 return
17
18 classname = args[0]
19 klass = getattr(dae, classname, None)
20 if klass is None:
21 self.stdout.write("Le modèle %s n'existe pas dans DAE.\n" % classname)
22
23 id = int(args[1])
24 try:
25 obj = klass.objects.get(id=id)
26 except:
27 self.stdout.write("L'id %s n'existe pas pour le modèle %s dans DAE.\n" % \
28 (id, classname))
29 return
30
31
32 dry_run = True
33 if len(args) >= 3 and args[2] == "run":
34 dry_run = False
35
36 verbosity = 0
37 if len(args) >= 4:
38 verbosity = int(args[3])
39
40 class_copier = getattr(exporter, "%sCopier" % classname.title())
41 copier = class_copier(verbosity=verbosity, dry_run=dry_run)
42
43 copy = copier.duplicate(obj)
44 self.stdout.write(u"\n[%s] DAE:%s => RH:%s\n" % (obj.__class__.__name__, obj.id, copy.id))
45