[IMP] res_config: better scope dont_skip_todo
[odoo/odoo.git] / bin / addons / base / res / res_config.py
index 68a5a38..f4aee87 100644 (file)
@@ -18,7 +18,7 @@
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 ##############################################################################
-
+import logging
 from operator import attrgetter
 
 from osv import osv, fields
@@ -26,6 +26,7 @@ from tools.translate import _
 import netsvc
 import pooler
 
+
 class res_config_configurable(osv.osv_memory):
     ''' Base classes for new-style configuration items
 
@@ -34,35 +35,53 @@ class res_config_configurable(osv.osv_memory):
     their view inherit from the related res_config_view_base view.
     '''
     _name = 'res.config'
+    _inherit = 'ir.wizard.screen'
     logger = netsvc.Logger()
+    __logger = logging.getLogger(_name)
+
+    def get_current_progress(self, cr, uid, context=None):
+        '''Return a description the current progress of configuration:
+        a tuple of (non_open_todos:int, total_todos: int)
+        '''
+        return (self.pool.get('ir.actions.todo')\
+                .search_count(cr, uid, [('state','<>','open')], context),
+                self.pool.get('ir.actions.todo')\
+                .search_count(cr, uid, [], context))
 
     def _progress(self, cr, uid, context=None):
-        total = self.pool.get('ir.actions.todo')\
-            .search_count(cr, uid, [], context)
-        open = self.pool.get('ir.actions.todo')\
-            .search_count(cr, uid, [('state','<>','open')], context)
+        closed, total = self.get_current_progress(cr, uid, context=context)
         if total:
-            return round(open*100./total)
+            return round(closed*100./total)
         return 100.
 
     _columns = dict(
-        progress=fields.float('Configuration Progress', readonly=True),
-        )
+        progress = fields.float('Configuration Progress', readonly=True),
+    )
+
     _defaults = dict(
-        progress=_progress
-        )
+        progress = _progress,
+    )
 
-    def _next_action(self, cr, uid):
+    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)
+        self.__logger.info('getting next %s', todos)
         active_todos = todos.search(cr, uid, [('state','=','open')],
                                     limit=1)
         if active_todos:
-            return todos.browse(cr, uid, active_todos[0], context=None)
+            todo_obj = todos.browse(cr, uid, active_todos[0], context=None)
+            todo_groups = map(lambda x:x.id, todo_obj.groups_id)
+            dont_skip_todo = True
+            if todo_groups:
+                cr.execute("select 1 from res_groups_users_rel where uid=%s and gid IN %s",(uid, tuple(todo_groups),))
+                dont_skip_todo = bool(cr.fetchone())
+            if dont_skip_todo:
+                return todos.browse(cr, uid, active_todos[0], context=None)
+            else:
+                todos.write(cr, uid, active_todos[0], {'state':'skip'}, context=None)
+                return self._next_action(cr, uid)
         return None
 
-    def _set_previous_todo(self, cr, uid, state):
+    def _set_previous_todo(self, cr, uid, state, context=None):
         """ lookup the previous (which is still the next at this point)
         ir.actions.todo, set it to whatever state was provided.
 
@@ -74,20 +93,17 @@ class res_config_configurable(osv.osv_memory):
         # this is ultra brittle, but apart from storing the todo id
         # into the res.config view, I'm not sure how to get the
         # "previous" todo
-        previous_todo = self._next_action(cr, uid)
+        previous_todo = self._next_action(cr, uid, context=context)
         if not previous_todo:
             raise LookupError(_("Couldn't find previous ir.actions.todo"))
         if not state:
-            raise ValueError(_("Can't set an ir.actions.todo's state to "
-                               "nothingness"))
+            raise ValueError(_("Can't set an ir.actions.todo's state to an empty value."))
         previous_todo.write({'state':state})
 
-    def _next(self, cr, uid):
-        self.logger.notifyChannel('actions', netsvc.LOG_INFO,
-                                  'getting next operation')
+    def _next(self, cr, uid, context=None):
+        self.__logger.info('getting next operation')
         next = self._next_action(cr, uid)
-        self.logger.notifyChannel('actions', netsvc.LOG_INFO,
-                                  'next action is %s' % next)
+        self.__logger.info('next action is %s', next)
         if next:
             action = next.action_id
             return {
@@ -97,10 +113,8 @@ class res_config_configurable(osv.osv_memory):
                 'res_model': action.res_model,
                 'type': action.type,
                 'target': action.target,
-                }
-        self.logger.notifyChannel(
-            'actions', netsvc.LOG_INFO,
-            'all configuration actions have been executed')
+            }
+        self.__logger.info('all configuration actions have been executed')
 
         current_user_menu = self.pool.get('res.users')\
             .browse(cr, uid, uid).menu_id
@@ -119,7 +133,7 @@ class res_config_configurable(osv.osv_memory):
         """ Returns the next todo action to execute (using the default
         sort order)
         """
-        return self._next(cr, uid)
+        return self._next(cr, uid, context=context)
 
     def execute(self, cr, uid, ids, context=None):
         """ Method called when the user clicks on the ``Next`` button.
@@ -154,7 +168,10 @@ class res_config_configurable(osv.osv_memory):
         an action dictionary -- executes the action provided by calling
         ``next``.
         """
-        self._set_previous_todo(cr, uid, state='done')
+        try:
+            self._set_previous_todo(cr, uid, state='done', context=context)
+        except Exception, e:
+            raise osv.except_osv(_('Error'), e.message)
         next = self.execute(cr, uid, ids, context=None)
         if next: return next
         return self.next(cr, uid, ids, context=context)
@@ -167,7 +184,10 @@ class res_config_configurable(osv.osv_memory):
         an action dictionary -- executes the action provided by calling
         ``next``.
         """
-        self._set_previous_todo(cr, uid, state='skip')
+        try:
+            self._set_previous_todo(cr, uid, state='skip', context=context)
+        except Exception, e:
+            raise osv.except_osv(_('Error'), e.message)
         next = self.cancel(cr, uid, ids, context=None)
         if next: return next
         return self.next(cr, uid, ids, context=context)
@@ -183,7 +203,10 @@ class res_config_configurable(osv.osv_memory):
         an action dictionary -- executes the action provided by calling
         ``next``.
         """
-        self._set_previous_todo(cr, uid, state='cancel')
+        try:
+            self._set_previous_todo(cr, uid, state='cancel', context=context)
+        except Exception, e:
+            raise osv.except_osv(_('Error'), e.message)
         next = self.cancel(cr, uid, ids, context=None)
         if next: return next
         return self.next(cr, uid, ids, context=context)
@@ -284,12 +307,13 @@ class res_config_installer(osv.osv_memory):
     """
     _name = 'res.config.installer'
     _inherit = 'res.config'
+    __logger = logging.getLogger(_name)
 
     _install_if = {}
 
     def _already_installed(self, cr, uid, context=None):
         """ For each module (boolean fields in a res.config.installer),
-        check if it's already installed (neither uninstallable nor uninstalled)
+        check if it's already installed (either 'to install', 'to upgrade' or 'installed')
         and if it is, check it by default
         """
         modules = self.pool.get('ir.module.module')
@@ -300,7 +324,7 @@ class res_config_installer(osv.osv_memory):
             cr, uid,
             modules.search(cr, uid,
                            [('name','in',selectable),
-                            ('state','not in',['uninstallable', 'uninstalled'])],
+                            ('state','in',['to install', 'installed', 'to upgrade'])],
                            context=context),
             context=context)
 
@@ -357,15 +381,17 @@ class res_config_installer(osv.osv_memory):
                             self._already_installed(cr, uid, context=context)),
                         True))
 
-    def fields_get(self, cr, uid, fields=None, context=None, read_access=True):
+    def fields_get(self, cr, uid, fields=None, context=None, write_access=True):
         """ If an addon is already installed, set it to readonly as
         res.config.installer doesn't handle uninstallations of already
         installed addons
         """
         fields = super(res_config_installer, self).fields_get(
-            cr, uid, fields, context, read_access)
+            cr, uid, fields, context, write_access)
 
         for module in self._already_installed(cr, uid, context=context):
+            if module.name not in fields:
+                continue
             fields[module.name].update(
                 readonly=True,
                 help=fields[module.name].get('help', '') +
@@ -377,15 +403,12 @@ class res_config_installer(osv.osv_memory):
         modules = self.pool.get('ir.module.module')
         to_install = list(self.modules_to_install(
             cr, uid, ids, context=context))
-        self.logger.notifyChannel(
-            'installer', netsvc.LOG_INFO,
-            'Selecting addons %s to install'%to_install)
+        self.__logger.info('Selecting addons %s to install', to_install)
         modules.state_update(
             cr, uid,
             modules.search(cr, uid, [('name','in',to_install)]),
             'to install', ['uninstalled'], context=context)
-        cr.commit()
-
+        cr.commit() #TOFIX: after remove this statement, installation wizard is fail 
         pooler.restart_pool(cr.dbname, update_module=True)
 res_config_installer()
 
@@ -402,6 +425,7 @@ class ir_actions_configuration_wizard(osv.osv_memory):
     '''
     _name='ir.actions.configuration.wizard'
     _inherit = 'res.config'
+    __logger = logging.getLogger(_name)
 
     def _next_action_note(self, cr, uid, ids, context=None):
         next = self._next_action(cr, uid)
@@ -421,8 +445,7 @@ class ir_actions_configuration_wizard(osv.osv_memory):
         }
 
     def execute(self, cr, uid, ids, context=None):
-        self.logger.notifyChannel(
-            'configuration', netsvc.LOG_WARNING, DEPRECATION_MESSAGE)
+        self.__logger.warn(DEPRECATION_MESSAGE)
 
 ir_actions_configuration_wizard()