From 77b5911137664f177e821d78187055da0ee99d5f Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 1 Oct 2018 12:23:24 +0000 Subject: [PATCH 1/5] [IMP] core: convert_xml refactorings to prepare for improvements * add _tag_ methods for root nodes (remove some of the parse special casing) * remove unnecessary/useless returns * remove cr & uid (there's an env) * remove mode argument from callbacks (redundant and confused with self.mode) * try to cleanup menuitem callback a bit * make second callback param not optional (because it's not) --- odoo/tools/convert.py | 177 ++++++++++++++++++++---------------------- 1 file changed, 84 insertions(+), 93 deletions(-) diff --git a/odoo/tools/convert.py b/odoo/tools/convert.py index ab530f6bbd6..4363fa25de6 100644 --- a/odoo/tools/convert.py +++ b/odoo/tools/convert.py @@ -239,7 +239,7 @@ class xml_import(object): node_uid = node.get('uid','') or (len(data_node) and data_node.get('uid','')) if node_uid: return self.id_get(node_uid) - return self.uid + return self.env.user.id def make_xml_id(self, xml_id): if not xml_id or '.' in xml_id: @@ -247,7 +247,6 @@ class xml_import(object): return "%s.%s" % (self.module, xml_id) def _test_xml_id(self, xml_id): - id = xml_id if '.' in xml_id: module, id = xml_id.split('.', 1) assert '.' not in id, """The ID reference "%s" must contain @@ -257,30 +256,30 @@ form: module.record_id""" % (xml_id,) modcnt = self.env['ir.module.module'].search_count([('name', '=', module), ('state', '=', 'installed')]) assert modcnt == 1, """The ID "%s" refers to an uninstalled module""" % (xml_id,) - def _tag_delete(self, rec, data_node=None, mode=None): - d_model = rec.get("model") - d_search = rec.get("search") - d_id = rec.get("id") + def _tag_delete(self, rec, _data_node): + d_model = rec["model"] records = self.env[d_model] + d_search = rec.get("search") if d_search: idref = _get_idref(self, self.env, d_model, {}) try: records = records.search(safe_eval(d_search, idref)) except ValueError: _logger.warning('Skipping deletion for failed search `%r`', d_search, exc_info=True) - pass + + d_id = rec.get("id") if d_id: try: records += records.browse(self.id_get(d_id)) except ValueError: # d_id cannot be found. doesn't matter in this case _logger.warning('Skipping deletion for missing XML ID `%r`', d_id, exc_info=True) - pass + if records: records.unlink() - def _tag_report(self, rec, data_node=None, mode=None): + def _tag_report(self, rec, data_node): res = {} for dest,f in (('name','string'),('model','model'),('report_name','name')): res[dest] = rec.get(f) @@ -333,16 +332,15 @@ form: module.record_id""" % (xml_id,) report.unlink_action() return report.id - def _tag_function(self, rec, data_node=None, mode=None): + def _tag_function(self, rec, data_node): if self.isnoupdate(data_node) and self.mode != 'init': return context = self.get_context(data_node, rec, {'ref': self.id_get}) uid = self.get_uid(data_node, rec) env = self.env(user=uid, context=context) _eval_xml(self, rec, env) - return - def _tag_act_window(self, rec, data_node=None, mode=None): + def _tag_act_window(self, rec, data_node): name = rec.get('name') xml_id = rec.get('id','') self._test_xml_id(xml_id) @@ -357,7 +355,7 @@ form: module.record_id""" % (xml_id,) view_mode = rec.get('view_mode') or 'tree,form' usage = rec.get('usage') limit = rec.get('limit') - uid = self.uid + uid = self.env.user.id # Act_window's 'domain' and 'context' contain mostly literals # but they can also refer to the variables provided below @@ -444,72 +442,68 @@ form: module.record_id""" % (xml_id,) data = dict(xml_id=xid, values=res, noupdate=self.isnoupdate(data_node)) self.env['ir.actions.act_window']._load_records([data], self.mode == 'update') - def _tag_menuitem(self, rec, data_node=None, mode=None): - rec_id = rec.get("id") + def _tag_menuitem(self, rec, data_node): + rec_id = rec.attrib["id"] self._test_xml_id(rec_id) # The parent attribute was specified, if non-empty determine its ID, otherwise # explicitly make a top-level menu - if rec.get('parent'): - menu_parent_id = self.id_get(rec.get('parent','')) - else: - # we get here with , explicit clear of parent, or - # if no parent attribute at all but menu name is not a menu path - menu_parent_id = False - values = {'parent_id': menu_parent_id} - if rec.get('name'): - values['name'] = rec.get('name') - try: - res = [ self.id_get(rec.get('id','')) ] - except: - res = None - - if rec.get('action'): - a_action = rec.get('action') - - # determine the type of action - action_model, action_id = self.model_id_get(a_action) - action_type = action_model.split('.')[-1] # keep only type part - values['action'] = "ir.actions.%s,%d" % (action_type, action_id) - - if not values.get('name') and action_type in ('act_window', 'wizard', 'url', 'client', 'server'): - resw = self.env[action_model].sudo().browse(action_id).name - if resw: - values['name'] = resw - - if not values.get('name'): - # ensure menu has a name - values['name'] = rec_id or '?' + values = { + 'parent_id': False, + 'active': self.nodeattr2bool(rec, 'active', default=True), + } if rec.get('sequence'): values['sequence'] = int(rec.get('sequence')) - values['active'] = self.nodeattr2bool(rec, 'active', default=True) - - if rec.get('groups'): - g_names = rec.get('groups','').split(',') - groups_value = [] - for group in g_names: - if group.startswith('-'): - group_id = self.id_get(group[1:]) - groups_value.append((3, group_id)) - else: - group_id = self.id_get(group) - groups_value.append((4, group_id)) - values['groups_id'] = groups_value - - if not values.get('parent_id'): + if rec.get('parent'): + values['parent_id'] = self.id_get(rec.attrib['parent']) + else: if rec.get('web_icon'): - values['web_icon'] = rec.get('web_icon') + values['web_icon'] = rec.attrib['web_icon'] - xid = self.make_xml_id(rec_id) - data = dict(xml_id=xid, values=values, noupdate=self.isnoupdate(data_node)) + + if rec.get('name'): + values['name'] = rec.attrib['name'] + + if rec.get('action'): + a_action = rec.attrib['action'] + + if '.' not in a_action: + a_action = '%s.%s' % (self.module, a_action) + act = self.env.ref(a_action).sudo() + values['action'] = "%s,%d" % (act.type, act.id) + + if not values.get('name') and act.type.endswith(('act_window', 'wizard', 'url', 'client', 'server')) and act.name: + values['name'] = act.name + + if not values.get('name'): + values['name'] = rec_id or '?' + + + groups = [] + for group in rec.get('groups', '').split(','): + if group.startswith('-'): + group_id = self.id_get(group[1:]) + groups.append((3, group_id)) + elif group: + group_id = self.id_get(group) + groups.append((4, group_id)) + if groups: + values['groups_id'] = groups + + + data = { + 'xml_id': self.make_xml_id(rec_id), + 'values': values, + 'noupdate': self.isnoupdate(data_node), + } self.env['ir.ui.menu']._load_records([data], self.mode == 'update') def _assert_equals(self, f1, f2, prec=4): return not round(f1 - f2, prec) - def _tag_assert(self, rec, data_node=None, mode=None): + def _tag_assert(self, rec, data_node): if self.isnoupdate(data_node) and self.mode != 'init': return @@ -568,7 +562,7 @@ form: module.record_id""" % (xml_id,) else: # all tests were successful for this assertion tag (no break) self.assertion_report.record_success() - def _tag_record(self, rec, data_node=None, mode=None): + def _tag_record(self, rec, data_node): rec_model = rec.get("model") model = self.env[rec_model] rec_id = rec.get("id", '') @@ -654,10 +648,10 @@ form: module.record_id""" % (xml_id,) if rec_id: self.idref[rec_id] = record.id if config.get('import_partial'): - self.cr.commit() + self.env.cr.commit() return rec_model, record.id - def _tag_template(self, el, data_node=None, mode=None): + def _tag_template(self, el, data_node): # This helper transforms a