Premières expériences wizard
authorSébastien CHAZALLET <s.chazallet@gmail.com>
Fri, 3 May 2013 22:34:48 +0000 (00:34 +0200)
committerSébastien CHAZALLET <s.chazallet@gmail.com>
Fri, 3 May 2013 22:34:48 +0000 (00:34 +0200)
__init__.py
__openerp__.py
projet.py
views/projet.xml
views/wizards/domaine.xml [new file with mode: 0644]
wizards/__init__.py [new file with mode: 0644]
wizards/domaine.py [new file with mode: 0644]

index 2341845..01cb177 100644 (file)
@@ -5,3 +5,5 @@ import description
 import partner
 import projet
 import evolution
+
+import wizards.domaine
\ No newline at end of file
index 8fc5f7f..b063cf0 100644 (file)
@@ -6,11 +6,14 @@
     "category": "Tools",
     "description": """Permet de gérer des projets.""",
     "data": [
+        # Vues associées aux modèles
         'views/projet.xml',
+        # vues associées aux assistants
+        'views/wizards/domaine.xml',
+        # Données
         'datas/moe.xml',
         'datas/moa.xml',
         'datas/domaines.xml',
-        # fichiers de données (vues, ...)
     ],
     "demo": [
         'demo/projet.xml'
index 1e1038d..39b2327 100644 (file)
--- a/projet.py
+++ b/projet.py
@@ -56,6 +56,8 @@ class Projet(osv.Model):
                                           string="Structure principale"),
     }
 
+    def action_add_domain(self, cr, uid, ids, context=None):
+        pass
 
 class ProjetDomaine(osv.Model):
 
@@ -80,3 +82,34 @@ 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}}
index b2d8144..ea74c46 100644 (file)
                                 </field>
                             </page>
                             <page string="Domaines">
+                                <button type="action" target="new" name="%(cmmi.wizard_add_domain_to_project)d" string="Add a domain" class="oe_highlight" context="{'project_id': id}" />
                                 <field name="domaines">
-                                    <tree>
+                                    <tree create="false" delete="true" editable="true">
+                                        <field name="project_id" invisible="1" />
+                                        <field name="domaine_id" invisible="1" />
                                         <field name="name" />
-                                        <field name="main" />
+                                        <field name="main" on_change="onchange_main(project_id, domaine_id, main)" />
                                     </tree>
                                 </field>
                             </page>
diff --git a/views/wizards/domaine.xml b/views/wizards/domaine.xml
new file mode 100644 (file)
index 0000000..3c5ec9a
--- /dev/null
@@ -0,0 +1,30 @@
+<?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>
diff --git a/wizards/__init__.py b/wizards/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/wizards/domaine.py b/wizards/domaine.py
new file mode 100644 (file)
index 0000000..2304a2e
--- /dev/null
@@ -0,0 +1,124 @@
+#-*- 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"),
+    }
+