fix PJ
[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 if not self.dry_run:
102 dae.ImportPoste(dae=obj, rh=copy).save()
103 return copy
104
105 def clean_rh_dossiers(self, source, copy, parent, field, value):
106 self.out(SKIP, 1)
107 return []
108
109 def clean_rh_comparaisons_internes(self, source, copy, parent, field, value):
110 self.out(SKIP, 1)
111 return []
112
113 def clean_rh_financements(self, source, copy, parent, field, value):
114 self.out(SKIP, 1)
115 return []
116
117 def clean_rh_pieces(self, source, copy, parent, field, value):
118 self.out(SKIP, 1)
119 return []
120
121 def clean_dae_dossiers(self, source, copy, parent, field, value):
122 self.out(SKIP, 1)
123 return []
124
125 def clean_poste(self, source, copy, parent, field, value):
126 self.out(SKIP, 1)
127 return []
128
129 def clean_fichier(self, source, copy, parent, field, value):
130 if not self.dry_run and parent.id is None:
131 parent.save()
132 filename = value.path.split('/')[-1]
133 copy.poste_id = parent.id
134 new_value = rh.poste_piece_dispatch(copy, filename)
135
136 app, model, id, f = new_value.split('/')
137 app_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app)
138 model_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app, model)
139 id_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app, model, id)
140 if not os.path.exists(app_path):
141 os.mkdir(app_path)
142 if not os.path.exists(model_path):
143 os.mkdir(model_path)
144 if not os.path.exists(id_path):
145 os.mkdir(id_path)
146 src = value.path
147 dst = os.path.join(settings.PRIVE_MEDIA_ROOT, new_value)
148 shutil.copy(src, dst)
149 setattr(copy, field.name, new_value)
150
151
152 class DossierCopier(SuperCopier):
153
154 def copy(self, obj):
155 copy = super(DossierCopier, self).copy(obj)
156 if not self.dry_run:
157 dae.ImportDossier(dae=obj, rh=copy).save()
158 return copy
159
160 def clean_rh_contrats(self, source, copy, parent, field, value):
161 self.out(SKIP, 1)
162 return []
163
164 def clean_rh_dossierpieces(self, source, copy, parent, field, value):
165 self.out(SKIP, 1)
166 return []
167
168 def clean_rh_comparaisons(self, source, copy, parent, field, value):
169 self.out(SKIP, 1)
170 return []
171
172 def clean_rh_remunerations(self, source, copy, parent, field, value):
173 self.out(SKIP, 1)
174 return []
175
176 def clean_dossier(self, source, copy, parent, field, value):
177 self.out(SKIP, 1)
178 return []
179
180
181 def clean_employe(self, source, copy, parent, field, value):
182 if source.employe.id_rh is not None:
183 copy.employe = source.employe.id_rh
184 else:
185 nouvel_employe = rh.Employe()
186 nouvel_employe.nom = source.employe.nom
187 nouvel_employe.prenom = source.employe.prenom
188 nouvel_employe.genre = source.employe.genre
189 nouvel_employe.save()
190 copy.employe = nouvel_employe
191
192 def clean_poste(self, source, copy, parent, field, value):
193 # poste comparaison a un champs nommé poste
194 if isinstance(value, unicode):
195 setattr(copy, field.name, value)
196 return
197
198 if dae.ImportPoste.objects.filter(dae=value).exists():
199 poste = dae.ImportPoste.objects.get(dae=value).rh
200 else:
201 copier = PosteCopier(verbosity=self.verbosity,
202 dry_run=self.dry_run)
203 poste = copier.copy(value)
204 copy.poste_id = poste.id
205
206 def clean_fichier(self, source, copy, parent, field, value):
207 if not self.dry_run and parent.id is None:
208 parent.save()
209 filename = value.path.split('/')[-1]
210 copy.dossier_id = parent.id
211
212 ct = copy.__class__.__name__.lower()
213 if ct == 'contrat':
214 new_value = rh.contrat_dispatch(copy, filename)
215 elif ct == 'dossierpiece':
216 new_value = rh.dossier_piece_dispatch(copy, filename)
217 else:
218 raise Exception('fichier %s à mapper!' % ct)
219
220 app, model, id, f = new_value.split('/')
221 app_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app)
222 model_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app, model)
223 id_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app, model, id)
224 if not os.path.exists(app_path):
225 os.mkdir(app_path)
226 if not os.path.exists(model_path):
227 os.mkdir(model_path)
228 if not os.path.exists(id_path):
229 os.mkdir(id_path)
230 src = value.path
231 dst = os.path.join(settings.PRIVE_MEDIA_ROOT, new_value)
232 shutil.copy(src, dst)
233 setattr(copy, field.name, new_value)