Merge branch 'hotfix' into olivier
authorOlivier Larchevêque <olivier.larcheveque@auf.org>
Thu, 22 Mar 2012 20:12:04 +0000 (16:12 -0400)
committerOlivier Larchevêque <olivier.larcheveque@auf.org>
Thu, 22 Mar 2012 20:12:04 +0000 (16:12 -0400)
project/dae/management/__init__.py [new file with mode: 0644]
project/dae/management/commands/__init__.py [new file with mode: 0644]
project/dae/management/commands/dae.py [new file with mode: 0644]
project/dae/models.py
project/rh/models.py

diff --git a/project/dae/management/__init__.py b/project/dae/management/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/project/dae/management/commands/__init__.py b/project/dae/management/commands/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/project/dae/management/commands/dae.py b/project/dae/management/commands/dae.py
new file mode 100644 (file)
index 0000000..97d631f
--- /dev/null
@@ -0,0 +1,78 @@
+# -*- 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 handle(self, *args, **options):
+
+        self.stdout = codecs.getwriter('utf8')(self.stdout)
+
+        if len(args) != 2:
+            self.stdout.write("dae <modele> <id>\n")
+            return
+
+        classname = args[0]
+        klass = getattr(dae, classname, None)
+        if klass is None:
+            self.stdout.write("Le modèle %s n'existe pas dans DAE.\n" % classname)
+
+        id = int(args[1])
+        try:
+            obj = klass.objects.get(id=id)
+        except:
+            self.stdout.write("L'id %s n'existe pas pour le modèle %s dans DAE.\n" % \
+                    (id, classname))
+            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
+
+        copy = duplicate(obj, skip=['id', 'dae_dossiers', ])
+        print copy.__class__
+        print copy.id
+        
index 1a065c9..eba318d 100644 (file)
@@ -60,7 +60,6 @@ class Poste(PosteWorkflow, rh.Poste_):
         return ImportPoste.objects.filter(dae=self).exists()
 
     def importer(self):
-        from django.db.models import AutoField
         if self.est_importe():
             return ImportPoste.objects.get(dae=self)
         rh_poste = rh.Poste()
index 7fbf0ac..ee41fc1 100644 (file)
@@ -730,6 +730,9 @@ class DossierComparaison_(models.Model, DevisableMixin):
     class Meta:
         abstract = True
 
+    def __unicode__(self):
+        return "%s (%s)" % (self.poste, self.personne)
+
 
 class DossierComparaison(DossierComparaison_):
     pass