feb290f007554ee5e51477518ee13f3bcd115b6b
[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
11 class SuperCopier(object):
12 """
13 Classe abstraite pour faire de la copie profonde sur des objets en BD.
14 Elle prévoit des hooks pour traiter des propriétés en particulier.
15 """
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 try:
64 cleanup_fct = "clean_%s" % field_name
65 if hasattr(self, cleanup_fct):
66 cleaner = getattr(self, cleanup_fct)
67 children = cleaner(obj, copy, parent, f, value)
68 else:
69 children = getattr(obj, field_name).all()
70 self.out(u"\n%s + [%s] %s" % (indent, app_label, field_name), 1)
71 except Exception, e:
72 # no reverse relation
73 self.out(" %s %s" % (field_name, str(e)), 1)
74 continue
75
76 for child in children:
77 child_copy = self.duplicate(child, parent=copy, level=level+1,)
78 parentship_fct = "parent_%s" % (field_name)
79 if hasattr(self,parentship_fct):
80 parentship = getattr(self, parentship_fct)
81 else:
82 parentship = getattr(self, "parent")
83 parentship(copy, child_copy)
84
85 if not self.dry_run:
86 child_copy.save()
87 return copy
88
89
90 class PosteCopier(SuperCopier):
91
92 def clean_rh_dossiers(self, source, copy, parent, field, value):
93 self.out("\n** SKIP **", 1)
94 return []
95
96 def clean_rh_comparaisons_internes(self, source, copy, parent, field, value):
97 self.out("\n** SKIP **", 1)
98 return []
99
100 def clean_rh_financements(self, source, copy, parent, field, value):
101 self.out("\n** SKIP **", 1)
102 return []
103
104 def clean_rh_pieces(self, source, copy, parent, field, value):
105 self.out("\n** SKIP **", 1)
106 return []
107
108 def clean_dae_dossiers(self, source, copy, parent, field, value):
109 self.out("\n** SKIP **", 1)
110 return []
111
112 def clean_fichier(self, source, copy, parent, field, value):
113 filename = value.path.split('/')[-1]
114 copy.poste_id = parent.id
115 new_value = rh.poste_piece_dispatch(copy, filename)
116
117 app, model, id, f = new_value.split('/')
118 app_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app)
119 model_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app, model)
120 id_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app, model, id)
121 if not os.path.exists(app_path):
122 os.mkdir(app_path)
123 if not os.path.exists(model_path):
124 os.mkdir(model_path)
125 if not os.path.exists(id_path):
126 os.mkdir(id_path)
127 src = value.path
128 dst = os.path.join(settings.PRIVE_MEDIA_ROOT, new_value)
129 shutil.copy(src, dst)
130 setattr(copy, field.name, new_value)
131
132
133 class DossierCopier(SuperCopier):
134
135 def clean_rh_contrats(self, source, copy, parent, field, value):
136 self.out("\n** SKIP **", 1)
137 return []
138
139 def clean_rh_dossierpieces(self, source, copy, parent, field, value):
140 self.out("\n** SKIP **", 1)
141 return []
142
143 def clean_rh_comparaisons(self, source, copy, parent, field, value):
144 self.out("\n** SKIP **", 1)
145 return []
146
147 def clean_rh_remunerations(self, source, copy, parent, field, value):
148 self.out("\n** SKIP **", 1)
149 return []
150
151 def clean_employe(self, source, copy, parent, field, value):
152 if source.employe.id_rh is not None:
153 copy.employe = source.employe.id_rh
154 else:
155 nouvel_employe = rh.Employe()
156 nouvel_employe.nom = source.employe.nom
157 nouvel_employe.prenom = source.employe.prenom
158 nouvel_employe.genre = source.employe.genre
159 nouvel_employe.save()
160 copy.employe = nouvel_employe
161
162 def clean_poste(self, source, copy, parent, field, value):
163 copier = PosteCopier(verbosity=self.verbosity, dry_run=self.dry_run)
164 poste = copier.duplicate(value)
165 copy.poste_id = poste.id
166
167 def clean_fichier(self, source, copy, parent, field, value):
168 try:
169 filename = value.path.split('/')[-1]
170 except:
171 setattr(copy, field.name, value)
172 return
173
174 copy.dossier_id = parent.id
175
176 ct = copy.__class__.__name__.lower()
177 if ct == 'contrat':
178 new_value = rh.contrat_dispatch(copy, filename)
179 elif ct == 'dossierpiece':
180 new_value = rh.dossier_piece_dispatch(copy, filename)
181 else:
182 raise Exception('fichier %s à mapper!' % ct)
183
184 app, model, id, f = new_value.split('/')
185 app_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app)
186 model_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app, model)
187 id_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app, model, id)
188 if not os.path.exists(app_path):
189 os.mkdir(app_path)
190 if not os.path.exists(model_path):
191 os.mkdir(model_path)
192 if not os.path.exists(id_path):
193 os.mkdir(id_path)
194 src = value.path
195 dst = os.path.join(settings.PRIVE_MEDIA_ROOT, new_value)
196 shutil.copy(src, dst)
197 setattr(copy, field.name, new_value)
198
199