wip
[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)
34 self.stdout.write("Le modèle %s n'existe pas dans DAE.\n" % classname)
35
36 id = int(args[1])
37 try:
38 obj = klass.objects.get(id=id)
39 except:
40 self.stdout.write("L'id %s n'existe pas pour le modèle %s dans DAE.\n" % \
41 (id, classname))
42 return
43
44 def copy_model(src, dst, exclude=[]):
45 keys = [f.name for f in src._meta.fields if f.name not in ['id', ] + exclude]
46 for k in keys:
47 setattr(dst, k, getattr(src, k))
48 return dst
49
50 print obj
51
52 def duplicate(obj, level=0):
53 print "%s%s" % ("|"*level, type(obj))
54 print "%s%s" % ("|"*level, obj)
55 for obj_composition in obj._meta._related_objects_cache:
56 app_label, field_name = obj_composition.name.split(':')
57 if app_label == obj._meta.app_label:
58 import pdb; pdb.set_trace()
59
60 sub_obj = getattr(obj, field_name)
61 duplicate(sub_obj, level+1)
62
63
64 duplicate(obj)
65
66
67
68