import DAE->RH
[auf_rh_dae.git] / project / dae / exporter.py
1 # -*- coding: utf-8 -*-
2
3 import sys
4 import codecs
5 import os
6 import shutil
7
8 from django.conf import settings
9 from project.rh import models as rh
10 import models as dae
11
12 SKIP = "** SKIP **"
13
14 class SuperCopier(object):
15 """
16 Classe abstraite pour faire de la copie profonde sur des objets en BD.
17 Elle prévoit des hooks pour traiter des propriétés en particulier.
18 """
19 dry_run = True
20 verbosity = 0
21 stdout = codecs.getwriter('utf8')(sys.stdout)
22
23 def __init__(self, dry_run=True, verbosity=0):
24 self.verbosity = verbosity
25 self.dry_run = dry_run
26
27 def out(self, txt, level=0):
28 if self.verbosity >= level:
29 self.stdout.write(txt)
30
31 def clean_id(self, source, copy, parent, field, value):
32 self.out(SKIP, 2)
33 return []
34
35 def parent(self, parent, child):
36 if not self.dry_run:
37 parent.save()
38 klass = parent.__class__.__name__.lower()
39 k = "%s_id" % klass
40 setattr(child, k, parent.id)
41
42 def duplicate(self, obj, parent=None, level=0):
43 klass = getattr(rh, obj.__class__.__name__)
44 copy = klass()
45
46 indent = " "*level*4
47 self.out(u"\n%s [%s] %s" % (indent, str(obj.__class__),
48 obj), 1)
49
50 for f in obj._meta.fields:
51 value = getattr(obj, f.name)
52
53 self.out(u"\n%s * %s: %s " % (indent, f.name, value), 2)
54
55 cleanup_fct = "clean_%s" % f.name
56 if hasattr(self, cleanup_fct):
57 cleaner = getattr(self, cleanup_fct)
58 cleaner(obj, copy, parent, f, value)
59 else:
60 setattr(copy, f.name, value)
61
62 for obj_composition in obj._meta._related_objects_cache:
63 app_label, dummy = obj_composition.name.split(':')
64 field_name = obj_composition.field.rel.related_name
65 if field_name == '+':
66 continue
67 self.out(u"\n%s + [%s] %s " % (indent, app_label, field_name), 1)
68 try:
69 cleanup_fct = "clean_%s" % field_name
70 if hasattr(self, cleanup_fct):
71 cleaner = getattr(self, cleanup_fct)
72 children = cleaner(obj, copy, parent, f, value)
73 else:
74 children = getattr(obj, field_name).all()
75 except Exception, e:
76 # no reverse relation
77 self.out(" %s %s" % (field_name, str(e)), 1)
78 continue
79
80 for child in children:
81 child_copy = self.duplicate(child, parent=copy, level=level+1,)
82 parentship_fct = "parent_%s" % (field_name)
83 if hasattr(self,parentship_fct):
84 parentship = getattr(self, parentship_fct)
85 else:
86 parentship = getattr(self, "parent")
87 parentship(copy, child_copy)
88 if not self.dry_run:
89 child_copy.save()
90
91 return copy
92
93 def copy(self, obj):
94 return self.duplicate(obj)
95
96
97 class PosteCopier(SuperCopier):
98
99 def copy(self, obj):
100 copy = super(PosteCopier, self).copy(obj)
101 dae.ImportPoste(dae=obj, rh=copy).save()
102 return copy
103
104 def clean_rh_dossiers(self, source, copy, parent, field, value):
105 self.out(SKIP, 1)
106 return []
107
108 def clean_rh_comparaisons_internes(self, source, copy, parent, field, value):
109 self.out(SKIP, 1)
110 return []
111
112 def clean_rh_financements(self, source, copy, parent, field, value):
113 self.out(SKIP, 1)
114 return []
115
116 def clean_rh_pieces(self, source, copy, parent, field, value):
117 self.out(SKIP, 1)
118 return []
119
120 def clean_dae_dossiers(self, source, copy, parent, field, value):
121 self.out(SKIP, 1)
122 return []
123
124 def clean_poste(self, source, copy, parent, field, value):
125 self.out(SKIP, 1)
126 return []
127
128 def clean_fichier(self, source, copy, parent, field, value):
129 filename = value.path.split('/')[-1]
130 copy.poste_id = parent.id
131 new_value = rh.poste_piece_dispatch(copy, filename)
132
133 app, model, id, f = new_value.split('/')
134 app_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app)
135 model_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app, model)
136 id_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app, model, id)
137 if not os.path.exists(app_path):
138 os.mkdir(app_path)
139 if not os.path.exists(model_path):
140 os.mkdir(model_path)
141 if not os.path.exists(id_path):
142 os.mkdir(id_path)
143 src = value.path
144 dst = os.path.join(settings.PRIVE_MEDIA_ROOT, new_value)
145 shutil.copy(src, dst)
146 setattr(copy, field.name, new_value)
147
148
149 class DossierCopier(SuperCopier):
150
151 def copy(self, obj):
152 copy = super(DossierCopier, self).copy(obj)
153 dae.ImportDossier(dae=obj, rh=copy).save()
154 return copy
155
156 def clean_rh_contrats(self, source, copy, parent, field, value):
157 self.out(SKIP, 1)
158 return []
159
160 def clean_rh_dossierpieces(self, source, copy, parent, field, value):
161 self.out(SKIP, 1)
162 return []
163
164 def clean_rh_comparaisons(self, source, copy, parent, field, value):
165 self.out(SKIP, 1)
166 return []
167
168 def clean_rh_remunerations(self, source, copy, parent, field, value):
169 self.out(SKIP, 1)
170 return []
171
172 def clean_dossier(self, source, copy, parent, field, value):
173 self.out(SKIP, 1)
174 return []
175
176
177 def clean_employe(self, source, copy, parent, field, value):
178 if source.employe.id_rh is not None:
179 copy.employe = source.employe.id_rh
180 else:
181 nouvel_employe = rh.Employe()
182 nouvel_employe.nom = source.employe.nom
183 nouvel_employe.prenom = source.employe.prenom
184 nouvel_employe.genre = source.employe.genre
185 nouvel_employe.save()
186 copy.employe = nouvel_employe
187
188 def clean_poste(self, source, copy, parent, field, value):
189 # poste comparaison a un champs nommé poste
190 if isinstance(value, unicode):
191 setattr(copy, field.name, value)
192 return
193
194 if dae.ImportPoste.objects.filter(dae=value).exists():
195 poste = dae.ImportPoste.get(dae=value).rh
196 else:
197 copier = PosteCopier(verbosity=self.verbosity,
198 dry_run=self.dry_run)
199 poste = copier.duplicate(value)
200 copy.poste_id = poste.id
201
202 def clean_fichier(self, source, copy, parent, field, value):
203 filename = value.path.split('/')[-1]
204 copy.dossier_id = parent.id
205
206 ct = copy.__class__.__name__.lower()
207 if ct == 'contrat':
208 new_value = rh.contrat_dispatch(copy, filename)
209 elif ct == 'dossierpiece':
210 new_value = rh.dossier_piece_dispatch(copy, filename)
211 else:
212 raise Exception('fichier %s à mapper!' % ct)
213
214 app, model, id, f = new_value.split('/')
215 app_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app)
216 model_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app, model)
217 id_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app, model, id)
218 if not os.path.exists(app_path):
219 os.mkdir(app_path)
220 if not os.path.exists(model_path):
221 os.mkdir(model_path)
222 if not os.path.exists(id_path):
223 os.mkdir(id_path)
224 src = value.path
225 dst = os.path.join(settings.PRIVE_MEDIA_ROOT, new_value)
226 shutil.copy(src, dst)
227 setattr(copy, field.name, new_value)