a43283e1981a80367126b24f98c9db47892cd445
1 # -*- coding: utf-8 -*-
8 from django
.conf
import settings
9 from project
.rh
import models
as rh
13 class SuperCopier(object):
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.
20 stdout
= codecs
.getwriter('utf8')(sys
.stdout
)
22 def __init__(self
, dry_run
=True, verbosity
=0):
23 self
.verbosity
= verbosity
24 self
.dry_run
= dry_run
26 def out(self
, txt
, level
=0):
27 if self
.verbosity
>= level
:
28 self
.stdout
.write(txt
)
30 def clean_id(self
, source
, copy
, parent
, field
, value
):
34 def parent(self
, parent
, child
):
35 klass
= parent
.__class__
.__name__
.lower()
37 setattr(child
, k
, parent
.id)
39 def duplicate(self
, obj
, parent
=None, level
=0):
40 klass
= getattr(rh
, obj
.__class__
.__name__
)
44 self
.out(u
"\n%s [%s] %s" % (indent
, str(obj
.__class__
),
47 for f
in obj
._meta
.fields
:
48 value
= getattr(obj
, f
.name
)
50 self
.out(u
"\n%s * %s: %s " % (indent
, f
.name
, value
), 2)
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
)
57 setattr(copy
, f
.name
, value
)
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
67 self
.out(u
"\n%s + [%s] %s " % (indent
, app_label
, field_name
), 1)
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
)
74 children
= getattr(obj
, field_name
).all()
77 self
.out(" %s %s" % (field_name
, str(e
)), 1)
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
)
86 parentship
= getattr(self
, "parent")
87 parentship(copy
, child_copy
)
94 class PosteCopier(SuperCopier
):
96 def clean_rh_dossiers(self
, source
, copy
, parent
, field
, value
):
100 def clean_rh_comparaisons_internes(self
, source
, copy
, parent
, field
, value
):
104 def clean_rh_financements(self
, source
, copy
, parent
, field
, value
):
108 def clean_rh_pieces(self
, source
, copy
, parent
, field
, value
):
112 def clean_dae_dossiers(self
, source
, copy
, parent
, field
, value
):
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
)
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
):
127 if not os
.path
.exists(model_path
):
129 if not os
.path
.exists(id_path
):
132 dst
= os
.path
.join(settings
.PRIVE_MEDIA_ROOT
, new_value
)
133 shutil
.copy(src
, dst
)
134 setattr(copy
, field
.name
, new_value
)
137 class DossierCopier(SuperCopier
):
139 def clean_rh_contrats(self
, source
, copy
, parent
, field
, value
):
143 def clean_rh_dossierpieces(self
, source
, copy
, parent
, field
, value
):
147 def clean_rh_comparaisons(self
, source
, copy
, parent
, field
, value
):
151 def clean_rh_remunerations(self
, source
, copy
, parent
, field
, value
):
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
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
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
171 def clean_fichier(self
, source
, copy
, parent
, field
, value
):
172 filename
= value
.path
.split('/')[-1]
173 copy
.dossier_id
= parent
.id
175 ct
= copy
.__class__
.__name__
.lower()
177 new_value
= rh
.contrat_dispatch(copy
, filename
)
178 elif ct
== 'dossierpiece':
179 new_value
= rh
.dossier_piece_dispatch(copy
, filename
)
181 raise Exception('fichier %s à mapper!' % ct
)
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
):
189 if not os
.path
.exists(model_path
):
191 if not os
.path
.exists(id_path
):
194 dst
= os
.path
.join(settings
.PRIVE_MEDIA_ROOT
, new_value
)
195 shutil
.copy(src
, dst
)
196 setattr(copy
, field
.name
, new_value
)