modifs
[odoo/odoo.git] / addons / process / process.py
index d7a3eb7..ea350b0 100644 (file)
@@ -1,28 +1,22 @@
 # -*- encoding: utf-8 -*-
 ##############################################################################
 #
-# Copyright (c) 2005-TODAY TINY SPRL. (http://tiny.be) All Rights Reserved.
+#    OpenERP, Open Source Management Solution  
+#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
+#    $Id$
 #
-# WARNING: This program as such is intended to be used by professional
-# programmers who take the whole responsability of assessing all potential
-# consequences resulting from its eventual inadequacies and bugs
-# End users who are looking for a ready-to-use solution with commercial
-# garantees and support are strongly adviced to contract a Free Software
-# Service Company
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, either version 3 of the License, or
+#    (at your option) any later version.
 #
-# This program is Free Software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU General Public License for more details.
 #
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+#    You should have received a copy of the GNU General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 ##############################################################################
 
@@ -56,6 +50,7 @@ class process_process(osv.osv):
     _columns = {
         'name': fields.char('Name', size=30,required=True, translate=True),
         'active': fields.boolean('Active'),
+        'model_id': fields.many2one('ir.model', 'Object', ondelete='set null'),
         'note': fields.text('Notes', translate=True),
         'node_ids': fields.one2many('process.node', 'process_id', 'Nodes')
     }
@@ -65,47 +60,60 @@ class process_process(osv.osv):
 
     def search_by_model(self, cr, uid, res_model, context):
         pool = pooler.get_pool(cr.dbname)
+        model_ids = (res_model or None) and pool.get('ir.model').search(cr, uid, [('model', '=', res_model)])
 
-        model_ids = pool.get('ir.model').search(cr, uid, [('model', '=', res_model)])
-        if not model_ids:
-            return []
-
-        nodes = pool.get('process.node').search(cr, uid, [('model_id', 'in', model_ids)])
-        if not nodes:
-            return []
-
-        nodes = pool.get('process.node').browse(cr, uid, nodes, context)
-
-        unique = []
+        domain = (model_ids or []) and [('model_id', 'in', model_ids)]
         result = []
-        
-        for node in nodes:
-            if node.process_id.id not in unique:
-                result.append((node.process_id.id, node.process_id.name))
-                unique.append(node.process_id.id)
+
+        # search all processes
+        res = pool.get('process.process').search(cr, uid, domain)
+        if res:
+            res = pool.get('process.process').browse(cr, uid, res, context)
+            for process in res:
+                result.append((process.id, process.name))
+            return result
+
+        # else search process nodes
+        res = pool.get('process.node').search(cr, uid, domain)
+        if res:
+            res = pool.get('process.node').browse(cr, uid, res, context)
+            for node in res:
+                if (node.process_id.id, node.process_id.name) not in result:
+                    result.append((node.process_id.id, node.process_id.name))
 
         return result
 
     def graph_get(self, cr, uid, id, res_model, res_id, scale, context):
-        
+
         pool = pooler.get_pool(cr.dbname)
         
-        process = pool.get('process.process').browse(cr, uid, [id])[0]
-        current_object = pool.get(res_model).browse(cr, uid, [res_id], context)[0]
-        current_user = pool.get('res.users').browse(cr, uid, [uid], context)[0]
-        
-        expr_context = Env(current_object, current_user)
-        
-        notes = process.note
+        process = pool.get('process.process').browse(cr, uid, [id], context)[0]
+
+        name = process.name
+        resource = None
+        state = 'N/A'
+
+        expr_context = {}
+        states = {}
+        perm = None
+
+        if res_model:
+            states = dict(pool.get(res_model).fields_get(cr, uid, context=context).get('state', {}).get('selection', {}))
+
+        if res_id:
+            current_object = pool.get(res_model).browse(cr, uid, [res_id], context)[0]
+            current_user = pool.get('res.users').browse(cr, uid, [uid], context)[0]
+            expr_context = Env(current_object, current_user)
+            resource = current_object.name
+            if 'state' in current_object:
+                state = states.get(current_object.state, 'N/A')
+            perm = pool.get(res_model).perm_read(cr, uid, [res_id], context)[0]
+
+        notes = process.note or "N/A"
         nodes = {}
         start = []
         transitions = {}
 
-        states = dict(pool.get(res_model).fields_get(cr, uid, context=context).get('state', {}).get('selection', {}))
-        title = "%s - Resource: %s, State: %s" % (process.name, current_object.name, states.get(getattr(current_object, 'state'), 'N/A'))
-
-        perm = pool.get(res_model).perm_read(cr, uid, [res_id], context)[0]
-
         for node in process.node_ids:
             data = {}
             data['name'] = node.name
@@ -117,6 +125,14 @@ class process_process(osv.osv):
             data['gray'] = False
             data['url'] = node.help_url
 
+            # get assosiated workflow
+            if data['model']:
+                wkf_ids = self.pool.get('workflow').search(cr, uid, [('osv', '=', data['model'])])
+                data['workflow'] = (wkf_ids or False) and wkf_ids[0]
+
+            if 'directory_id' in node and node.directory_id:
+                data['directory_id'] = node.directory_id.id
+
             if node.menu_id:
                 data['menu'] = {'name': node.menu_id.complete_name, 'id': node.menu_id.id}
             
@@ -124,7 +140,6 @@ class process_process(osv.osv):
                 try:
                     data['active'] = eval(node.model_states, expr_context)
                 except Exception, e:
-                    # waring: invalid state expression
                     pass
 
             if not data['active']:
@@ -185,6 +200,10 @@ class process_process(osv.osv):
             refobj = pool.get(ref_model).browse(cr, uid, [ref_id], context)[0]
             fields = pool.get(ref_model).fields_get(cr, uid, context=context)
 
+            # chech for directory_id from inherited from document module
+            if nodes[nid].get('directory_id', False):
+                resource['directory'] = self.pool.get('document.directory').get_resource_path(cr, uid, nodes[nid]['directory_id'], ref_model, ref_id)
+
             resource['name'] = refobj.name_get(context)[0][1]
             resource['perm'] = pool.get(ref_model).perm_read(cr, uid, [ref_id], context)[0]
 
@@ -199,19 +218,18 @@ class process_process(osv.osv):
                             rel = refobj[n]
                             if rel and isinstance(rel, list) :
                                 rel = rel[0]
-                            if isinstance(rel, basestring):
-                                print "XXX: ", rel
-                            try: # XXXXXXXXXXXXXXXXXXX
+                            try: # XXX: rel has been reported as string (check it)
                                 _id = (rel or False) and rel.id
                                 _model = node['model']
                                 update_relatives(r, _id, _model)
                             except:
                                 pass
 
-        for nid, node in nodes.items():
-            if not node['gray'] and (node['active'] or node['model'] == res_model):
-                update_relatives(nid, res_id, res_model)
-                break
+        if res_id:
+            for nid, node in nodes.items():
+                if not node['gray'] and (node['active'] or node['model'] == res_model):
+                    update_relatives(nid, res_id, res_model)
+                    break
 
         # calculate graph layout
         g = tools.graph(nodes.keys(), map(lambda x: (x['source'], x['target']), transitions.values()))
@@ -222,8 +240,8 @@ class process_process(osv.osv):
         # fix the height problem
         miny = -1
         for k,v in nodes.items():
-            x = graph[k]['y']
-            y = graph[k]['x']
+            x = graph[k]['x']
+            y = graph[k]['y']
             if miny == -1:
                 miny = y
             miny = min(y, miny)
@@ -234,7 +252,7 @@ class process_process(osv.osv):
             y = v['y']
             v['y'] = min(y - miny + 10, y)
 
-        return dict(title=title, perm=perm, notes=notes, nodes=nodes, transitions=transitions)
+        return dict(name=name, resource=resource, state=state, perm=perm, notes=notes, nodes=nodes, transitions=transitions)
 
     def copy(self, cr, uid, id, default=None, context={}):
         """ Deep copy the entire process.
@@ -300,14 +318,14 @@ class process_node(osv.osv):
         'flow_start': lambda *args: False,
     }
 
-    def copy(self, cr, uid, id, default=None, context={}):
+    def copy_data(self, cr, uid, id, default=None, context={}):
         if not default:
             default = {}
         default.update({
             'transition_in': [],
             'transition_out': []
         })
-        return super(process_node, self).copy(cr, uid, id, default, context)
+        return super(process_node, self).copy_data(cr, uid, id, default, context)
 
 process_node()
 
@@ -356,5 +374,16 @@ class process_transition_action(osv.osv):
     _defaults = {
         'state': lambda *args: 'dummy',
     }
+
+    def copy_data(self, cr, uid, id, default=None, context={}):
+        if not default:
+            default = {}
+
+        state = self.pool.get('process.transition.action').browse(cr, uid, [id], context)[0].state
+        if state:
+            default['state'] = state
+
+        return super(process_transition_action, self).copy_data(cr, uid, id, default, context)
+
 process_transition_action()