Wizard pour l'ajout de phases à un palier - TERMINÉ
[OpenERP/cmmi.git] / wizards / palierPhase.py
1 #-*- coding: utf8 -*-
2
3 from openerp.osv import osv, fields
4
5 class PalierPhaseWizard(osv.TransientModel):
6
7     _name = "cmmi.palier.phase.wizard"
8
9
10     def default_get(self, cr, uid, fields, context=None):
11         result = osv.TransientModel.default_get(self, cr, uid, fields, context=context)
12         palier_id = context.get('palier_id', False)
13         if palier_id:
14             result['palier_id'] = palier_id
15         return result
16
17
18     def _phases_selection(self, cr, uid, context=None):
19         phase_model = self.pool.get("cmmi.projet.phase")
20         palier_phase_model = self.pool.get("cmmi.axes.palier.phase")
21
22         if context is None or not context.has_key("palier_id"):
23             return []
24
25         # on recherche des palier-phases
26         links_ids = palier_phase_model.search(
27             cr,
28             uid,
29             [('palier_id', '=', context["palier_id"])],
30             context=context,
31         )
32
33         # on recherche phases déjà présentes dans le palier
34         excluded_ids = list(set([p['phase_id'][0] for p in palier_phase_model.read(
35             cr,
36             uid,
37             links_ids,
38             fields=["phase_id"],
39             context=context
40         )]))
41
42         # on exclu les phases déjà présentes dans le palier
43         phases_ids = phase_model.search(
44             cr,
45             uid,
46             [('id', 'not in', excluded_ids), ('selectionne', '=', True)],
47             context=context,
48         )
49
50         print [(p["id"], p["name"]) for p in phase_model.read(
51             cr,
52             uid,
53             phases_ids,
54             fields=["id", "name"],
55             context=context
56         )]
57         return [(p["id"], p["name"]) for p in phase_model.read(
58             cr,
59             uid,
60             phases_ids,
61             fields=["id", "name"],
62             context=context
63         )]
64
65
66     def action_add_phase_to_palier(self, cr, uid, ids, context=None):
67         palier_phase_model = self.pool.get("cmmi.axes.palier.phase")
68
69         id = ids[0]
70
71         result = self.read(cr, uid, id, context=context)
72
73         palier_phase_model.create (
74             cr,
75             uid,
76             {"palier_id": result["palier_id"][0],"phase_id": result["phase_id"]},
77             context=context,
78         )
79
80         return {'type': 'ir.actions.act_window_close'}
81
82
83     _columns = {
84         "palier_id": fields.many2one("cmmi.axes.palier",
85                                      string="Palier",
86                                      required=True),
87 #        "phase_id": fields.selection("cmmi.projet.phase",
88 #                                     string="Phase",
89 #                                     required=True,
90 #                                     domain=[("selectionne", "=", True)]),
91         "phase_id": fields.selection(_phases_selection,
92                                     string="Phase",
93                                     required=True),
94     }