xxx
[auf_rh_dae.git] / project / dae / management / commands / dae.py
CommitLineData
0d5def11
OL
1# -*- encoding: utf-8 -*-
2
3from django.core.management.base import BaseCommand
4from project.dae import models as dae
5
6
7
8class Command(BaseCommand):
9
10 def explore(obj):
11 print obj
12 for f in obj._meta.fields:
13 def t(s):
14 s = "%s" % s
15 try:
16 s = s.ljust(40)
17 s = s.replace('\n', '')
18 s = s[0:28]
19 except Exception, e:
20 print e
21 print s
22 return
23 return s
24 print "%s%s%s" % (t(f.name), t(type(f).__name__), t(getattr(obj, f.name, "")))
25
26 def handle(self, *args, **options):
27
28 if len(args) != 2:
29 self.stdout.write("dae <modele> <id>\n")
30 return
31
32 classname = args[0]
33 klass = getattr(dae, classname, None)
3b14230d
OL
34 if klass is None:
35 self.stdout.write("Le modèle %s n'existe pas dans DAE.\n" % classname)
0d5def11
OL
36
37 id = int(args[1])
38 try:
39 obj = klass.objects.get(id=id)
40 except:
41 self.stdout.write("L'id %s n'existe pas pour le modèle %s dans DAE.\n" % \
42 (id, classname))
43 return
44
45 def copy_model(src, dst, exclude=[]):
46 keys = [f.name for f in src._meta.fields if f.name not in ['id', ] + exclude]
47 for k in keys:
48 setattr(dst, k, getattr(src, k))
49 return dst
50
51 print obj
52
53 def duplicate(obj, level=0):
3b14230d 54 print "%s [%s] %s" % ("|"*level*4, type(obj), obj)
0d5def11 55 for obj_composition in obj._meta._related_objects_cache:
3b14230d
OL
56 app_label, dummy = obj_composition.name.split(':')
57 field_name = obj_composition.field.rel.related_name
0d5def11 58 if app_label == obj._meta.app_label:
3b14230d
OL
59 try:
60 children = getattr(obj, field_name)
61 except:
62 print obj_composition
63 print field_name
64 continue
65 for child in children.all():
66 duplicate(child, level+1)
0d5def11
OL
67
68
69 duplicate(obj)
3b14230d
OL
70 import pdb; pdb.set_trace()
71
0d5def11
OL
72
73
74