1 # -=- encoding: utf-8 -=-
3 from django
.db
import models
5 from rh
import models
as rh
8 class Simulation(models
.Model
):
9 nom
= models
.CharField(max_length
=255,
10 verbose_name
=u
'Nom de la simulation')
12 def copy_rh_model(self
, obj
):
13 """ Crée une copie de simulation d'un modèle rh. """
14 initial
= dict(['simulation', self
] +
15 [(f
.name
, getattr(obj
, f
.name
))
16 for f
in obj
._meta
.fields
17 if not isinstance(f
, models
.AutoField
) and
18 not f
in obj
._meta
.parents
.values()])
19 return globals()[obj
.__class__
.__name__
](**initial
)
23 """ Copie les données nécessaires à une simulation. """
24 if (self
.poste_set
.all().count() > 0 or
25 self
.dossier_set
.all().count() > 0):
26 raise ValueError(u
'Cette simulation a déjà été initialisée.')
29 for rh_p
in rh
.Poste
.objects
.all():
30 sim_p
= self
.copy_rh_model(rh_p
)
32 postes
[rh_p
.id] = sim_p
35 for rh_d
in rh
.Dossier
.objects
.all():
36 sim_d
= self
.copy_rh_model(rh_d
)
37 if not sim_d
.poste
.id in postes
:
38 self
.poste_set
.all().delete()
39 self
.dossier_set
.all().delete()
40 raise ValueError(u
'Données inconsistantes.')
41 sim_d
.poste
= postes
[sim_d
.poste
.id]
43 dossiers
[rh_d
.id] = sim_d
45 class Poste(rh
.Poste_
):
46 __doc__
= rh
.Poste_
.__doc__
47 simulation
= models
.ForeignKey(Simulation
)
50 class Dossier(rh
.Dossier_
):
51 __doc__
= rh
.Dossier_
.__doc__
52 simulation
= models
.ForeignKey(Simulation
)