diff --git a/bin/addons/base/ir/ir_model.py b/bin/addons/base/ir/ir_model.py index a0040a836df..583985b5930 100644 --- a/bin/addons/base/ir/ir_model.py +++ b/bin/addons/base/ir/ir_model.py @@ -401,6 +401,12 @@ class ir_model_data(osv.osv): self.doinit = True self.unlink_mark = {} + def _auto_init(self, cr, context=None): + super(ir_model_data, self)._auto_init(cr, context) + cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'ir_model_data_module_name_index\'') + if not cr.fetchone(): + cr.execute('CREATE INDEX ir_model_data_module_name_index ON ir_model_data (module, name)') + @tools.cache() def _get_id(self, cr, uid, module, xml_id): """Returns the id of the ir.model.data record corresponding to a given module and xml_id (cached) or raise a ValueError if not found""" @@ -448,18 +454,19 @@ class ir_model_data(osv.osv): action_id = False if xml_id: - cr.execute('select id,res_id from ir_model_data where module=%s and name=%s', (module,xml_id)) + cr.execute('''SELECT imd.id, imd.res_id, md.id + FROM ir_model_data imd LEFT JOIN %s md ON (imd.res_id = md.id) + WHERE imd.module=%%s AND imd.name=%%s''' % model_obj._table, + (module, xml_id)) results = cr.fetchall() - for action_id2,res_id2 in results: - cr.execute('select id from '+model_obj._table+' where id=%s', (res_id2,)) - result3 = cr.fetchone() - if not result3: + for imd_id2,res_id2,real_id2 in results: + if not real_id2: self._get_id.clear_cache(cr.dbname, uid, module, xml_id) self.get_object_reference.clear_cache(cr.dbname, uid, module, xml_id) - cr.execute('delete from ir_model_data where id=%s', (action_id2,)) + cr.execute('delete from ir_model_data where id=%s', (imd_id2,)) res_id = False else: - res_id,action_id = res_id2,action_id2 + res_id,action_id = res_id2,imd_id2 if action_id and res_id: model_obj.write(cr, uid, [res_id], values, context=context) diff --git a/bin/addons/base/ir/ir_values.py b/bin/addons/base/ir/ir_values.py index 21ee1a66f93..602e2a10a80 100644 --- a/bin/addons/base/ir/ir_values.py +++ b/bin/addons/base/ir/ir_values.py @@ -72,21 +72,21 @@ class ir_values(osv.osv): 'name': fields.char('Name', size=128), 'model_id': fields.many2one('ir.model', 'Object', size=128, help="This field is not used, it only helps you to select a good model."), - 'model': fields.char('Object Name', size=128), + 'model': fields.char('Object Name', size=128, select=True), 'action_id': fields.many2one('ir.actions.actions', 'Action', help="This field is not used, it only helps you to select the right action."), 'value': fields.text('Value'), 'value_unpickle': fields.function(_value_unpickle, fnct_inv=_value_pickle, method=True, type='text', string='Value'), 'object': fields.boolean('Is Object'), - 'key': fields.selection([('action','Action'),('default','Default')], 'Type', size=128), - 'key2' : fields.char('Event Type',help="The kind of action or button in the client side that will trigger the action.", size=128), + 'key': fields.selection([('action','Action'),('default','Default')], 'Type', size=128, select=True), + 'key2' : fields.char('Event Type',help="The kind of action or button in the client side that will trigger the action.", size=128, select=True), 'meta': fields.text('Meta Datas'), 'meta_unpickle': fields.function(_value_unpickle, fnct_inv=_value_pickle, method=True, type='text', string='Metadata'), - 'res_id': fields.integer('Object ID', help="Keep 0 if the action must appear on all resources."), - 'user_id': fields.many2one('res.users', 'User', ondelete='cascade'), - 'company_id': fields.many2one('res.company', 'Company') + 'res_id': fields.integer('Object ID', help="Keep 0 if the action must appear on all resources.", select=True), + 'user_id': fields.many2one('res.users', 'User', ondelete='cascade', select=True), + 'company_id': fields.many2one('res.company', 'Company', select=True) } _defaults = { 'key': lambda *a: 'action', @@ -94,11 +94,11 @@ class ir_values(osv.osv): 'company_id': lambda *a: False } - def _auto_init(self, cr, context={}): + def _auto_init(self, cr, context=None): super(ir_values, self)._auto_init(cr, context) - cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'ir_values_key_model_key2_index\'') + cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'ir_values_key_model_key2_res_id_user_id_idx\'') if not cr.fetchone(): - cr.execute('CREATE INDEX ir_values_key_model_key2_index ON ir_values (key, model, key2)') + cr.execute('CREATE INDEX ir_values_key_model_key2_res_id_user_id_idx ON ir_values (key, model, key2, res_id, user_id)') def set(self, cr, uid, key, key2, name, models, value, replace=True, isobject=False, meta=False, preserve_user=False, company=False): if isinstance(value, unicode): diff --git a/bin/addons/base/ir/workflow/workflow.py b/bin/addons/base/ir/workflow/workflow.py index a117688399d..dda5dd46276 100644 --- a/bin/addons/base/ir/workflow/workflow.py +++ b/bin/addons/base/ir/workflow/workflow.py @@ -175,7 +175,7 @@ class wkf_instance(osv.osv): 'res_type': fields.char('Resource Object', size=64, select=True), 'state': fields.char('State', size=32, select=True), } - def _auto_init(self, cr, context={}): + def _auto_init(self, cr, context=None): super(wkf_instance, self)._auto_init(cr, context) cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = \'wkf_instance_res_id_res_type_state_index\'') if not cr.fetchone(): diff --git a/bin/addons/base/module/module.py b/bin/addons/base/module/module.py index bdd76d75e0c..27979bfb2cc 100644 --- a/bin/addons/base/module/module.py +++ b/bin/addons/base/module/module.py @@ -57,7 +57,7 @@ class module_category(osv.osv): return result _columns = { - 'name': fields.char("Name", size=128, required=True), + 'name': fields.char("Name", size=128, required=True, select=True), 'parent_id': fields.many2one('ir.module.category', 'Parent Category', select=True), 'child_ids': fields.one2many('ir.module.category', 'parent_id', 'Child Categories'), 'module_nr': fields.function(_module_nbr, method=True, string='Number of Modules', type='integer') @@ -96,26 +96,34 @@ class module(osv.osv): mlist = self.browse(cr, uid, ids, context=context) mnames = {} for m in mlist: - mnames[m.name] = m.id + # skip uninstalled modules below, + # no data to find anyway + if m.state in ('installed', 'to upgrade', 'to remove'): + mnames[m.name] = m.id res[m.id] = { 'menus_by_module':[], 'reports_by_module':[], 'views_by_module': [] } + + if not mnames: + return res + view_id = model_data_obj.search(cr,uid,[('module','in', mnames.keys()), ('model','in',('ir.ui.view','ir.actions.report.xml','ir.ui.menu'))]) for data_id in model_data_obj.browse(cr,uid,view_id,context): # We use try except, because views or menus may not exist try: key = data_id.model + res_mod_dic = res[mnames[data_id.module]] if key=='ir.ui.view': v = view_obj.browse(cr,uid,data_id.res_id) aa = v.inherit_id and '* INHERIT ' or '' - res[mnames[data_id.module]]['views_by_module'].append(aa + v.name + '('+v.type+')') + res_mod_dic['views_by_module'].append(aa + v.name + '('+v.type+')') elif key=='ir.actions.report.xml': - res[mnames[data_id.module]]['reports_by_module'].append(report_obj.browse(cr,uid,data_id.res_id).name) + res_mod_dic['reports_by_module'].append(report_obj.browse(cr,uid,data_id.res_id).name) elif key=='ir.ui.menu': - res[mnames[data_id.module]]['menus_by_module'].append(menu_obj.browse(cr,uid,data_id.res_id).complete_name) + res_mod_dic['menus_by_module'].append(menu_obj.browse(cr,uid,data_id.res_id).complete_name) except KeyError, e: self.__logger.warning( 'Data not found for reference %s[%s:%s.%s]', data_id.model, @@ -344,8 +352,8 @@ class module(osv.osv): 'maintainer': terp.get('maintainer', False), 'contributors': ', '.join(terp.get('contributors', [])) or False, 'website': terp.get('website', ''), - 'license': terp.get('license', 'GPL-2'), - 'certificate': terp.get('certificate') or None, + 'license': terp.get('license', 'AGPL-3'), + 'certificate': terp.get('certificate') or False, 'web': terp.get('web') or False, } @@ -353,34 +361,40 @@ class module(osv.osv): def update_list(self, cr, uid, context={}): res = [0, 0] # [update, add] - # iterate through installed modules and mark them as being so + known_mods = self.browse(cr, uid, self.search(cr, uid, [])) + known_mods_names = dict([(m.name, m) for m in known_mods]) + + # iterate through detected modules and update/create them in db for mod_name in addons.get_modules(): - ids = self.search(cr, uid, [('name','=',mod_name)]) + mod = known_mods_names.get(mod_name) terp = self.get_module_info(mod_name) values = self.get_values_from_terp(terp) - if ids: - id = ids[0] - mod = self.browse(cr, uid, id) + if mod: + updated_values = {} + for key in values: + old = getattr(mod, key) + updated = isinstance(values[key], basestring) and tools.ustr(values[key]) or values[key] + if not old == updated: + updated_values[key] = values[key] if terp.get('installable', True) and mod.state == 'uninstallable': - self.write(cr, uid, id, {'state': 'uninstalled'}) + updated_values['state'] = 'uninstalled' if parse_version(terp.get('version', '')) > parse_version(mod.latest_version or ''): - self.write(cr, uid, id, {'url': ''}) res[0] += 1 - self.write(cr, uid, id, values) - cr.execute('DELETE FROM ir_module_module_dependency WHERE module_id = %s', (id,)) + if updated_values: + self.write(cr, uid, mod.id, updated_values) else: mod_path = addons.get_module_path(mod_name) if not mod_path: continue if not terp or not terp.get('installable', True): continue - - ids = self.search(cr, uid, [('name','=',mod_name)]) id = self.create(cr, uid, dict(name=mod_name, state='uninstalled', **values)) + mod = self.browse(cr, uid, id) res[1] += 1 - self._update_dependencies(cr, uid, id, terp.get('depends', [])) - self._update_category(cr, uid, id, terp.get('category', 'Uncategorized')) + + self._update_dependencies(cr, uid, mod, terp.get('depends', [])) + self._update_category(cr, uid, mod, terp.get('category', 'Uncategorized')) return res @@ -412,39 +426,49 @@ class module(osv.osv): self.write(cr, uid, mod.id, self.get_values_from_terp(terp)) cr.execute('DELETE FROM ir_module_module_dependency ' \ 'WHERE module_id = %s', (mod.id,)) - self._update_dependencies(cr, uid, mod.id, terp.get('depends', + self._update_dependencies(cr, uid, mod, terp.get('depends', [])) - self._update_category(cr, uid, mod.id, terp.get('category', + self._update_category(cr, uid, mod, terp.get('category', 'Uncategorized')) # Import module zimp = zipimport.zipimporter(fname) zimp.load_module(mod.name) return res - def _update_dependencies(self, cr, uid, id, depends=None): + def _update_dependencies(self, cr, uid, mod_browse, depends=None): if depends is None: depends = [] - for d in depends: - cr.execute('INSERT INTO ir_module_module_dependency (module_id, name) values (%s, %s)', (id, d)) + existing = set(x.name for x in mod_browse.dependencies_id) + needed = set(depends) + for dep in (needed - existing): + cr.execute('INSERT INTO ir_module_module_dependency (module_id, name) values (%s, %s)', (mod_browse.id, dep)) + for dep in (existing - needed): + cr.execute('DELETE FROM ir_module_module_dependency WHERE module_id = %s and name = %s', (mod_browse.id, dep)) + + def _update_category(self, cr, uid, mod_browse, category='Uncategorized'): + current_category = mod_browse.category_id + current_category_path = [] + while current_category: + current_category_path.insert(0, current_category.name) + current_category = current_category.parent_id - def _update_category(self, cr, uid, id, category='Uncategorized'): categs = category.split('/') - p_id = None - while categs: - if p_id is not None: - cr.execute('select id from ir_module_category where name=%s and parent_id=%s', (categs[0], p_id)) - else: - cr.execute('select id from ir_module_category where name=%s and parent_id is NULL', (categs[0],)) - c_id = cr.fetchone() - if not c_id: - cr.execute('select nextval(\'ir_module_category_id_seq\')') - c_id = cr.fetchone()[0] - cr.execute('insert into ir_module_category (id, name, parent_id) values (%s, %s, %s)', (c_id, categs[0], p_id)) - else: - c_id = c_id[0] - p_id = c_id - categs = categs[1:] - self.write(cr, uid, [id], {'category_id': p_id}) + if categs != current_category_path: + p_id = None + while categs: + if p_id is not None: + cr.execute('SELECT id FROM ir_module_category WHERE name=%s AND parent_id=%s', (categs[0], p_id)) + else: + cr.execute('SELECT id FROM ir_module_category WHERE name=%s AND parent_id is NULL', (categs[0],)) + c_id = cr.fetchone() + if not c_id: + cr.execute('INSERT INTO ir_module_category (name, parent_id) VALUES (%s, %s) RETURNING id', (categs[0], p_id)) + c_id = cr.fetchone()[0] + else: + c_id = c_id[0] + p_id = c_id + categs = categs[1:] + self.write(cr, uid, [mod_browse.id], {'category_id': p_id}) def update_translations(self, cr, uid, ids, filter_lang=None, context=None): logger = logging.getLogger('i18n') diff --git a/bin/tools/misc.py b/bin/tools/misc.py index 7a416052d21..3d7dbd8a4f4 100644 --- a/bin/tools/misc.py +++ b/bin/tools/misc.py @@ -76,20 +76,19 @@ def init_db(cr): p_id = None while categs: if p_id is not None: - cr.execute('select id \ - from ir_module_category \ - where name=%s and parent_id=%s', (categs[0], p_id)) + cr.execute('SELECT id \ + FROM ir_module_category \ + WHERE name=%s AND parent_id=%s', (categs[0], p_id)) else: - cr.execute('select id \ - from ir_module_category \ - where name=%s and parent_id is NULL', (categs[0],)) + cr.execute('SELECT id \ + FROM ir_module_category \ + WHERE name=%s AND parent_id IS NULL', (categs[0],)) c_id = cr.fetchone() if not c_id: - cr.execute('select nextval(\'ir_module_category_id_seq\')') - c_id = cr.fetchone()[0] - cr.execute('insert into ir_module_category \ + cr.execute('INSERT INTO ir_module_category \ (id, name, parent_id) \ - values (%s, %s, %s)', (c_id, categs[0], p_id)) + VALUES (%s, %s) RETURNING id', (categs[0], p_id)) + c_id = cr.fetchone()[0] else: c_id = c_id[0] p_id = c_id @@ -104,23 +103,23 @@ def init_db(cr): state = 'uninstalled' else: state = 'uninstallable' - cr.execute('select nextval(\'ir_module_module_id_seq\')') - id = cr.fetchone()[0] - cr.execute('insert into ir_module_module \ - (id, author, website, name, shortdesc, description, \ - category_id, state, certificate, web) \ - values (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)', ( - id, info.get('author', ''), + cr.execute('INSERT INTO ir_module_module \ + (author, website, name, shortdesc, description, \ + category_id, state, certificate, web, license) \ + VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s) RETURNING id', ( + info.get('author', ''), info.get('website', ''), i, info.get('name', False), info.get('description', ''), p_id, state, info.get('certificate') or None, - info.get('web') or False)) - cr.execute('insert into ir_model_data \ - (name,model,module, res_id, noupdate) values (%s,%s,%s,%s,%s)', ( + info.get('web') or False, + info.get('license') or 'AGPL-3')) + id = cr.fetchone()[0] + cr.execute('INSERT INTO ir_model_data \ + (name,model,module, res_id, noupdate) VALUES (%s,%s,%s,%s,%s)', ( 'module_meta_information', 'ir.module.module', i, id, True)) dependencies = info.get('depends', []) for d in dependencies: - cr.execute('insert into ir_module_module_dependency \ - (module_id,name) values (%s, %s)', (id, d)) + cr.execute('INSERT INTO ir_module_module_dependency \ + (module_id,name) VALUES (%s, %s)', (id, d)) cr.commit() def find_in_path(name):