string="Structure principale"),
}
+ def action_add_domain(self, cr, uid, ids, context=None):
+ pass
class ProjetDomaine(osv.Model):
"domaine_id": fields.many2one("cmmi.description.domaine",
string="Domaine"),
}
+
+ def onchange_main(self, cr, uid, ids, project, domaine, main, context=None):
+ if not main:
+ return {'value': {'main': True},
+ 'warning': {
+ 'title' : "Integrity Warning",
+ 'message' : "One of the domains should be the main domain",
+ }
+ }
+ ids = self.search(
+ cr,
+ uid,
+ [
+ ('project_id', '=', project),
+ ('domaine_id', '!=', domaine),
+ ],
+ context=context,
+ )
+ current_id = self.search(
+ cr,
+ uid,
+ [
+ ('project_id', '=', project),
+ ('domaine_id', '=', domaine),
+ ],
+ context=context,
+ )
+ self.write(cr, uid, ids, {'main': False}, context=context)
+ self.write(cr, uid, current_id, {'main': True}, context=context)
+
+ return {'value': {'main': True}}
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<openerp>
+ <data>
+
+
+ <!-- View -->
+ <record model="ir.ui.view" id="wizard_add_domain_to_project_form">
+ <field name="name">wizard.project.domain.add</field>
+ <field name="model">cmmi.projet.domain.wizard</field>
+ <field name="arch" type="xml">
+ <form string="Add Domain to Project">
+ <field name="project_id" colspan="4" readonly="1" />
+ <field name="domaine_id" colspan="4" />
+ <field name="main" colspan="4" />
+ <button type="object" name="action_add_domain_to_project" string="Add Domain to Project"/>
+ </form>
+ </field>
+ </record>
+
+ <!-- Actions window -->
+ <record model="ir.actions.act_window" id="wizard_add_domain_to_project">
+ <field name="name">Add Domain to Project</field>
+ <field name="res_model">cmmi.projet.domain.wizard</field>
+ <field name="view_mode">form</field>
+ <field name="target">new</field>
+ </record>
+
+
+ </data>
+</openerp>
--- /dev/null
+#-*- coding: utf8 -*-
+
+from openerp.osv import osv, fields
+
+#TODO: A la place de modifier le contexte pour ajouter projet_id,
+# Pourrait-on se fier à active_id ou active_ids ?
+
+#TODO: Il faut que si l'on coche une autre case, le rafraichissement se fasse.
+
+#TODO: erreur inexpliquée à corriger.
+
+class ProjetDomaineWizard(osv.TransientModel):
+
+ _name = "cmmi.projet.domain.wizard"
+
+ def default_get(self, cr, uid, fields, context=None):
+ ret = osv.TransientModel.default_get(self, cr, uid, fields, context=context)
+ project_id = context.get('project_id', False)
+ if project_id:
+ ret['project_id'] = project_id
+ return ret
+
+ def _domaines_selection(self, cr, uid, context=None):
+ model_base = self.pool.get("cmmi.description.domaine")
+ model_proj = self.pool.get("cmmi.projet.domaine")
+
+ if context is None or not context.has_key("project_id"):
+ return []
+
+ # recherche des lien entre le projet et les domaines
+ links_ids = model_proj.search(
+ cr,
+ uid,
+ [('project_id', '=', context["project_id"])],
+ context=context,
+ )
+
+ # récupérations des domaines déjà sélectionnés pour le projet
+ excluded_ids = list(set([r['domaine_id'][0] for r in model_proj.read(
+ cr,
+ uid,
+ links_ids,
+ fields=["domaine_id"],
+ context=context
+ )]))
+
+ # recherche des domaines autres que ceux déjà sélectionnés
+ domaines_ids = model_base.search(
+ cr,
+ uid,
+ [('id', 'not in', excluded_ids)],
+ context=context,
+ )
+
+ # renvoi des 2 uplets (id, name)
+ print [(r["id"], r["name"]) for r in model_base.read(
+ cr,
+ uid,
+ domaines_ids,
+ fields=["id", "name"],
+ context=context
+ )]
+ return [(r["id"], r["name"]) for r in model_base.read(
+ cr,
+ uid,
+ domaines_ids,
+ fields=["id", "name"],
+ context=context
+ )]
+
+ def action_add_domain_to_project(self, cr, uid, ids, context=None):
+ # Récupération du modèle utile pour écrire les données
+ model = self.pool.get("cmmi.projet.domaine")
+
+ # Un wizard, donc un seul identifiant
+ id = ids[0]
+
+ # Récupération des informations mises dans l'assistant
+ result = self.read(cr, uid, id, context=context)
+
+ # Si on a coché principal, on vire principal des autres domaines
+ if result["main"]:
+ model.write(
+ cr,
+ uid,
+ model.search(
+ cr,
+ uid,
+ [('project_id', '=', result["project_id"][0])],
+ context=context,
+ ),
+ {'main': False},
+ context=context,
+ )
+
+ # Création de la donnée à partir de la donnée du magicien
+ model.create(cr, uid, {
+ "main": result["main"],
+ "project_id": result["project_id"][0],
+ "domaine_id": result["domaine_id"],
+ }, context=context)
+
+ # Fermer simplement la fenêtre
+ #return {'type': 'ir.actions.act_window_close'}
+
+ # Renvoi vers la vue du modèle
+ return {
+ "type": 'ir.actions.act_window',
+ "res_model": "cmmi.projet",
+ 'view_type': 'form',
+ 'view_mode': 'form',
+ 'res_id': result["project_id"][0],
+ #'target': 'new', = pop-up
+ 'context': context,
+ }
+
+ _columns = {
+ "main": fields.boolean(string="Domaine principal ?"),
+ "project_id": fields.many2one("cmmi.projet",
+ string="Projet"),
+ "domaine_id": fields.selection(_domaines_selection,
+ string="Domaine"),
+ }
+