From 0c248d0a16d00bfdd99103ed86fd739d9a53a880 Mon Sep 17 00:00:00 2001 From: Nicolas Cadou Date: Fri, 13 May 2011 16:25:07 -0400 Subject: [PATCH] =?utf8?q?ajout=C3=A9=20l'initialisation=20d'une=20simulatio?= =?utf8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- project/budget/models.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/project/budget/models.py b/project/budget/models.py index 47dbbda..67d8371 100755 --- a/project/budget/models.py +++ b/project/budget/models.py @@ -9,6 +9,38 @@ class Simulation(models.Model): nom = models.CharField(max_length=255, verbose_name=u'Nom de la simulation') + def copy_rh_model(self, obj): + """ Crée une copie de simulation d'un modèle rh. """ + initial = dict(['simulation', self] + + [(f.name, getattr(obj, f.name)) + for f in obj._meta.fields + if not isinstance(f, models.AutoField) and + not f in obj._meta.parents.values()]) + return globals()[obj.__class__.__name__](**initial) + + + def initialize(self): + """ Copie les données nécessaires à une simulation. """ + if (self.poste_set.all().count() > 0 or + self.dossier_set.all().count() > 0): + raise ValueError(u'Cette simulation a déjà été initialisée.') + + postes = {} + for rh_p in rh.Poste.objects.all(): + sim_p = self.copy_rh_model(rh_p) + sim_p.save() + postes[rh_p.id] = sim_p + + dossiers = {} + for rh_d in rh.Dossier.objects.all(): + sim_d = self.copy_rh_model(rh_d) + if not sim_d.poste.id in postes: + self.poste_set.all().delete() + self.dossier_set.all().delete() + raise ValueError(u'Données inconsistantes.') + sim_d.poste = postes[sim_d.poste.id] + sim_d.save() + dossiers[rh_d.id] = sim_d class Poste(rh.Poste_): __doc__ = rh.Poste_.__doc__ -- 1.7.10.4