Commit | Line | Data |
---|---|---|
3fde9fe8 NC |
1 | # -=- encoding: utf-8 -=- |
2 | ||
3 | from django.db import models | |
4 | ||
5 | from rh import models as rh | |
6 | ||
7 | ||
8 | class Simulation(models.Model): | |
9 | nom = models.CharField(max_length=255, | |
10 | verbose_name=u'Nom de la simulation') | |
11 | ||
0c248d0a NC |
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) | |
20 | ||
21 | ||
22 | def initialize(self): | |
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.') | |
27 | ||
28 | postes = {} | |
29 | for rh_p in rh.Poste.objects.all(): | |
30 | sim_p = self.copy_rh_model(rh_p) | |
31 | sim_p.save() | |
32 | postes[rh_p.id] = sim_p | |
33 | ||
34 | dossiers = {} | |
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] | |
42 | sim_d.save() | |
43 | dossiers[rh_d.id] = sim_d | |
3fde9fe8 NC |
44 | |
45 | class Poste(rh.Poste_): | |
46 | __doc__ = rh.Poste_.__doc__ | |
47 | simulation = models.ForeignKey(Simulation) | |
48 | ||
49 | ||
50 | class Dossier(rh.Dossier_): | |
51 | __doc__ = rh.Dossier_.__doc__ | |
52 | simulation = models.ForeignKey(Simulation) |