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

index ae32173..97d631f 100644 (file)
@@ -1,30 +1,16 @@
 # -*- 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 explore(obj):
-        print obj
-        for f in obj._meta.fields:
-            def t(s):
-                s = "%s" % s
-                try:
-                    s = s.ljust(40)
-                    s = s.replace('\n', '')
-                    s = s[0:28]
-                except Exception, e:
-                    print e
-                    print s
-                    return
-                return s
-            print "%s%s%s" % (t(f.name), t(type(f).__name__), t(getattr(obj, f.name, "")))
-
     def handle(self, *args, **options):
 
+        self.stdout = codecs.getwriter('utf8')(self.stdout)
+
         if len(args) != 2:
             self.stdout.write("dae <modele> <id>\n")
             return
@@ -42,31 +28,51 @@ class Command(BaseCommand):
                     (id, classname))
             return
 
-        def copy_model(src, dst, exclude=[]):
-            keys = [f.name for f in src._meta.fields if f.name not in ['id', ] + exclude]
-            for k in keys:
-                setattr(dst, k, getattr(src, k))
-            return dst
 
-        def duplicate(obj, level=0):
-            print "%s [%s] %s" % ("|"*level*4, obj.__class__.__name__, obj)
+        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:
-                        print obj_composition
-                        print field_name
+                        # no reverse relation
+                        #print "%s %s %s" % (indent, obj_composition, field_name)
                         continue
                     for child in children.all():
-                        duplicate(child, level+1)
-                
-
-        duplicate(obj)
-        import pdb; pdb.set_trace()
-        
-
+                        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
 
+        copy = duplicate(obj, skip=['id', 'dae_dossiers', ])
+        print copy.__class__
+        print copy.id