[IMP] code style improvements

* Remove unused variables
* Simplify dict.get call in conditionals (default is None)
* Simplify dict.update calls (use kwargs)
* Merge dict.copy() with dict.update() as dict()
* Use isinstance instead of type() equality tests
* Simplify deeply nested code: merge conditionals and remove useless
  conditional, use dict.iteritems()
* Log traceback in exception handler logging

bzr revid: xmo@openerp.com-20120806071249-heh32pi1v0qd3m2j
This commit is contained in:
Xavier Morel 2012-08-06 09:12:49 +02:00
parent 0d2d7ee585
commit 76f885299f
1 changed files with 24 additions and 25 deletions

View File

@ -178,14 +178,16 @@ class ir_model(osv.osv):
def create(self, cr, user, vals, context=None): def create(self, cr, user, vals, context=None):
if context is None: if context is None:
context = {} context = {}
if context and context.get('manual',False): if context and context.get('manual'):
vals['state']='manual' vals['state']='manual'
res = super(ir_model,self).create(cr, user, vals, context) res = super(ir_model,self).create(cr, user, vals, context)
if vals.get('state','base')=='manual': if vals.get('state','base')=='manual':
self.instanciate(cr, user, vals['model'], context) self.instanciate(cr, user, vals['model'], context)
self.pool.get(vals['model']).__init__(self.pool, cr) self.pool.get(vals['model']).__init__(self.pool, cr)
ctx = context.copy() ctx = dict(context,
ctx.update({'field_name':vals['name'],'field_state':'manual','select':vals.get('select_level','0')}) field_name=vals['name'],
field_state='manual',
select=vals.get('select_level', '0'))
self.pool.get(vals['model'])._auto_init(cr, ctx) self.pool.get(vals['model'])._auto_init(cr, ctx)
#pooler.restart_pool(cr.dbname) #pooler.restart_pool(cr.dbname)
return res return res
@ -335,8 +337,11 @@ class ir_model_fields(osv.osv):
if self.pool.get(vals['model']): if self.pool.get(vals['model']):
self.pool.get(vals['model']).__init__(self.pool, cr) self.pool.get(vals['model']).__init__(self.pool, cr)
#Added context to _auto_init for special treatment to custom field for select_level #Added context to _auto_init for special treatment to custom field for select_level
ctx = context.copy() ctx = dict(context,
ctx.update({'field_name':vals['name'],'field_state':'manual','select':vals.get('select_level','0'),'update_custom_fields':True}) field_name=vals['name'],
field_state='manual',
select=vals.get('select_level', '0'),
update_custom_fields=True)
self.pool.get(vals['model'])._auto_init(cr, ctx) self.pool.get(vals['model'])._auto_init(cr, ctx)
return res return res
@ -446,8 +451,8 @@ class ir_model_fields(osv.osv):
# was called earlier, they will be in-sync before the _auto_init. # was called earlier, they will be in-sync before the _auto_init.
# Anything we don't update in _columns now will be reset from # Anything we don't update in _columns now will be reset from
# the model into ir.model.fields (db). # the model into ir.model.fields (db).
ctx = context.copy() ctx = dict(context, select=vals.get('select_level', '0'),
ctx.update({'select': vals.get('select_level','0'),'update_custom_fields':True}) update_custom_fields=True)
for __, patch_struct in models_patch.items(): for __, patch_struct in models_patch.items():
obj = patch_struct[0] obj = patch_struct[0]
@ -825,20 +830,16 @@ class ir_model_data(osv.osv):
'res_id': inherit_id.id, 'res_id': inherit_id.id,
'noupdate': noupdate, 'noupdate': noupdate,
},context=context) },context=context)
if xml_id: if xml_id and res_id:
if res_id: self.loads[(module, xml_id)] = (model, res_id)
self.loads[(module, xml_id)] = (model, res_id) for table, inherit_field in model_obj._inherits.iteritems():
if model_obj._inherits: inherit_id = model_obj.read(cr, uid, res_id,
for table in model_obj._inherits: [inherit_field])[inherit_field]
inherit_field = model_obj._inherits[table] self.loads[(module, xml_id + '_' + table.replace('.', '_'))] = (table, inherit_id)
inherit_id = model_obj.read(cr, uid, res_id,
[inherit_field])[inherit_field]
self.loads[(module, xml_id + '_' + \
table.replace('.', '_'))] = (table, inherit_id)
return res_id return res_id
def ir_set(self, cr, uid, key, key2, name, models, value, replace=True, isobject=False, meta=None, xml_id=False): def ir_set(self, cr, uid, key, key2, name, models, value, replace=True, isobject=False, meta=None, xml_id=False):
if type(models[0])==type([]) or type(models[0])==type(()): if isinstance(models[0], (list, tuple)):
model,res_id = models[0] model,res_id = models[0]
else: else:
res_id=None res_id=None
@ -858,7 +859,7 @@ class ir_model_data(osv.osv):
res = cr.fetchone() res = cr.fetchone()
if not res: if not res:
ir_values_obj = pooler.get_pool(cr.dbname).get('ir.values') ir_values_obj = pooler.get_pool(cr.dbname).get('ir.values')
res = ir_values_obj.set(cr, uid, key, key2, name, models, value, replace, isobject, meta) ir_values_obj.set(cr, uid, key, key2, name, models, value, replace, isobject, meta)
elif xml_id: elif xml_id:
cr.execute('UPDATE ir_values set value=%s WHERE model=%s and key=%s and name=%s'+where,(value, model, key, name)) cr.execute('UPDATE ir_values set value=%s WHERE model=%s and key=%s and name=%s'+where,(value, model, key, name))
return True return True
@ -890,8 +891,6 @@ class ir_model_data(osv.osv):
for data in self.browse(cr, uid, ids, context): for data in self.browse(cr, uid, ids, context):
model = data.model model = data.model
res_id = data.res_id res_id = data.res_id
model_obj = self.pool.get(model)
name = tools.ustr(data.name)
pair_to_unlink = (model, res_id) pair_to_unlink = (model, res_id)
if pair_to_unlink not in to_unlink: if pair_to_unlink not in to_unlink:
@ -909,19 +908,19 @@ class ir_model_data(osv.osv):
for model,res_id in wkf_todo: for model,res_id in wkf_todo:
try: try:
wf_service.trg_write(uid, model, res_id, cr) wf_service.trg_write(uid, model, res_id, cr)
except: except Exception:
_logger.info('Unable to force processing of workflow for item %s@%s in order to leave activity to be deleted', res_id, model) _logger.info('Unable to force processing of workflow for item %s@%s in order to leave activity to be deleted', res_id, model, exc_info=True)
def unlink_if_refcount(to_unlink): def unlink_if_refcount(to_unlink):
for model, res_id in to_unlink: for model, res_id in to_unlink:
external_ids = self.search(cr, uid, [('model', '=', model),('res_id', '=', res_id)]) external_ids = self.search(cr, uid, [('model', '=', model),('res_id', '=', res_id)])
if (set(external_ids)-ids_set): if set(external_ids)-ids_set:
# if other modules have defined this record, we must not delete it # if other modules have defined this record, we must not delete it
continue continue
_logger.info('Deleting %s@%s', res_id, model) _logger.info('Deleting %s@%s', res_id, model)
try: try:
self.pool.get(model).unlink(cr, uid, [res_id], context=context) self.pool.get(model).unlink(cr, uid, [res_id], context=context)
except: except Exception:
_logger.info('Unable to delete %s@%s', res_id, model, exc_info=True) _logger.info('Unable to delete %s@%s', res_id, model, exc_info=True)
# Remove non-model records first, then model fields, and finish with models # Remove non-model records first, then model fields, and finish with models