Correction du bug avec les wizards
[OpenERP/cmmi.git] / wizards / moa.py
1 #-*- coding: utf8 -*-
2
3 from openerp.osv import osv, fields
4
5 class ProjetMoaWizard(osv.TransientModel):
6
7     _name = "cmmi.projet.moa.wizard"
8
9     def default_get(self, cr, uid, fields, context=None):
10         ret = osv.TransientModel.default_get(self, cr, uid, fields, context=context)
11         project_id = context.get('project_id', False)
12         if project_id:
13             ret['project_id'] = project_id
14         return ret
15
16     def _moas_selection(self, cr, uid, context=None):
17         model_base = self.pool.get("cmmi.mo.moa")
18         model_proj = self.pool.get("cmmi.projet.moa")
19
20         if context is None or not context.has_key("project_id"):
21             return []
22
23         # recherche des lien entre le projet et les moas
24         links_ids = model_proj.search(
25             cr,
26             uid,
27             [('project_id', '=', context["project_id"])],
28             context=context,
29         )
30
31         # récupérations des moas déjà sélectionnés pour le projet
32         excluded_ids = list(set([r['moa_id'][0] for r in model_proj.read(
33             cr,
34             uid,
35             links_ids,
36             fields=["moa_id"],
37             context=context
38         )]))
39
40         # recherche des moas autres que ceux déjà sélectionnés
41         moa_ids = model_base.search(
42             cr,
43             uid,
44             [('id', 'not in', excluded_ids)],
45             context=context,
46         )
47
48         # renvoi des 2 uplets (id, name)
49         print [(r["id"], r["name"]) for r in model_base.read(
50             cr,
51             uid,
52             moa_ids,
53             fields=["id", "name"],
54             context=context
55         )]
56         return [(r["id"], r["name"]) for r in model_base.read(
57             cr,
58             uid,
59             moa_ids,
60             fields=["id", "name"],
61             context=context
62         )]
63
64
65     def action_add_moa_to_project(self, cr, uid, ids, context=None):
66         # Récupération du modèle utile pour écrire les données
67         model = self.pool.get("cmmi.projet.moa")
68
69         # Un wizard, donc un seul identifiant
70         id = ids[0]
71
72         # Récupération des informations mises dans l'assistant
73         result = self.read(cr, uid, id, context=context)
74
75         # Si on a coché principal, on vire principal des autres moas
76         if result["main"]:
77             model.write(
78                 cr,
79                 uid,
80                 model.search(
81                     cr,
82                     uid,
83                     [('project_id', '=', result["project_id"][0])],
84                     context=context,
85                 ),
86                 {'main': False},
87                 context=context,
88             )
89
90         # Création de la donnée à partir de la donnée du magicien
91         model.create(cr, uid, {
92             "main": result["main"],
93             "project_id": result["project_id"][0],
94             "moa_id": result["moa_id"],
95             }, context=context)
96
97         # Fermer simplement la fenêtre
98         return {'type': 'ir.actions.act_window_close'}
99
100
101         #-----------------------------------------------------------------------
102         # # Renvoi vers la vue du modèle
103         # return {
104         #    "type": 'ir.actions.act_window',
105         #    "res_model": "cmmi.projet",
106         #    'view_type': 'form',
107         #    'view_mode': 'form',
108         #    'res_id': result["project_id"][0],
109         #    'context': context,
110         # }
111         #-----------------------------------------------------------------------
112
113     _columns = {
114         "main": fields.boolean(string="MOA principale ?"),
115         "project_id": fields.many2one("cmmi.projet",
116                                       string="Projet",
117                                       required=True),
118         "moa_id": fields.selection(_moas_selection,
119                                    string="moa",
120                                    required=True),
121     }