skip info
[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 SKIP = "** SKIP **"
12
13 class SuperCopier(object):
14 """
15 Classe abstraite pour faire de la copie profonde sur des objets en BD.
16 Elle prévoit des hooks pour traiter des propriétés en particulier.
17 """
18 dry_run = True
19 verbosity = 0
20 stdout = codecs.getwriter('utf8')(sys.stdout)
21
22 def __init__(self, dry_run=True, verbosity=0):
23 self.verbosity = verbosity
24 self.dry_run = dry_run
25
26 def out(self, txt, level=0):
27 if self.verbosity >= level:
28 self.stdout.write(txt)
29
30 def clean_id(self, source, copy, parent, field, value):
31 self.out(SKIP, 1)
32 return []
33
34 def parent(self, parent, child):
35 klass = parent.__class__.__name__.lower()
36 k = "%s_id" % klass
37 setattr(child, k, parent.id)
38
39 def duplicate(self, obj, parent=None, level=0):
40 klass = getattr(rh, obj.__class__.__name__)
41 copy = klass()
42
43 indent = " "*level*4
44 self.out(u"\n%s [%s] %s" % (indent, str(obj.__class__),
45 obj), 1)
46
47 for f in obj._meta.fields:
48 value = getattr(obj, f.name)
49
50 self.out(u"\n%s * %s: %s " % (indent, f.name, value), 2)
51
52 cleanup_fct = "clean_%s" % f.name
53 if hasattr(self, cleanup_fct):
54 cleaner = getattr(self, cleanup_fct)
55 cleaner(obj, copy, parent, f, value)
56 else:
57 setattr(copy, f.name, value)
58
59 if not self.dry_run:
60 copy.save()
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
89 if not self.dry_run:
90 child_copy.save()
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_fichier(self, source, copy, parent, field, value):
117 filename = value.path.split('/')[-1]
118 copy.poste_id = parent.id
119 new_value = rh.poste_piece_dispatch(copy, filename)
120
121 app, model, id, f = new_value.split('/')
122 app_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app)
123 model_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app, model)
124 id_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app, model, id)
125 if not os.path.exists(app_path):
126 os.mkdir(app_path)
127 if not os.path.exists(model_path):
128 os.mkdir(model_path)
129 if not os.path.exists(id_path):
130 os.mkdir(id_path)
131 src = value.path
132 dst = os.path.join(settings.PRIVE_MEDIA_ROOT, new_value)
133 shutil.copy(src, dst)
134 setattr(copy, field.name, new_value)
135
136
137 class DossierCopier(SuperCopier):
138
139 def clean_rh_contrats(self, source, copy, parent, field, value):
140 self.out(SKIP, 1)
141 return []
142
143 def clean_rh_dossierpieces(self, source, copy, parent, field, value):
144 self.out(SKIP, 1)
145 return []
146
147 def clean_rh_comparaisons(self, source, copy, parent, field, value):
148 self.out(SKIP, 1)
149 return []
150
151 def clean_rh_remunerations(self, source, copy, parent, field, value):
152 self.out(SKIP, 1)
153 return []
154
155 def clean_employe(self, source, copy, parent, field, value):
156 if source.employe.id_rh is not None:
157 copy.employe = source.employe.id_rh
158 else:
159 nouvel_employe = rh.Employe()
160 nouvel_employe.nom = source.employe.nom
161 nouvel_employe.prenom = source.employe.prenom
162 nouvel_employe.genre = source.employe.genre
163 nouvel_employe.save()
164 copy.employe = nouvel_employe
165
166 def clean_poste(self, source, copy, parent, field, value):
167 copier = PosteCopier(verbosity=self.verbosity, dry_run=self.dry_run)
168 poste = copier.duplicate(value)
169 copy.poste_id = poste.id
170
171 def clean_fichier(self, source, copy, parent, field, value):
172 filename = value.path.split('/')[-1]
173 copy.dossier_id = parent.id
174
175 ct = copy.__class__.__name__.lower()
176 if ct == 'contrat':
177 new_value = rh.contrat_dispatch(copy, filename)
178 elif ct == 'dossierpiece':
179 new_value = rh.dossier_piece_dispatch(copy, filename)
180 else:
181 raise Exception('fichier %s à mapper!' % ct)
182
183 app, model, id, f = new_value.split('/')
184 app_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app)
185 model_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app, model)
186 id_path = os.path.join(settings.PRIVE_MEDIA_ROOT, app, model, id)
187 if not os.path.exists(app_path):
188 os.mkdir(app_path)
189 if not os.path.exists(model_path):
190 os.mkdir(model_path)
191 if not os.path.exists(id_path):
192 os.mkdir(id_path)
193 src = value.path
194 dst = os.path.join(settings.PRIVE_MEDIA_ROOT, new_value)
195 shutil.copy(src, dst)
196 setattr(copy, field.name, new_value)
197
198