poste importer
[auf_rh_dae.git] / project / dae / management / commands / dae.py
1 # -*- encoding: utf-8 -*-
2
3 import sys
4 import codecs
5 import os
6 import shutil
7
8 from django.core.management.base import BaseCommand
9 from django.conf import settings
10
11 from project.dae import models as dae
12 from project.rh import models as rh
13
14
15 class SuperCopier(object):
16 dry_run = True
17 verbosity = 0
18 stdout = codecs.getwriter('utf8')(sys.stdout)
19
20 def __init__(self, dry_run=True, verbosity=0):
21 self.verbosity = verbosity
22 self.dry_run = dry_run
23
24 def out(self, txt, level=0):
25 if self.verbosity >= level:
26 self.stdout.write(txt)
27
28 def clean_id(self, source, copy, parent, field, value):
29 pass
30
31 def parent(self, parent, child):
32 klass = parent.__class__.__name__.lower()
33 k = "%s_id" % klass
34 setattr(child, k, parent.id)
35
36 def duplicate(self, obj, parent=None, level=0):
37 klass = getattr(rh, obj.__class__.__name__)
38 copy = klass()
39
40 indent = " "*level*4
41 self.out(u"\n%s [%s] %s" % (indent, str(obj.__class__),
42 obj), 1)
43
44 for f in obj._meta.fields:
45 value = getattr(obj, f.name)
46 self.out(u"\n%s * %s: %s" % (indent, f.name, value), 2)
47
48 cleanup_fct = "clean_%s" % f.name
49 if hasattr(self, cleanup_fct):
50 cleaner = getattr(self, cleanup_fct)
51 cleaner(obj, copy, parent, f, value)
52 else:
53 setattr(copy, f.name, value)
54
55 if not self.dry_run:
56 copy.save()
57
58 for obj_composition in obj._meta._related_objects_cache:
59 app_label, dummy = obj_composition.name.split(':')
60 field_name = obj_composition.field.rel.related_name
61 if field_name == '+':
62 continue
63 #if app_label == obj._meta.app_label:
64 try:
65 cleanup_fct = "clean_%s" % field_name
66 if hasattr(self, cleanup_fct):
67 cleaner = getattr(self, cleanup_fct)
68 children = cleaner(obj, copy, parent, f, value)
69 else:
70 children = getattr(obj, field_name).all()
71 self.out(u"\n%s + [%s] %s" % (indent, app_label, field_name), 1)
72 except Exception, e:
73 # no reverse relation
74 #print "%s %s %s" % (indent, obj_composition, field_name)
75 self.out(" %s %s" % (field_name, str(e)), 1)
76 continue
77
78 for child in children:
79 child_copy = self.duplicate(child, parent=copy, level=level+1,)
80 parentship_fct = "parent_%s" % (field_name)
81 if hasattr(self,parentship_fct):
82 parentship = getattr(self, parentship_fct)
83 else:
84 parentship = getattr(self, "parent")
85 parentship(copy, child_copy)
86
87 if not self.dry_run:
88 child_copy.save()
89 return copy
90
91
92 class PosteCopier(SuperCopier):
93
94 def clean_rh_dossiers(self, source, copy, parent, field, value):
95 self.out("\n** SKIP **", 1)
96 return []
97
98 def clean_rh_comparaisons_internes(self, source, copy, parent, field, value):
99 self.out("\n** SKIP **", 1)
100 return []
101
102 def clean_rh_financements(self, source, copy, parent, field, value):
103 self.out("\n** SKIP **", 1)
104 return []
105
106 def clean_rh_pieces(self, source, copy, parent, field, value):
107 self.out("\n** SKIP **", 1)
108 return []
109
110 def clean_dae_dossiers(self, source, copy, parent, field, value):
111 self.out("\n** SKIP **", 1)
112 return []
113
114 def clean_fichier(self, source, copy, parent, field, value):
115 filename = value.path.split('/')[-1]
116 copy.poste_id = parent.id
117 new_value = rh.poste_piece_dispatch(copy, filename)
118
119 app, model, id, f = new_value.split('/')
120 app_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app)
121 model_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app, model)
122 id_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app, model, id)
123 if not os.path.exists(app_path):
124 os.mkdir(app_path)
125 if not os.path.exists(model_path):
126 os.mkdir(model_path)
127 if not os.path.exists(id_path):
128 os.mkdir(id_path)
129 src = value.path
130 dst = os.path.join(settings.PRIVE_MEDIA_ROOT, new_value)
131 shutil.copy(src, dst)
132 setattr(copy, field.name, new_value)
133
134
135 class DossierCopier(SuperCopier):
136
137 def clean_employe(self, source, copy, field, value):
138 if source.employe.id_rh is not None:
139 copy.employe = source.employe.id_rh
140 else:
141 nouvel_employe = rh.Employe()
142 nouvel_employe.nom = source.employe.nom
143 nouvel_employe.prenom = source.employe.prenom
144 nouvel_employe.genre = source.employe.genre
145 nouvel_employe.save()
146 copy.employe = nouvel_employe
147
148 def clean_fichier(self, source, copy, field, value):
149 pass
150
151 def clean_poste(self, source, copy, field, value):
152 pass
153
154
155 class Command(BaseCommand):
156
157
158 def handle(self, *args, **options):
159
160 if len(args) < 2:
161 print len(args)
162 self.stdout.write("dae <modele> <id> <run>\n")
163 return
164
165 classname = args[0]
166 klass = getattr(dae, classname, None)
167 if klass is None:
168 self.stdout.write("Le modèle %s n'existe pas dans DAE.\n" % classname)
169
170 id = int(args[1])
171 try:
172 obj = klass.objects.get(id=id)
173 except:
174 self.stdout.write("L'id %s n'existe pas pour le modèle %s dans DAE.\n" % \
175 (id, classname))
176 return
177
178
179 dry_run = True
180 if len(args) == 3 and args[2] == "run":
181 dry_run = False
182
183 verbosity = 0
184 if len(args) == 4:
185 verbosity = int(args[3])
186
187 copier = globals()[ "%sCopier" % classname.title()](verbosity=verbosity,
188 dry_run=dry_run)
189
190 copy = copier.duplicate(obj)
191 self.stdout.write(u"\n[%s] DAE:%s => RH:%s\n" % (obj.__class__.__name__, obj.id, copy.id))
192