employe
authorOlivier Larchevêque <olivier.larcheveque@auf.org>
Thu, 22 Mar 2012 20:55:17 +0000 (16:55 -0400)
committerOlivier Larchevêque <olivier.larcheveque@auf.org>
Thu, 22 Mar 2012 20:55:17 +0000 (16:55 -0400)
project/dae/management/commands/dae.py

index 97d631f..63332e4 100644 (file)
@@ -7,12 +7,71 @@ from project.rh import  models as rh
 
 class Command(BaseCommand):
 
+    def clean_employe(self, source, copy, field, value):
+        if source.employe.id_rh is not None:
+            copy.employe = source.employe.id_rh
+        else:
+            raise Exception('Creer un employe')
+
+    def duplicate(self, obj, level=0, skip=(), dry_run=True):
+        klass = getattr(rh, obj.__class__.__name__)
+        copy = klass()
+    
+        indent = " "*level*4
+        self.stdout.write(u"\n%s [%s] %s" % (indent, str(obj.__class__),
+            obj))
+        
+        self.stdout.write(u"\n*** SKIP: %s ***" % (", ".join(skip)))
+    
+        for f in [f for f in obj._meta.fields if f.name not in skip]:
+            value = getattr(obj, f.name)
+            self.stdout.write(u"\n%s * %s: %s" % (indent, f.name, value))
+
+            cleanup_fct = "clean_%s" % f.name
+            if hasattr(self, cleanup_fct):
+                cleaner = getattr(self, cleanup_fct)
+                cleaner(obj, copy, f, value)
+            else:
+                setattr(copy, f.name, value)
+    
+        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 in skip:
+                continue
+            if app_label == obj._meta.app_label:
+                try:
+                    children = getattr(obj, field_name)
+                    self.stdout.write(u"\n%s + [%s] %s" % (indent,
+                        app_label, field_name))
+                except:
+                    # no reverse relation
+                    #print "%s %s %s" % (indent, obj_composition, field_name)
+                    continue
+                for child in children.all():
+                    extra_skip = []
+                    
+                    for f in child._meta.fields:
+                        if getattr(child, f.name) == obj:
+                            extra_skip.append(f.name)
+                    
+                    child_copy = self.duplicate(child, level+1, skip +
+                            extra_skip, dry_run)
+                    if not dry_run:
+                        copy.save()
+                    setattr(child_copy, "%s_id" % extra_skip[0], copy.id)
+                    if not dry_run:
+                        child_copy.save()
+        return copy
+
+
     def handle(self, *args, **options):
 
         self.stdout = codecs.getwriter('utf8')(self.stdout)
 
-        if len(args) != 2:
-            self.stdout.write("dae <modele> <id>\n")
+        if len(args) < 2:
+            print len(args)
+            self.stdout.write("dae <modele> <id> <run>\n")
             return
 
         classname = args[0]
@@ -29,50 +88,11 @@ class Command(BaseCommand):
             return
 
 
-        def duplicate(obj, level=0, skip=()):
-
-            klass = getattr(rh, obj.__class__.__name__)
-            copy = klass()
-
-            indent = " "*level*4
-            self.stdout.write(u"\n%s [%s] %s" % (indent, str(obj.__class__),
-                obj))
-            
-            self.stdout.write(u"\n*** SKIP: %s ***" % (", ".join(skip)))
-
-            for f in [f for f in obj._meta.fields if f.name not in skip]:
-                value = getattr(obj, f.name)
-                self.stdout.write(u"\n%s * %s: %s" % (indent, f.name, value))
-                setattr(copy, f.name, value)
-
-            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 in skip:
-                    continue
-                if app_label == obj._meta.app_label:
-                    try:
-                        children = getattr(obj, field_name)
-                        self.stdout.write(u"\n%s + [%s] %s" % (indent,
-                            app_label, field_name))
-                    except:
-                        # no reverse relation
-                        #print "%s %s %s" % (indent, obj_composition, field_name)
-                        continue
-                    for child in children.all():
-                        extra_skip = []
-                        
-                        for f in child._meta.fields:
-                            if getattr(child, f.name) == obj:
-                                extra_skip.append(f.name)
-                        
-                        child_copy = duplicate(child, level+1, skip + extra_skip)
-                        copy.save()
-                        setattr(child_copy, "%s_id" % extra_skip[0], copy.id)
-                        child_copy.save()
-            return copy
+        dry_run = True
+        if len(args) == 3 and args[2] == "run":
+            dry_run = False
 
-        copy = duplicate(obj, skip=['id', 'dae_dossiers', ])
-        print copy.__class__
-        print copy.id
+        copy = self.duplicate(obj, skip=['id', 'dae_dossiers', ],
+                dry_run=dry_run)
+        self.stdout.write(u"\n[%s] DAE:%s => RH:%s\n" % (obj.__class__.__name__, obj.id, copy.id))