poste import + liens dans RH
[auf_rh_dae.git] / project / dae / exporter.py
index feb290f..5b64c0f 100644 (file)
@@ -6,7 +6,10 @@ import os
 import  shutil
 
 from django.conf import settings
-from project.rh import  models as rh
+from project.rh import models as rh
+import models as dae
+
+SKIP = "** SKIP **"
 
 class SuperCopier(object):
     """
@@ -26,9 +29,12 @@ class SuperCopier(object):
             self.stdout.write(txt)
 
     def clean_id(self, source, copy, parent, field, value):
-        pass
+        self.out(SKIP, 2)
+        return []
 
     def parent(self, parent, child):
+        if not self.dry_run:
+            parent.save()
         klass = parent.__class__.__name__.lower()
         k = "%s_id" % klass
         setattr(child, k, parent.id)
@@ -43,7 +49,8 @@ class SuperCopier(object):
         
         for f in obj._meta.fields:
             value = getattr(obj, f.name)
-            self.out(u"\n%s * %s: %s" % (indent, f.name, value), 2)
+
+            self.out(u"\n%s * %s: %s " % (indent, f.name, value), 2)
 
             cleanup_fct = "clean_%s" % f.name
             if hasattr(self, cleanup_fct):
@@ -52,14 +59,12 @@ class SuperCopier(object):
             else:
                 setattr(copy, f.name, value)
 
-        if not self.dry_run:
-            copy.save()
-    
         for obj_composition in obj._meta._related_objects_cache:
             app_label, dummy = obj_composition.name.split(':')
             field_name = obj_composition.field.rel.related_name
             if field_name == '+':
                 continue
+            self.out(u"\n%s + [%s] %s " % (indent, app_label, field_name), 1)
             try:
                 cleanup_fct = "clean_%s" % field_name
                 if hasattr(self, cleanup_fct):
@@ -67,12 +72,11 @@ class SuperCopier(object):
                     children = cleaner(obj, copy, parent, f, value)
                 else:
                     children = getattr(obj, field_name).all()
-                self.out(u"\n%s + [%s] %s" % (indent, app_label, field_name), 1)
             except Exception, e:
                 # no reverse relation
                 self.out(" %s %s" % (field_name, str(e)), 1)
                 continue
-            
+
             for child in children:
                 child_copy = self.duplicate(child, parent=copy, level=level+1,)
                 parentship_fct = "parent_%s" % (field_name)
@@ -81,32 +85,44 @@ class SuperCopier(object):
                 else:
                     parentship = getattr(self, "parent")
                 parentship(copy, child_copy)
-                
                 if not self.dry_run:
                     child_copy.save()
+    
         return copy
 
+    def copy(self, obj):
+        return self.duplicate(obj)
+
 
 class PosteCopier(SuperCopier):
 
+    def copy(self, obj):
+        copy = super(PosteCopier, self).copy(obj)
+        dae.ImportPoste(dae=obj, rh=copy).save()
+        return copy
+
     def clean_rh_dossiers(self, source, copy, parent, field, value):
-        self.out("\n** SKIP **", 1)
+        self.out(SKIP, 1)
         return []
 
     def clean_rh_comparaisons_internes(self, source, copy, parent, field, value):
-        self.out("\n** SKIP **", 1)
+        self.out(SKIP, 1)
         return []
 
     def clean_rh_financements(self, source, copy, parent, field, value):
-        self.out("\n** SKIP **", 1)
+        self.out(SKIP, 1)
         return []
 
     def clean_rh_pieces(self, source, copy, parent, field, value):
-        self.out("\n** SKIP **", 1)
+        self.out(SKIP, 1)
         return []
 
     def clean_dae_dossiers(self, source, copy, parent, field, value):
-        self.out("\n** SKIP **", 1)
+        self.out(SKIP, 1)
+        return []
+
+    def clean_poste(self, source, copy, parent, field, value):
+        self.out(SKIP, 1)
         return []
 
     def clean_fichier(self, source, copy, parent, field, value):
@@ -132,22 +148,32 @@ class PosteCopier(SuperCopier):
 
 class DossierCopier(SuperCopier):
 
+    def copy(self, obj):
+        copy = super(DossierCopier, self).copy(obj)
+        dae.ImportDossier(dae=obj, rh=copy).save()
+        return copy
+
     def clean_rh_contrats(self, source, copy, parent, field, value):
-        self.out("\n** SKIP **", 1)
+        self.out(SKIP, 1)
         return []
 
     def clean_rh_dossierpieces(self, source, copy, parent, field, value):
-        self.out("\n** SKIP **", 1)
+        self.out(SKIP, 1)
         return []
 
     def clean_rh_comparaisons(self, source, copy, parent, field, value):
-        self.out("\n** SKIP **", 1)
+        self.out(SKIP, 1)
         return []
     
     def clean_rh_remunerations(self, source, copy, parent, field, value):
-        self.out("\n** SKIP **", 1)
+        self.out(SKIP, 1)
         return []
 
+    def clean_dossier(self, source, copy, parent, field, value):
+        self.out(SKIP, 1)
+        return []
+
+
     def clean_employe(self, source, copy, parent, field, value):
         if source.employe.id_rh is not None:
             copy.employe = source.employe.id_rh
@@ -160,19 +186,23 @@ class DossierCopier(SuperCopier):
             copy.employe = nouvel_employe
 
     def clean_poste(self, source, copy, parent, field, value):
-        copier = PosteCopier(verbosity=self.verbosity, dry_run=self.dry_run)
-        poste = copier.duplicate(value)
-        copy.poste_id = poste.id
-
-    def clean_fichier(self, source, copy, parent, field, value):
-        try:
-            filename = value.path.split('/')[-1]
-        except:
+        # poste comparaison a un champs nommé poste
+        if isinstance(value, unicode):
             setattr(copy, field.name, value)
             return
 
+        if dae.ImportPoste.objects.filter(dae=value).exists():
+            poste = dae.ImportPoste.objects.get(dae=value).rh
+        else:
+            copier = PosteCopier(verbosity=self.verbosity,
+                    dry_run=self.dry_run)
+            poste = copier.copy(value)
+        copy.poste_id = poste.id
+
+    def clean_fichier(self, source, copy, parent, field, value):
+        filename = value.path.split('/')[-1]
         copy.dossier_id = parent.id
-        
+
         ct = copy.__class__.__name__.lower()
         if ct == 'contrat':
             new_value = rh.contrat_dispatch(copy, filename)
@@ -195,5 +225,3 @@ class DossierCopier(SuperCopier):
         dst = os.path.join(settings.PRIVE_MEDIA_ROOT, new_value)
         shutil.copy(src, dst)
         setattr(copy, field.name, new_value)
-
-