dossier
[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_rh_contrats(self, source, copy, parent, field, value):
138 self.out("\n** SKIP **", 1)
139 return []
140
141 def clean_rh_dossierpieces(self, source, copy, parent, field, value):
142 self.out("\n** SKIP **", 1)
143 return []
144
145 def clean_rh_comparaisons(self, source, copy, parent, field, value):
146 self.out("\n** SKIP **", 1)
147 return []
148
149 def clean_rh_remunerations(self, source, copy, parent, field, value):
150 self.out("\n** SKIP **", 1)
151 return []
152
153 def clean_employe(self, source, copy, parent, field, value):
154 if source.employe.id_rh is not None:
155 copy.employe = source.employe.id_rh
156 else:
157 nouvel_employe = rh.Employe()
158 nouvel_employe.nom = source.employe.nom
159 nouvel_employe.prenom = source.employe.prenom
160 nouvel_employe.genre = source.employe.genre
161 nouvel_employe.save()
162 copy.employe = nouvel_employe
163
164 def clean_poste(self, source, copy, parent, field, value):
165 copier = PosteCopier(verbosity=self.verbosity, dry_run=self.dry_run)
166 poste = copier.duplicate(value)
167 copy.poste_id = poste.id
168
169 def clean_fichier(self, source, copy, parent, field, value):
170 try:
171 filename = value.path.split('/')[-1]
172 except:
173 setattr(copy, field.name, value)
174 return
175
176 copy.dossier_id = parent.id
177
178 ct = copy.__class__.__name__.lower()
179 if ct == 'contrat':
180 new_value = rh.contrat_dispatch(copy, filename)
181 elif ct == 'dossierpiece':
182 new_value = rh.dossier_piece_dispatch(copy, filename)
183 else:
184 raise Exception('fichier %s à mapper!' % ct)
185
186 app, model, id, f = new_value.split('/')
187 app_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app)
188 model_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app, model)
189 id_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app, model, id)
190 if not os.path.exists(app_path):
191 os.mkdir(app_path)
192 if not os.path.exists(model_path):
193 os.mkdir(model_path)
194 if not os.path.exists(id_path):
195 os.mkdir(id_path)
196 src = value.path
197 dst = os.path.join(settings.PRIVE_MEDIA_ROOT, new_value)
198 shutil.copy(src, dst)
199 setattr(copy, field.name, new_value)
200
201
202 class Command(BaseCommand):
203
204
205 def handle(self, *args, **options):
206
207 if len(args) < 2:
208 print len(args)
209 self.stdout.write("dae <modele> <id> <run>\n")
210 return
211
212 classname = args[0]
213 klass = getattr(dae, classname, None)
214 if klass is None:
215 self.stdout.write("Le modèle %s n'existe pas dans DAE.\n" % classname)
216
217 id = int(args[1])
218 try:
219 obj = klass.objects.get(id=id)
220 except:
221 self.stdout.write("L'id %s n'existe pas pour le modèle %s dans DAE.\n" % \
222 (id, classname))
223 return
224
225
226 dry_run = True
227 if len(args) >= 3 and args[2] == "run":
228 dry_run = False
229
230 verbosity = 0
231 if len(args) >= 4:
232 verbosity = int(args[3])
233
234 copier = globals()[ "%sCopier" % classname.title()](verbosity=verbosity,
235 dry_run=dry_run)
236
237 copy = copier.duplicate(obj)
238 self.stdout.write(u"\n[%s] DAE:%s => RH:%s\n" % (obj.__class__.__name__, obj.id, copy.id))
239