Mise en page vue Projet : affichage du champ 'selectionne' dans le tree_view des...
[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     def _get_main_domain(self, cr, uid, ids, field_name=None, arg=None, context=None):
13         if type(ids) in (int, float):
14             ids = [ids]
15
16         projects = self.browse(cr, uid, ids, context=context)
17
18         result = {}
19
20         for project in projects:
21             for domain in project.domains:
22                 if domain.main:
23                     result[project.id] = domain.name
24                     break
25             else:
26                 result[project.id] = "Any domain is selected as main domain yet"
27
28         return result
29
30
31     def _get_main_moa(self, cr, uid, ids, field_name=None, arg=None, context=None):
32         if type(ids) in (int, float):
33             ids = [ids]
34
35         projects = self.browse(cr, uid, ids, context=context)
36
37         result = {}
38
39         for project in projects:
40             for moa in project.moa_ids:
41                 if moa.main:
42                     result[project.id] = moa.name
43                     break
44             else:
45                 result[project.id] = "Any MOA is selected as main MOA yet"
46
47         return result
48
49
50     def _get_main_moe(self, cr, uid, ids, field_name=None, arg=None, context=None):
51         if type(ids) in (int, float):
52             ids = [ids]
53
54         projects = self.browse(cr, uid, ids, context=context)
55
56         result = {}
57
58         for project in projects:
59             for moe in project.moe_ids:
60                 if moe.main:
61                     result[project.id] = moe.name
62                     break
63             else:
64                 result[project.id] = "Any MOE is selected as main MOE yet"
65
66         return result
67
68
69     _columns = {
70         "name": fields.char(string="Title", size=64, required=True),
71         "description": fields.text(string="Description"),
72         "domains": fields.one2many("cmmi.projet.domaine",
73                                     "project_id",
74                                     string="Domaines"),
75         "team_members": fields.many2many("res.partner",
76                                          "cmmi_projet_teammember_rel",
77                                          "projet_id",
78                                          "partner_id",
79                                          string="Team Members"),
80         "modules": fields.one2many("cmmi.description.module",
81                                    "projet_id",
82                                    string="Modules"),
83         "chantiers": fields.one2many("cmmi.axes.chantier",
84                                      "projet_id",
85                                      string="Chantiers"),
86         "paliers": fields.one2many("cmmi.axes.palier",
87                                    "projet_id",
88                                    string="Paliers"),
89         "phases": fields.one2many("cmmi.projet.phase",
90                                   "projet_id",
91                                   string="Phases"),
92         "evolutions": fields.one2many("cmmi.evolution",
93                                       "projet_id",
94                                       string="Evolutions"),
95         "moe_ids": fields.one2many("cmmi.projet.moe",
96                                    "project_id",
97                                    string="MOEs"),
98         "moa_ids": fields.one2many("cmmi.projet.moa",
99                                    "project_id",
100                                    string="MOAs"),
101         "moe_id": fields.function(_get_main_moe,
102                                   type="string",
103                                   string="MOE principale"),
104         "moa_id": fields.function(_get_main_moa,
105                                   type="string",
106                                   string="MOA principale"),
107         "main_domain": fields.function(_get_main_domain,
108                                        type="string",
109                                        string="Domaine principal"),
110     }
111
112
113     def create(self, cr, uid, vals, context=None):
114         project_id = osv.Model.create(self, cr, uid, vals, context=context)
115
116         # Récupération des ids de toutes les phases
117         phase_model = self.pool.get("cmmi.phase.type")
118         phases_ids = phase_model.search(cr, uid, [])
119
120         # Création des relations
121         projet_phase_model = self.pool.get("cmmi.projet.phase")
122         for phase_id in phases_ids:
123             projet_phase_model.create(
124                 cr,
125                 uid,
126                 {
127                     'projet_id': project_id,
128                     'phase_id': phase_id,
129                     'selectionne': False,
130                 }
131             )
132
133         return project_id
134
135
136     def action_add_domain(self, cr, uid, ids, context=None):
137         pass
138
139     def action_add_moe(self, cr, uid, ids, context=None):
140         pass
141
142     def action_add_moa(self, cr, uid, ids, context=None):
143         pass
144
145 class ProjetDomaine(osv.Model):
146
147     _name = "cmmi.projet.domaine"
148
149     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
150         if isinstance(ids, (int, long)):
151             ids = [ids]
152         #return {i: r.domaine_id.name for i, r in
153         #        zip(ids, self.browse(cr, uid, ids, context=context))}
154         return dict([(i, r.domaine_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,
161                                 string="Nom du domaine"),
162         "main": fields.boolean(string="Domaine principal ?"),
163         "project_id": fields.many2one("cmmi.projet",
164                                       string="Projet"),
165         "domaine_id": fields.many2one("cmmi.description.domaine",
166                                       string="Domaine"),
167     }
168
169     def onchange_main(self, cr, uid, ids, project, domaine, main, context=None):
170         if not main:
171             return {'value': {'main': True},
172                     'warning': {
173                         'title'   : "Integrity Warning",
174                         'message' : "One of the domains should be the main domain",
175                     }
176                 }
177         ids = self.search(
178             cr,
179             uid,
180             [
181                 ('project_id', '=', project),
182                 ('domaine_id', '!=', domaine),
183             ],
184             context=context,
185         )
186         current_id = self.search(
187             cr,
188             uid,
189             [
190                 ('project_id', '=', project),
191                 ('domaine_id', '=', domaine),
192             ],
193             context=context,
194         )
195         self.write(cr, uid, ids, {'main': False}, context=context)
196         self.write(cr, uid, current_id, {'main': True}, context=context)
197
198         return {'value': {'main': True}}
199
200
201
202 class ProjetMoe(osv.Model):
203     _name = "cmmi.projet.moe"
204
205     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
206         if isinstance(ids, (int, long)):
207             ids = [ids]
208         return dict([(i, r.moe_id.name) for i, r in
209                 zip(ids, self.browse(cr, uid, ids, context=context))])
210
211     _columns = {
212         "name": fields.function(_get_name,
213                                 type='char',
214                                 store=True, # Permet d'enregistrer le champ.
215                                 string="Nom de la MOE"),
216         "main": fields.boolean(string="MOE principale ?"),
217         "project_id": fields.many2one("cmmi.projet",
218                                       string="Projet",
219                                       required=True),
220         "moe_id": fields.many2one("cmmi.mo.moe",
221                                   string="MOE",
222                                   required=True),
223     }
224
225     def onchange_main(self, cr, uid, ids, project, moe, main, context=None):
226         if not main:
227             return {'value': {'main': True},
228                     'warning': {
229                         'title'   : "Integrity Warning",
230                         'message' : "Une des MOE doit être la MOE principale",
231                     }
232                 }
233         ids = self.search(
234             cr,
235             uid,
236             [
237                 ('project_id', '=', project),
238                 ('moe_id', '!=', moe),
239             ],
240             context=context,
241         )
242         current_id = self.search(
243             cr,
244             uid,
245             [
246                 ('project_id', '=', project),
247                 ('moe_id', '=', moe),
248             ],
249             context=context,
250         )
251         self.write(cr, uid, ids, {'main': False}, context=context)
252         self.write(cr, uid, current_id, {'main': True}, context=context)
253
254         return {'value': {'main': True}}
255
256
257 class ProjetMoa(osv.Model):
258     _name = "cmmi.projet.moa"
259
260     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
261         if isinstance(ids, (int, long)):
262             ids = [ids]
263         return dict([(i, r.moa_id.name) for i, r in
264                 zip(ids, self.browse(cr, uid, ids, context=context))])
265
266     _columns = {
267         "name": fields.function(_get_name,
268                                 type='char',
269                                 store=True,
270                                 string="Nom de la MOA"),
271         "main": fields.boolean(string="MOA principale ?"),
272         "project_id": fields.many2one("cmmi.projet",
273                                       string="Projet",
274                                       required=True),
275         "moa_id": fields.many2one("cmmi.mo.moa",
276                                   string="MOA",
277                                   required=True),
278     }
279
280     def onchange_main(self, cr, uid, ids, project, moa, main, context=None):
281         if not main:
282             return {'value': {'main': True},
283                     'warning': {
284                         'title'   : "Integrity Warning",
285                         'message' : "Une des MOA doit être la MOA principale",
286                     }
287                 }
288         ids = self.search(
289             cr,
290             uid,
291             [
292                 ('project_id', '=', project),
293                 ('moa_id', '!=', moa),
294             ],
295             context=context,
296         )
297         current_id = self.search(
298             cr,
299             uid,
300             [
301                 ('project_id', '=', project),
302                 ('moa_id', '=', moa),
303             ],
304             context=context,
305         )
306         self.write(cr, uid, ids, {'main': False}, context=context)
307         self.write(cr, uid, current_id, {'main': True}, context=context)
308
309         return {'value': {'main': True}}
310
311
312 class ProjetPhase(osv.Model):
313     _name = "cmmi.projet.phase"
314
315     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
316         if isinstance(ids, (int, long)):
317             ids = [ids]
318         return dict([(i, r.phase_id.name) for i, r in
319                 zip(ids, self.browse(cr, uid, ids, context=context))])
320
321     _columns = {
322         "name": fields.function(_get_name,
323                                 type='char',
324                                 store=True,
325                                 string="Nom de la phase"),
326         "selectionne": fields.boolean(string="Phase sélectionnée ?"),
327         "projet_id": fields.many2one("cmmi.projet",
328                                      string="Projet",
329                                      required=True),
330         "phase_id": fields.many2one("cmmi.phase.type",
331                                     string="Phase",
332                                     required=True),
333     }