Modification de la relation projet/phase en many2many : création d'une table de relation
[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 action_add_domain(self, cr, uid, ids, context=None):
60         pass
61
62
63 class ProjetDomaine(osv.Model):
64
65     _name = "cmmi.projet.domaine"
66
67     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
68         if isinstance(ids, (int, long)):
69             ids = [ids]
70         #return {i: r.domaine_id.name for i, r in
71         #        zip(ids, self.browse(cr, uid, ids, context=context))}
72         return dict([(i, r.domaine_id.name) for i, r in
73                 zip(ids, self.browse(cr, uid, ids, context=context))])
74
75     _columns = {
76         "name": fields.function(_get_name,
77                                 type='char',
78                                 store=True,
79                                 string="Nom du domaine"),
80         "main": fields.boolean(string="Domaine principal ?"),
81         "project_id": fields.many2one("cmmi.projet",
82                                       string="Projet"),
83         "domaine_id": fields.many2one("cmmi.description.domaine",
84                                       string="Domaine"),
85     }
86
87     def onchange_main(self, cr, uid, ids, project, domaine, main, context=None):
88         if not main:
89             return {'value': {'main': True},
90                     'warning': {
91                         'title'   : "Integrity Warning",
92                         'message' : "One of the domains should be the main domain",
93                     }
94                 }
95         ids = self.search(
96             cr,
97             uid,
98             [
99                 ('project_id', '=', project),
100                 ('domaine_id', '!=', domaine),
101             ],
102             context=context,
103         )
104         current_id = self.search(
105             cr,
106             uid,
107             [
108                 ('project_id', '=', project),
109                 ('domaine_id', '=', domaine),
110             ],
111             context=context,
112         )
113         self.write(cr, uid, ids, {'main': False}, context=context)
114         self.write(cr, uid, current_id, {'main': True}, context=context)
115
116         return {'value': {'main': True}}
117
118
119
120 class ProjetMoe(osv.Model):
121     _name = "cmmi.projet.moe"
122
123     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
124         if isinstance(ids, (int, long)):
125             ids = [ids]
126         return dict([(i, r.moe_id.name) for i, r in
127                 zip(ids, self.browse(cr, uid, ids, context=context))])
128
129     _columns = {
130         "name": fields.function(_get_name,
131                                 type='char',
132                                 store=True, # Permet d'enregistrer le champ.
133                                 string="Nom de la MOE"),
134         "main": fields.boolean(string="MOE principale ?"),
135         "project_id": fields.many2one("cmmi.projet",
136                                       string="Projet"),
137         "moe_id": fields.many2one("cmmi.mo.moe",
138                                       string="MOE"),
139     }
140
141
142 class ProjetMoa(osv.Model):
143     _name = "cmmi.projet.moe"
144
145     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
146         if isinstance(ids, (int, long)):
147             ids = [ids]
148         return dict([(i, r.moa_id.name) for i, r in
149                 zip(ids, self.browse(cr, uid, ids, context=context))])
150
151     _columns = {
152         "name": fields.function(_get_name,
153                                 type='char',
154                                 store=True,
155                                 string="Nom de la MOA"),
156         "main": fields.boolean(string="MOA principale ?"),
157         "project_id": fields.many2one("cmmi.projet",
158                                       string="Projet"),
159         "moa_id": fields.many2one("cmmi.mo.moa",
160                                       string="MOA"),
161     }
162
163
164 class ProjetPhase(osv.Model):
165     _name = "cmmi.projet.phase"
166
167     def _get_name(self, cr, uid, ids, field_name=None, arg=None, context=None):
168         if isinstance(ids, (int, long)):
169             ids = [ids]
170         return dict([(i, r.phase_id.name) for i, r in
171                 zip(ids, self.browse(cr, uid, ids, context=context))])
172
173     _columns = {
174         "name": fields.function(_get_name,
175                                 type='char',
176                                 store=True,
177                                 string="Nom de la phase"),
178         "selectionne": fields.boolean(string="Phase sélectionnée ?"),
179         "projet_id": fields.many2one("cmmi.projet",
180                                      string="Projet"),
181         "phase_id": fields.many2one("cmmi.phase",
182                                     string="Phase"),
183     }