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