Utilisation de fields.related pour l'affectation du name de la class ProjetPhase
[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     _description = "Table des projets."
13
14     def _get_main_domain(self, cr, uid, ids, field_name=None, arg=None, context=None):
15         if type(ids) in (int, float):
16             ids = [ids]
17
18         projects = self.browse(cr, uid, ids, context=context)
19
20         result = {}
21
22         for project in projects:
23             for domain in project.domains:
24                 if domain.main:
25                     result[project.id] = domain.name
26                     break
27             else:
28                 result[project.id] = "Any domain is selected as main domain yet"
29
30         return result
31
32
33     def _get_main_moa(self, cr, uid, ids, field_name=None, arg=None, context=None):
34         if type(ids) in (int, float):
35             ids = [ids]
36
37         projects = self.browse(cr, uid, ids, context=context)
38
39         result = {}
40
41         for project in projects:
42             for moa in project.moa_ids:
43                 if moa.main:
44                     result[project.id] = moa.name
45                     break
46             else:
47                 result[project.id] = "Any MOA is selected as main MOA yet"
48
49         return result
50
51
52     def _get_main_moe(self, cr, uid, ids, field_name=None, arg=None, context=None):
53         if type(ids) in (int, float):
54             ids = [ids]
55
56         projects = self.browse(cr, uid, ids, context=context)
57
58         result = {}
59
60         for project in projects:
61             for moe in project.moe_ids:
62                 if moe.main:
63                     result[project.id] = moe.name
64                     break
65             else:
66                 result[project.id] = "Any MOE is selected as main MOE yet"
67         return result
68
69
70     def _get_phases_selectionnees(self, cr, uid, ids, field_name=None, arg=None, context=None):
71         result = {}
72         for projet in self.browse(cr, uid, ids, context=context):
73             res = []
74             for phase in projet.phases:
75                 #import pdb; pdb.set_trace()
76                 if phase.selectionne:
77                     res.append(phase.id)
78             result[projet.id] = res
79         return result
80
81
82     _columns = {
83         "name": fields.char(string="Title", size=64, required=True),
84         "description": fields.text(string="Description"),
85         "use_chantier": fields.boolean(string="Utilisation de la notion de chantier ?"),
86         "use_palier": fields.boolean(string="Utilisation de la notion de palier ?"),
87
88         # Backrefs
89         "domains": fields.one2many("cmmi.projet.domaine",
90                                     "project_id",
91                                     string="Domaines"),
92         "moe_ids": fields.one2many("cmmi.projet.moe",
93                                    "project_id",
94                                    string="MOEs"),
95         "moa_ids": fields.one2many("cmmi.projet.moa",
96                                    "project_id",
97                                    string="MOAs"),
98         "team_members": fields.one2many("cmmi.projet.teammember",
99                                          "projet_id",
100                                          string="Team Members"),
101         "modules": fields.one2many("cmmi.description.module",
102                                    "projet_id",
103                                    string="Modules"),
104         "chantiers": fields.one2many("cmmi.axes.chantier",
105                                      "projet_id",
106                                      string="Chantiers"),
107         "paliers": fields.one2many("cmmi.axes.palier",
108                                    "projet_id",
109                                    string="Paliers"),
110         "phases": fields.one2many("cmmi.projet.phase",
111                                   "projet_id",
112                                   string="Phases"),
113         "evolutions": fields.one2many("cmmi.evolution",
114                                       "projet_id",
115                                       string="Evolutions"),
116
117         # Champs fonction rapatriant les mo ou domaine principaux
118         "moe_id": fields.function(_get_main_moe,
119                                   type="string",
120                                   string="MOE principale"),
121         "moa_id": fields.function(_get_main_moa,
122                                   type="string",
123                                   string="MOA principale"),
124         "main_domain": fields.function(_get_main_domain,
125                                        type="string",
126                                        string="Domaine principal"),
127         "phases_selectionnees": fields.function(_get_phases_selectionnees,
128                                                 type="one2many",
129                                                 obj="cmmi.projet.phase",
130                                                 string="Phases sélectionnées"),
131     }
132
133     _defaults = {
134         "use_chantier": True,
135         "use_palier": True,
136     }
137
138
139     def create(self, cr, uid, vals, context=None):
140         project_id = osv.Model.create(self, cr, uid, vals, context=context)
141
142         # Récupération des ids de toutes les phases
143         phase_model = self.pool.get("cmmi.phase")
144         phases_ids = phase_model.search(cr, uid, [])
145
146         # Création des relations
147         projet_phase_model = self.pool.get("cmmi.projet.phase")
148         for phase_id in phases_ids:
149             projet_phase_model.create(
150                 cr,
151                 uid,
152                 {
153                     'projet_id': project_id,
154                     'phase_id': phase_id,
155                     'selectionne': False,
156                 }
157             )
158
159         return project_id
160
161
162     def action_add_domain(self, cr, uid, ids, context=None):
163         pass
164
165     def action_add_moe(self, cr, uid, ids, context=None):
166         pass
167
168     def action_add_moa(self, cr, uid, ids, context=None):
169         pass
170
171
172
173 class ProjetDomaine(osv.Model):
174
175     _name = "cmmi.projet.domaine"
176
177     _description = "Rattachement des domaines a un projet."
178
179     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
180         if isinstance(ids, (int, long)):
181             ids = [ids]
182         #return {i: r.domaine_id.name for i, r in
183         #        zip(ids, self.browse(cr, uid, ids, context=context))}
184         return dict([(i, r.domaine_id.name) for i, r in
185                 zip(ids, self.browse(cr, uid, ids, context=context))])
186
187     _columns = {
188         "name": fields.function(_get_name,
189                                 type='char',
190                                 store=True,
191                                 string="Nom du domaine"),
192         "main": fields.boolean(string="Domaine principal ?"),
193         "project_id": fields.many2one("cmmi.projet",
194                                       string="Projet"),
195         "domaine_id": fields.many2one("cmmi.description.domaine",
196                                       string="Domaine"),
197     }
198
199     def onchange_main(self, cr, uid, ids, project, domaine, main, context=None):
200         if not main:
201             return {'value': {'main': True},
202                     'warning': {
203                         'title'   : "Integrity Warning",
204                         'message' : "One of the domains should be the main domain",
205                     }
206                 }
207         ids = self.search(
208             cr,
209             uid,
210             [
211                 ('project_id', '=', project),
212                 ('domaine_id', '!=', domaine),
213             ],
214             context=context,
215         )
216         current_id = self.search(
217             cr,
218             uid,
219             [
220                 ('project_id', '=', project),
221                 ('domaine_id', '=', domaine),
222             ],
223             context=context,
224         )
225         self.write(cr, uid, ids, {'main': False}, context=context)
226         self.write(cr, uid, current_id, {'main': True}, context=context)
227
228         return {'value': {'main': True}}
229
230
231
232 class ProjetMoe(osv.Model):
233     _name = "cmmi.projet.moe"
234
235     _description = "Rattachement des MOEs au projet."
236
237     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
238         if isinstance(ids, (int, long)):
239             ids = [ids]
240         return dict([(i, r.moe_id.name) for i, r in
241                 zip(ids, self.browse(cr, uid, ids, context=context))])
242
243     _columns = {
244         "name": fields.function(_get_name,
245                                 type='char',
246                                 store=True, # Permet d'enregistrer le champ.
247                                 string="Nom de la MOE"),
248         "main": fields.boolean(string="MOE principale ?"),
249         "project_id": fields.many2one("cmmi.projet",
250                                       string="Projet",
251                                       required=True),
252         "moe_id": fields.many2one("cmmi.mo.moe",
253                                   string="MOE",
254                                   required=True),
255     }
256
257     def onchange_main(self, cr, uid, ids, project, moe, main, context=None):
258         if not main:
259             return {'value': {'main': True},
260                     'warning': {
261                         'title'   : "Integrity Warning",
262                         'message' : "Une des MOE doit être la MOE principale",
263                     }
264                 }
265         ids = self.search(
266             cr,
267             uid,
268             [
269                 ('project_id', '=', project),
270                 ('moe_id', '!=', moe),
271             ],
272             context=context,
273         )
274         current_id = self.search(
275             cr,
276             uid,
277             [
278                 ('project_id', '=', project),
279                 ('moe_id', '=', moe),
280             ],
281             context=context,
282         )
283         self.write(cr, uid, ids, {'main': False}, context=context)
284         self.write(cr, uid, current_id, {'main': True}, context=context)
285
286         return {'value': {'main': True}}
287
288
289
290 class ProjetMoa(osv.Model):
291     _name = "cmmi.projet.moa"
292
293     _description = "Rattachement des MOAs a une projet."
294
295     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
296         if isinstance(ids, (int, long)):
297             ids = [ids]
298         return dict([(i, r.moa_id.name) for i, r in
299                 zip(ids, self.browse(cr, uid, ids, context=context))])
300
301     _columns = {
302         "name": fields.function(_get_name,
303                                 type='char',
304                                 store=True,
305                                 string="Nom de la MOA"),
306         "main": fields.boolean(string="MOA principale ?"),
307         "project_id": fields.many2one("cmmi.projet",
308                                       string="Projet",
309                                       required=True),
310         "moa_id": fields.many2one("cmmi.mo.moa",
311                                   string="MOA",
312                                   required=True),
313     }
314
315     def onchange_main(self, cr, uid, ids, project, moa, main, context=None):
316         if not main:
317             return {'value': {'main': True},
318                     'warning': {
319                         'title'   : "Integrity Warning",
320                         'message' : "Une des MOA doit être la MOA principale",
321                     }
322                 }
323         ids = self.search(
324             cr,
325             uid,
326             [
327                 ('project_id', '=', project),
328                 ('moa_id', '!=', moa),
329             ],
330             context=context,
331         )
332         current_id = self.search(
333             cr,
334             uid,
335             [
336                 ('project_id', '=', project),
337                 ('moa_id', '=', moa),
338             ],
339             context=context,
340         )
341         self.write(cr, uid, ids, {'main': False}, context=context)
342         self.write(cr, uid, current_id, {'main': True}, context=context)
343
344         return {'value': {'main': True}}
345
346
347
348 class ProjetPhase(osv.Model):
349     _name = "cmmi.projet.phase"
350
351     _description = "Rattachement des Phases aux projets"
352
353     _columns = {
354         "name": fields.related("phase_id",
355                                 "name",
356                                 read_only=True,
357                                 type="char",
358                                 relation="cmmi.phase",
359                                 string="Nom de la phase"),
360         "phase_id": fields.many2one("cmmi.phase",
361                                     string="Phase",
362                                     required=True),
363         "projet_id": fields.many2one("cmmi.projet",
364                                      string="Projet",
365                                      required=True),
366         "selectionne": fields.boolean(string="Phase sélectionnée ?"),
367     }
368
369
370
371 class ProjetTeammember(osv.Model):
372     _name = "cmmi.projet.teammember"
373
374     _description = "Rattachement des Team members a un projet."
375
376     def _get_partner_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
377         if isinstance(ids, (int, long)):
378             ids = [ids]
379         return dict([(i, r.partner_id.name) for i, r in
380                 zip(ids, self.browse(cr, uid, ids, context=context))])
381
382     def _get_project_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
383         if isinstance(ids, (int, long)):
384             ids = [ids]
385         return dict([(i, r.projet_id.name) for i, r in
386                 zip(ids, self.browse(cr, uid, ids, context=context))])
387
388     _columns = {
389         "name": fields.function(_get_partner_name,
390                                 type='char',
391                                 store=True,
392                                 string="Nom du partner"),
393         "projet_name": fields.function(_get_project_name,
394                                        type='char',
395                                        store=True,
396                                        string="Nom du projet"),
397         "affecte": fields.integer(string="Affecté à"),
398         "depuis": fields.date(string="Depuis"),
399         "jusqua": fields.date(string="Jusqu'à"),
400         "projet_id": fields.many2one("cmmi.projet",
401                                      string="Projet",
402                                      required=True),
403         "partner_id": fields.many2one("res.partner",
404                                     string="Team Member",
405                                     required=True),
406     }