[IMP] add new handler for addons configuration, corresponding menu item
[odoo/odoo.git] / bin / addons / base / ir / ir_actions.py
index d806f49..f8c6dfa 100644 (file)
@@ -1,21 +1,20 @@
-# -*- encoding: utf-8 -*-
+# -*- coding: utf-8 -*-
 ##############################################################################
 #
-#    OpenERP, Open Source Management Solution    
-#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
-#    $Id$
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
 #
 #    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.
+#    it under the terms of the GNU Affero 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 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.
+#    GNU Affero General Public License for more details.
 #
-#    You should have received a copy of the GNU General Public License
+#    You should have received a copy of the GNU Affero General Public License
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 ##############################################################################
@@ -29,13 +28,7 @@ import netsvc
 import re
 import copy
 import sys
-
-try:
-    from xml import dom, xpath
-except ImportError:
-    sys.stderr.write("ERROR: Import xpath module\n")
-    sys.stderr.write("ERROR: Try to install the old python-xml package\n")
-    sys.exit(2)
+from xml import dom
 
 class actions(osv.osv):
     _name = 'ir.actions.actions'
@@ -128,9 +121,10 @@ class report_xml(osv.osv):
             ('html', 'html'),
             ('raw', 'raw'),
             ('sxw', 'sxw'),
+            ('txt', 'txt'),
             ('odt', 'odt'),
             ('html2html','HTML from HTML'),
-            ('mako2html','Mako from HTML'),
+            ('mako2html','HTML from HTML(Mako)'),
             ], string='Type', required=True),
         'groups_id': fields.many2many('res.groups', 'res_groups_report_rel', 'uid', 'gid', 'Groups'),
         'attachment': fields.char('Save As Attachment Prefix', size=128, help='This is the filename of the attachment used to store the printing result. Keep empty to not save the printed reports. You can use a python expression with the object and time variables.'),
@@ -152,7 +146,7 @@ class act_window(osv.osv):
     _name = 'ir.actions.act_window'
     _table = 'ir_act_window'
     _sequence = 'ir_actions_id_seq'
-    
+
     def _check_model(self, cr, uid, ids, context={}):
         for action in self.browse(cr, uid, ids, context):
             if not self.pool.get(action.res_model):
@@ -253,7 +247,7 @@ class act_window(osv.osv):
         'filter': fields.boolean('Filter'),
         'default_user_ids': fields.many2many('res.users', 'ir_act_window_user_rel', 'act_id', 'uid', 'Users'),
         'search_view' : fields.function(_search_view, type='text', method=True, string='Search View'),
-        'menus': fields.char('Menus', size=4096)            
+        'menus': fields.char('Menus', size=4096)
     }
     _defaults = {
         'type': lambda *a: 'ir.actions.act_window',
@@ -349,59 +343,6 @@ class ir_model_fields(osv.osv):
     _columns = {
         'complete_name': fields.char('Complete Name', size=64, select=1),
     }
-
-    def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=80):
-        return super(ir_model_fields, self).name_search(cr, uid, name, args, operator, context, limit)
-#        def get_fields(cr, uid, field, rel):
-#            result = []
-#            mobj = self.pool.get('ir.model')
-#            id = mobj.search(cr, uid, [('model','=',rel)])
-
-#            obj = self.pool.get('ir.model.fields')
-#            ids = obj.search(cr, uid, [('model_id','in',id)])
-#            records = obj.read(cr, uid, ids)
-#            for record in records:
-#                id = record['id']
-#                fld = field + '/' + record['name']
-
-#                result.append((id, fld))
-#            return result
-
-#        if not args:
-#            args=[]
-#        if not context:
-#            context={}
-#            return super(ir_model_fields, self).name_search(cr, uid, name, args, operator, context, limit)
-
-#        if context.get('key') != 'server_action':
-#            return super(ir_model_fields, self).name_search(cr, uid, name, args, operator, context, limit)
-#        result = []
-#        obj = self.pool.get('ir.model.fields')
-#        ids = obj.search(cr, uid, args)
-#        records = obj.read(cr, uid, ids)
-#        for record in records:
-#            id = record['id']
-#            field = record['name']
-
-#            if record['ttype'] == 'many2one':
-#                rel = record['relation']
-#                res = get_fields(cr, uid, field, record['relation'])
-#                for rs in res:
-#                    result.append(rs)
-
-#            result.append((id, field))
-
-#        for rs in result:
-#            obj.write(cr, uid, [rs[0]], {'complete_name':rs[1]})
-
-#        iids = []
-#        for rs in result:
-#            iids.append(rs[0])
-
-#        result = super(ir_model_fields, self).name_search(cr, uid, name, [('complete_name','ilike',name), ('id','in',iids)], operator, context, limit)
-
-#        return result
-
 ir_model_fields()
 
 class server_object_lines(osv.osv):
@@ -429,12 +370,12 @@ class actions_server(osv.osv):
     def _select_signals(self, cr, uid, context={}):
         cr.execute("select distinct t.signal as key, t.signal || ' - [ ' || w.osv || ' ] ' as val from wkf w, wkf_activity a, wkf_transition t "\
                         " where w.id = a.wkf_id " \
-                        " and t.act_from = a.wkf_id " \
-                        " or t.act_to = a.wkf_id and t.signal not in (null, NULL)")
+                        " and t.act_from = a.id " \
+                        " or t.act_to = a.id and t.signal not in (null, NULL)")
         result = cr.fetchall() or []
         res = []
         for rs in result:
-            if not rs[0] == None and not rs[1] == None:
+            if rs[0] is not None and rs[1] is not None:
                 res.append(rs)
         return res
 
@@ -548,10 +489,10 @@ class actions_server(osv.osv):
             if result in (None, False):
                 return str("--------")
             return str(result)
-        
+
         com = re.compile('(\[\[.+?\]\])')
         message = com.sub(merge, keystr)
-        
+
         return message
 
     # Context should contains:
@@ -560,16 +501,16 @@ class actions_server(osv.osv):
     # OUT:
     #   False : Finnished correctly
     #   ACTION_ID : Action to launch
-    
+
     def run(self, cr, uid, ids, context={}):
         logger = netsvc.Logger()
-        
+
         for action in self.browse(cr, uid, ids, context):
             obj_pool = self.pool.get(action.model_id.model)
             obj = obj_pool.browse(cr, uid, context['active_id'], context=context)
             cxt = {
-                'context':context, 
-                'object': obj, 
+                'context':context,
+                'object': obj,
                 'time':time,
                 'cr': cr,
                 'pool' : self.pool,
@@ -578,12 +519,12 @@ class actions_server(osv.osv):
             expr = eval(str(action.condition), cxt)
             if not expr:
                 continue
-            
+
             if action.state=='client_action':
                 if not action.action_id:
-                    raise osv.except_osv(_('Error'), _("Please specify an action to launch !")) 
-                result = self.pool.get(action.action_id.type).read(cr, uid, action.action_id.id, context=context)
-                return result
+                    raise osv.except_osv(_('Error'), _("Please specify an action to launch !"))
+                return self.pool.get(action.action_id.type)\
+                    .read(cr, uid, action.action_id.id, context=context)
 
             if action.state == 'code':
                 localdict = {
@@ -606,16 +547,16 @@ class actions_server(osv.osv):
                     address =  eval(str(action.email), cxt)
                 except:
                     pass
-                
+
                 if not address:
                     logger.notifyChannel('email', netsvc.LOG_INFO, 'Partner Email address not Specified!')
                     continue
                 if not user:
                     raise osv.except_osv(_('Error'), _("Please specify server option --smtp-from !"))
-                
+
                 subject = self.merge_message(cr, uid, str(action.subject), action, context)
                 body = self.merge_message(cr, uid, str(action.message), action, context)
-                
+
                 if tools.email_send(user, [address], subject, body, debug=False, subtype='html') == True:
                     logger.notifyChannel('email', netsvc.LOG_INFO, 'Email successfully send to : %s' % (address))
                 else:
@@ -640,7 +581,7 @@ class actions_server(osv.osv):
                     logger.notifyChannel('sms', netsvc.LOG_INFO, 'SMS successfully send to : %s' % (action.address))
                 else:
                     logger.notifyChannel('sms', netsvc.LOG_ERROR, 'Failed to send SMS to : %s' % (action.address))
-            
+
             if action.state == 'other':
                 res = []
                 for act in action.child_ids:
@@ -648,15 +589,15 @@ class actions_server(osv.osv):
                     result = self.run(cr, uid, [act.id], context)
                     if result:
                         res.append(result)
-                    
+
                 return res
-            
+
             if action.state == 'loop':
                 obj_pool = self.pool.get(action.model_id.model)
                 obj = obj_pool.browse(cr, uid, context['active_id'], context=context)
                 cxt = {
-                    'context':context, 
-                    'object': obj, 
+                    'context':context,
+                    'object': obj,
                     'time':time,
                     'cr': cr,
                     'pool' : self.pool,
@@ -667,7 +608,7 @@ class actions_server(osv.osv):
                 for i in expr:
                     context['active_id'] = i.id
                     result = self.run(cr, uid, [action.loop_action.id], context)
-            
+
             if action.state == 'object_write':
                 res = {}
                 for exp in action.fields_lines:
@@ -689,7 +630,7 @@ class actions_server(osv.osv):
                         write_id = context.get('active_id')
                         obj_pool = self.pool.get(action.srcmodel_id.model)
                         obj_pool.write(cr, uid, [write_id], res)
-                        
+
                 elif action.write_id:
                     obj_pool = self.pool.get(action.srcmodel_id.model)
                     rec = self.pool.get(action.model_id.model).browse(cr, uid, context.get('active_id'))
@@ -698,7 +639,7 @@ class actions_server(osv.osv):
                         id = int(id)
                     except:
                         raise osv.except_osv(_('Error'), _("Problem in configuration `Record Id` in Server Action!"))
-                    
+
                     if type(id) != type(1):
                         raise osv.except_osv(_('Error'), _("Problem in configuration `Record Id` in Server Action!"))
                     write_id = id
@@ -742,9 +683,9 @@ act_window_close()
 # if action type is 'service',
 #                - if start_type= 'at once', it will be start at one time on start date
 #                - if start_type='auto', it will be start on auto starting from start date, and stop on stop date
-#                - if start_type="manual", it will start and stop on manually 
+#                - if start_type="manual", it will start and stop on manually
 class ir_actions_todo(osv.osv):
-    _name = 'ir.actions.todo'    
+    _name = 'ir.actions.todo'
     _columns={
         'name':fields.char('Name',size=64,required=True, select=True),
         'note':fields.text('Text', translate=True),
@@ -773,67 +714,77 @@ class ir_actions_configuration_wizard(osv.osv_memory):
     _name='ir.actions.configuration.wizard'
     def next_configuration_action(self,cr,uid,context={}):
         item_obj = self.pool.get('ir.actions.todo')
-        item_ids = item_obj.search(cr, uid, [('type','=','configure'),('state', '=', 'open'),('active','=',True)], limit=1, context=context)
-        if item_ids and len(item_ids):
-            item = item_obj.browse(cr, uid, item_ids[0], context=context)
-            return item
+        item_ids = item_obj.search(cr, uid,
+                                   [('type','=','configure'),
+                                    ('state', '=', 'open'),
+                                    ('active','=',True)],
+                                   limit=1, context=context)
+        if item_ids:
+            return item_obj.browse(cr, uid, item_ids[0], context=context)
         return False
     def _get_action_name(self, cr, uid, context={}):
-        next_action=self.next_configuration_action(cr,uid,context=context)        
+        next_action = self.next_configuration_action(cr,uid,context=context)
         if next_action:
             return next_action.note
         else:
             return "Your database is now fully configured.\n\nClick 'Continue' and enjoy your OpenERP experience..."
-        return False
 
     def _get_action(self, cr, uid, context={}):
-        next_action=self.next_configuration_action(cr,uid,context=context)
+        next_action = self.next_configuration_action(cr, uid, context=context)
         if next_action:
             return next_action.id
-        return False
-
-    def _progress_get(self,cr,uid, context={}):
-        total = self.pool.get('ir.actions.todo').search_count(cr, uid, [], context)
-        todo = self.pool.get('ir.actions.todo').search_count(cr, uid, [('type','=','configure'),('active','=',True),('state','<>','open')], context)
+        return
+
+    def _progress_get(self, cr, uid, context=None):
+        total = self.pool.get('ir.actions.todo')\
+            .search_count(cr, uid, [], context)
+        todo = self.pool.get('ir.actions.todo')\
+            .search_count(cr, uid,[('type','=','configure'),
+                                   ('active','=',True),
+                                   ('state','<>','open')],
+                          context)
         if total > 0.0:
-            return max(5.0,round(todo*100/total))
+            return max(5.0, round(todo*100/total))
         else:
             return 100.0
 
     _columns = {
-        'name': fields.text('Next Wizard',readonly=True),
+        'name': fields.text('Next Wizard', readonly=True),
         'progress': fields.float('Configuration Progress', readonly=True),
-        'item_id':fields.many2one('ir.actions.todo', 'Next Configuration Wizard',invisible=True, readonly=True),
+        'item_id': fields.many2one('ir.actions.todo',
+                                  'Next Configuration Wizard',
+                                  invisible=True, readonly=True),
     }
     _defaults={
         'progress': _progress_get,
-        'item_id':_get_action,
-        'name':_get_action_name,
+        'item_id': _get_action,
+        'name': _get_action_name,
     }
     def button_next(self,cr,uid,ids,context=None):
-        user_action=self.pool.get('res.users').browse(cr,uid,uid)
-        act_obj=self.pool.get(user_action.menu_id.type)
-        action_ids=act_obj.search(cr,uid,[('name','=',user_action.menu_id.name)])
-        action_open=act_obj.browse(cr,uid,action_ids)[0]
-        if context.get('menu',False):
-            return{
+        user_action = self.pool.get('res.users').browse(cr,uid,uid)
+        act_obj = self.pool.get(user_action.menu_id.type)
+        action_ids = act_obj.search(cr,uid,[
+                ('name', '=', user_action.menu_id.name)])
+        action_open = act_obj.browse(cr, uid, action_ids)[0]
+        if context and 'menu' in context:
+            return {
                 'view_type': action_open.view_type,
-                'view_id':action_open.view_id and [action_open.view_id.id] or False,
+                'view_id': action_open.view_id and [action_open.view_id.id] or False,
                 'res_model': action_open.res_model,
                 'type': action_open.type,
-                'domain':action_open.domain
+                'domain': action_open.domain
             }
-        return {'type':'ir.actions.act_window_close'}
+        return {'type': 'ir.actions.act_window_close'}
 
     def button_skip(self,cr,uid,ids,context=None):
         item_obj = self.pool.get('ir.actions.todo')
-        item_id=self.read(cr,uid,ids)[0]['item_id']
+        item_id = self.read(cr,uid,ids)[0]['item_id']
         if item_id:
             item = item_obj.browse(cr, uid, item_id, context=context)
             item_obj.write(cr, uid, item.id, {
                 'state': 'skip',
                 }, context=context)
-            return{
+            return {
                 'view_type': 'form',
                 "view_mode": 'form',
                 'res_model': 'ir.actions.configuration.wizard',
@@ -844,13 +795,13 @@ class ir_actions_configuration_wizard(osv.osv_memory):
 
     def button_continue(self, cr, uid, ids, context=None):
         item_obj = self.pool.get('ir.actions.todo')
-        item_id=self.read(cr,uid,ids)[0]['item_id']
+        item_id = self.read(cr,uid,ids)[0]['item_id']
         if item_id:
             item = item_obj.browse(cr, uid, item_id, context=context)
             item_obj.write(cr, uid, item.id, {
                 'state': 'done',
                 }, context=context)
-            return{
+            return {
                   'view_mode': item.action_id.view_mode,
                   'view_type': item.action_id.view_type,
                   'view_id':item.action_id.view_id and [item.action_id.view_id.id] or False,
@@ -861,5 +812,42 @@ class ir_actions_configuration_wizard(osv.osv_memory):
         return self.button_next(cr, uid, ids, context)
 ir_actions_configuration_wizard()
 
+class ir_actions_configurator(osv.osv_memory):
+    _name = 'ir.actions.configurator'
+    logger = netsvc.Logger()
+    def next_action(self, cr, uid, context=None):
+        todos = self.pool.get('ir.actions.todo')
+        self.logger.notifyChannel('actions', netsvc.LOG_INFO,
+                                  'getting next %s' % todos)
+        active_todos = todos.search(cr, uid,
+                                [('type','=','configure'),
+                                 ('state', '=', 'open'),
+                                 ('active','=',True)],
+                                limit=1, context=None)
+        if active_todos:
+            return todos.browse(cr, uid, active_todos[0], context=None)
+        return None
+
+    def next(self, cr, uid):
+        self.logger.notifyChannel('actions', netsvc.LOG_INFO,
+                                  'getting next operation')
+        next = self.next_action(cr, uid)
+        self.logger.notifyChannel('actions', netsvc.LOG_INFO,
+                                  'next action is %s' % next)
+        if next:
+            self.pool.get('ir.actions.todo').write(cr, uid, next.id, {
+                    'state':'done',
+                    }, context=None)
+            action = next.action_id
+            return {
+                'view_mode': action.view_mode,
+                'view_type': action.view_type,
+                'view_id': action.view_id and [action.view_id.id] or False,
+                'res_model': action.res_model,
+                'type': action.type,
+                'target': action.target,
+                }
+        return {'type': 'ir.actions.act_window_close'}
+ir_actions_configurator()
 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: