poste import + liens dans RH
[auf_rh_dae.git] / project / dae / exporter.py
CommitLineData
2d25d3ca
OL
1# -*- coding: utf-8 -*-
2
3import sys
4import codecs
5import os
6import shutil
7
8from django.conf import settings
47d7067b
OL
9from project.rh import models as rh
10import models as dae
2d25d3ca 11
c12e724e
OL
12SKIP = "** SKIP **"
13
2d25d3ca
OL
14class 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):
47d7067b 32 self.out(SKIP, 2)
c12e724e 33 return []
2d25d3ca
OL
34
35 def parent(self, parent, child):
47d7067b
OL
36 if not self.dry_run:
37 parent.save()
2d25d3ca
OL
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)
c12e724e
OL
52
53 self.out(u"\n%s * %s: %s " % (indent, f.name, value), 2)
2d25d3ca
OL
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
2d25d3ca
OL
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
c12e724e 67 self.out(u"\n%s + [%s] %s " % (indent, app_label, field_name), 1)
2d25d3ca
OL
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()
2d25d3ca
OL
75 except Exception, e:
76 # no reverse relation
77 self.out(" %s %s" % (field_name, str(e)), 1)
78 continue
47d7067b 79
2d25d3ca
OL
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)
2d25d3ca
OL
88 if not self.dry_run:
89 child_copy.save()
47d7067b 90
2d25d3ca
OL
91 return copy
92
59c67b87
OL
93 def copy(self, obj):
94 return self.duplicate(obj)
95
2d25d3ca
OL
96
97class PosteCopier(SuperCopier):
98
59c67b87
OL
99 def copy(self, obj):
100 copy = super(PosteCopier, self).copy(obj)
101 dae.ImportPoste(dae=obj, rh=copy).save()
102 return copy
103
2d25d3ca 104 def clean_rh_dossiers(self, source, copy, parent, field, value):
c12e724e 105 self.out(SKIP, 1)
2d25d3ca
OL
106 return []
107
108 def clean_rh_comparaisons_internes(self, source, copy, parent, field, value):
c12e724e 109 self.out(SKIP, 1)
2d25d3ca
OL
110 return []
111
112 def clean_rh_financements(self, source, copy, parent, field, value):
c12e724e 113 self.out(SKIP, 1)
2d25d3ca
OL
114 return []
115
116 def clean_rh_pieces(self, source, copy, parent, field, value):
c12e724e 117 self.out(SKIP, 1)
2d25d3ca
OL
118 return []
119
120 def clean_dae_dossiers(self, source, copy, parent, field, value):
c12e724e 121 self.out(SKIP, 1)
2d25d3ca
OL
122 return []
123
47d7067b
OL
124 def clean_poste(self, source, copy, parent, field, value):
125 self.out(SKIP, 1)
126 return []
127
2d25d3ca
OL
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
149class DossierCopier(SuperCopier):
150
59c67b87
OL
151 def copy(self, obj):
152 copy = super(DossierCopier, self).copy(obj)
153 dae.ImportDossier(dae=obj, rh=copy).save()
154 return copy
155
2d25d3ca 156 def clean_rh_contrats(self, source, copy, parent, field, value):
c12e724e 157 self.out(SKIP, 1)
2d25d3ca
OL
158 return []
159
160 def clean_rh_dossierpieces(self, source, copy, parent, field, value):
c12e724e 161 self.out(SKIP, 1)
2d25d3ca
OL
162 return []
163
164 def clean_rh_comparaisons(self, source, copy, parent, field, value):
c12e724e 165 self.out(SKIP, 1)
2d25d3ca
OL
166 return []
167
168 def clean_rh_remunerations(self, source, copy, parent, field, value):
c12e724e 169 self.out(SKIP, 1)
2d25d3ca
OL
170 return []
171
47d7067b
OL
172 def clean_dossier(self, source, copy, parent, field, value):
173 self.out(SKIP, 1)
174 return []
175
176
2d25d3ca
OL
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):
47d7067b
OL
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():
a47ed016 195 poste = dae.ImportPoste.objects.get(dae=value).rh
47d7067b
OL
196 else:
197 copier = PosteCopier(verbosity=self.verbosity,
198 dry_run=self.dry_run)
a47ed016 199 poste = copier.copy(value)
2d25d3ca
OL
200 copy.poste_id = poste.id
201
202 def clean_fichier(self, source, copy, parent, field, value):
c12e724e 203 filename = value.path.split('/')[-1]
2d25d3ca 204 copy.dossier_id = parent.id
c12e724e 205
2d25d3ca
OL
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)