Ajout de création de relations projet/phase lors de la création d'un projet
[OpenERP/cmmi.git] / projet.py
1 #-*- coding: utf8 -*-
2 '''
3 '''
4
5 from openerp.osv import osv, fields
6
7
8 class Projet(osv.Model):
9
10     _name = "cmmi.projet"
11
12     _domains = {
13         'moe': [('role_mo_id.type_mo', '=', "MOE")],
14         'moa': [('role_mo_id.type_mo', '=', "MOA")],
15     }
16
17     _columns = {
18         "name": fields.char(string="Title", size=64, required=True),
19         "description": fields.text(string="Description"),
20         "domaines": fields.one2many("cmmi.projet.domaine",
21                                     "project_id",
22                                     string="Domaines"),
23         "structures": fields.many2many("cmmi.mo.structure",
24                                        string="Structures"),
25         "structures_moe": fields.many2many("cmmi.mo.structure",
26                                            string="Structures",
27                                            domain=_domains['moe']),
28         "structures_moa": fields.many2many("cmmi.mo.structure",
29                                            string="Structures",
30                                            domain=_domains['moa']),
31         "team_members": fields.many2many("cmmi.partner.teammember",
32                                          "cmmi_projet_teammember_rel",
33                                          "projet_id",
34                                          "partner_id",
35                                          string="Team Members"),
36         "modules": fields.one2many("cmmi.description.module",
37                                    "projet_id",
38                                    string="Modules"),
39         "chantiers": fields.one2many("cmmi.axes.chantier",
40                                      "projet_id",
41                                      string="Chantiers"),
42         "paliers": fields.one2many("cmmi.axes.palier",
43                                    "projet_id",
44                                    string="Paliers"),
45         "phases": fields.one2many("cmmi.projet.phase",
46                                   "projet_id",
47                                   string="Phases"),
48         "evolutions": fields.one2many("cmmi.evolution",
49                                       "projet_id",
50                                       string="Evolutions"),
51         "moe_id": fields.many2one("cmmi.mo.moe", string="MoE", required=True),
52         "moa_id": fields.many2one("cmmi.mo.moa", string="MoA", required=True),
53         "main_domain": fields.many2one("cmmi.description.domaine",
54                                        string="Domaine principal"),
55         "main_structure": fields.many2one("cmmi.mo.structure",
56                                           string="Structure principale"),
57     }
58
59     def create(self, cr, uid, vals, context=None):
60         project_id = osv.Model.create(self, cr, uid, vals, context=context)
61
62         # Récupération des ids de toutes les phases
63         phase_model = self.pool.get("cmmi.phase")
64         phases_ids = phase_model.search(cr, uid, [])
65
66         # Création des relations
67         projet_phase_model = self.pool.get("cmmi.projet.phase")
68         for phase_id in phases_ids:
69             projet_phase_model.create(
70                 cr,
71                 uid,
72                 {
73                     'projet_id': project_id,
74                     'phase_id': phase_id,
75                     'selectionne': False,
76                 }
77             )
78
79         return project_id
80
81
82     def action_add_domain(self, cr, uid, ids, context=None):
83         pass
84
85     def action_add_moe(self, cr, uid, ids, context=None):
86         pass
87
88     def action_add_moa(self, cr, uid, ids, context=None):
89         pass
90
91 class ProjetDomaine(osv.Model):
92
93     _name = "cmmi.projet.domaine"
94
95     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
96         if isinstance(ids, (int, long)):
97             ids = [ids]
98         #return {i: r.domaine_id.name for i, r in
99         #        zip(ids, self.browse(cr, uid, ids, context=context))}
100         return dict([(i, r.domaine_id.name) for i, r in
101                 zip(ids, self.browse(cr, uid, ids, context=context))])
102
103     _columns = {
104         "name": fields.function(_get_name,
105                                 type='char',
106                                 store=True,
107                                 string="Nom du domaine"),
108         "main": fields.boolean(string="Domaine principal ?"),
109         "project_id": fields.many2one("cmmi.projet",
110                                       string="Projet"),
111         "domaine_id": fields.many2one("cmmi.description.domaine",
112                                       string="Domaine"),
113     }
114
115     def onchange_main(self, cr, uid, ids, project, domaine, main, context=None):
116         if not main:
117             return {'value': {'main': True},
118                     'warning': {
119                         'title'   : "Integrity Warning",
120                         'message' : "One of the domains should be the main domain",
121                     }
122                 }
123         ids = self.search(
124             cr,
125             uid,
126             [
127                 ('project_id', '=', project),
128                 ('domaine_id', '!=', domaine),
129             ],
130             context=context,
131         )
132         current_id = self.search(
133             cr,
134             uid,
135             [
136                 ('project_id', '=', project),
137                 ('domaine_id', '=', domaine),
138             ],
139             context=context,
140         )
141         self.write(cr, uid, ids, {'main': False}, context=context)
142         self.write(cr, uid, current_id, {'main': True}, context=context)
143
144         return {'value': {'main': True}}
145
146
147
148 class ProjetMoe(osv.Model):
149     _name = "cmmi.projet.moe"
150
151     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
152         if isinstance(ids, (int, long)):
153             ids = [ids]
154         return dict([(i, r.moe_id.name) for i, r in
155                 zip(ids, self.browse(cr, uid, ids, context=context))])
156
157     _columns = {
158         "name": fields.function(_get_name,
159                                 type='char',
160                                 store=True, # Permet d'enregistrer le champ.
161                                 string="Nom de la MOE"),
162         "main": fields.boolean(string="MOE principale ?"),
163         "project_id": fields.many2one("cmmi.projet",
164                                       string="Projet",
165                                       required=True),
166         "moe_id": fields.many2one("cmmi.mo.moe",
167                                   string="MOE",
168                                   required=True),
169     }
170
171
172 class ProjetMoa(osv.Model):
173     _name = "cmmi.projet.moe"
174
175     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
176         if isinstance(ids, (int, long)):
177             ids = [ids]
178         return dict([(i, r.moa_id.name) for i, r in
179                 zip(ids, self.browse(cr, uid, ids, context=context))])
180
181     _columns = {
182         "name": fields.function(_get_name,
183                                 type='char',
184                                 store=True,
185                                 string="Nom de la MOA"),
186         "main": fields.boolean(string="MOA principale ?"),
187         "project_id": fields.many2one("cmmi.projet",
188                                       string="Projet",
189                                       required=True),
190         "moa_id": fields.many2one("cmmi.mo.moa",
191                                   string="MOA",
192                                   required=True),
193     }
194
195
196 class ProjetPhase(osv.Model):
197     _name = "cmmi.projet.phase"
198
199     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
200         if isinstance(ids, (int, long)):
201             ids = [ids]
202         return dict([(i, r.phase_id.name) for i, r in
203                 zip(ids, self.browse(cr, uid, ids, context=context))])
204
205     _columns = {
206         "name": fields.function(_get_name,
207                                 type='char',
208                                 store=True,
209                                 string="Nom de la phase"),
210         "selectionne": fields.boolean(string="Phase sélectionnée ?"),
211         "projet_id": fields.many2one("cmmi.projet",
212                                      string="Projet",
213                                      required=True),
214         "phase_id": fields.many2one("cmmi.phase",
215                                     string="Phase",
216                                     required=True),
217     }