From: Nicolas Cadou Date: Fri, 13 May 2011 20:25:07 +0000 (-0400) Subject: ajouté l'initialisation d'une simulation X-Git-Tag: DAE+RH~315^2 X-Git-Url: http://git.auf.org/?p=auf_rh_dae.git;a=commitdiff_plain;h=180a4d25bf712d425f4ec4e60c2ef938e85c5b4a ajouté l'initialisation d'une simulation --- 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__