import DAE
[auf_rh_dae.git] / project / dae / management / commands / dae.py
index 63332e4..0c2b404 100644 (file)
@@ -1,74 +1,14 @@
 # -*- encoding: utf-8 -*-
 
-import codecs
 from django.core.management.base import BaseCommand
+
 from project.dae import  models as dae
-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:
             print len(args)
             self.stdout.write("dae <modele> <id> <run>\n")
@@ -88,11 +28,7 @@ class Command(BaseCommand):
             return
 
 
-        dry_run = True
-        if len(args) == 3 and args[2] == "run":
-            dry_run = False
-
-        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))
+        copy = obj.importer()
+        self.stdout.write(u"\n[%s] DAE:%s => RH:%s\n" %
+                (obj.__class__.__name__, obj.id, copy.id))