[MERGE] Fixed expression builder + code cosmetic/naming.

bzr revid: jth@openerp.com-20101005142749-9fuvum0j1m20jaws
This commit is contained in:
Julien Thewys 2010-10-05 16:27:49 +02:00
commit f921e61f29
1 changed files with 29 additions and 33 deletions

View File

@ -331,38 +331,34 @@ This is useful for CRM leads for example"),
default.update({'name':new_name}) default.update({'name':new_name})
return super(email_template, self).copy(cr, uid, id, default, context) return super(email_template, self).copy(cr, uid, id, default, context)
def compute_pl(self, def build_expression(self, field_name, sub_field_name, null_value, template_language='mako'):
model_object_field,
sub_model_object_field,
null_value, template_language='mako'):
""" """
Returns the expression based on data provided Returns a template expression based on data provided
@param model_object_field: First level field @param field_name: field name
@param sub_model_object_field: Second level drilled down field (M2O) @param sub_field_name: sub field name (M2O)
@param null_value: What has to be returned if the value is empty @param null_value: default value if the target value is empty
@param template_language: The language used for templating @param template_language: name of template engine
@return: computed expression @return: computed expression
""" """
#Configure for MAKO
copy_val = '' expression = ''
if template_language == 'mako': if template_language == 'mako':
if model_object_field: if field_name:
copy_val = "${object." + model_object_field expression = "${object." + field_name
if sub_model_object_field: if sub_field_name:
copy_val += "." + sub_model_object_field expression += "." + sub_field_name
if null_value: if null_value:
copy_val += " or '" + null_value + "'" expression += " or '''%s'''" % null_value
if model_object_field: expression += "}"
copy_val += "}"
elif template_language == 'django': elif template_language == 'django':
if model_object_field: if field_name:
copy_val = "{{object." + model_object_field expression = "{{object." + field_name
if sub_model_object_field: if sub_field_name:
copy_val += "." + sub_model_object_field expression += "." + sub_field_name
if null_value: if null_value:
copy_val = copy_val + '|default:"' + null_value + '"' expression += "|default: '''%s'''" % null_value
copy_val = copy_val + "}}" expression += "}}"
return copy_val return expression
def onchange_model_object_field(self, cr, uid, ids, model_object_field, template_language, context=None): def onchange_model_object_field(self, cr, uid, ids, model_object_field, template_language, context=None):
if not model_object_field: if not model_object_field:
@ -374,7 +370,7 @@ This is useful for CRM leads for example"),
res_ids = self.pool.get('ir.model').search(cr, uid, [('model', '=', field_obj.relation)], context=context) res_ids = self.pool.get('ir.model').search(cr, uid, [('model', '=', field_obj.relation)], context=context)
if res_ids: if res_ids:
result['sub_object'] = res_ids[0] result['sub_object'] = res_ids[0]
result['copyvalue'] = self.compute_pl(False, result['copyvalue'] = self.build_expression(False,
False, False,
False, False,
template_language) template_language)
@ -383,7 +379,7 @@ This is useful for CRM leads for example"),
else: else:
#Its a simple field... just compute placeholder #Its a simple field... just compute placeholder
result['sub_object'] = False result['sub_object'] = False
result['copyvalue'] = self.compute_pl(field_obj.name, result['copyvalue'] = self.build_expression(field_obj.name,
False, False,
False, False,
template_language template_language
@ -402,7 +398,7 @@ This is useful for CRM leads for example"),
sub_field_obj = self.pool.get('ir.model.fields').browse(cr, uid, sub_model_object_field, context) sub_field_obj = self.pool.get('ir.model.fields').browse(cr, uid, sub_model_object_field, context)
if res_ids: if res_ids:
result['sub_object'] = res_ids[0] result['sub_object'] = res_ids[0]
result['copyvalue'] = self.compute_pl(field_obj.name, result['copyvalue'] = self.build_expression(field_obj.name,
sub_field_obj.name, sub_field_obj.name,
False, False,
template_language template_language
@ -412,7 +408,7 @@ This is useful for CRM leads for example"),
else: else:
#Its a simple field... just compute placeholder #Its a simple field... just compute placeholder
result['sub_object'] = False result['sub_object'] = False
result['copyvalue'] = self.compute_pl(field_obj.name, result['copyvalue'] = self.build_expression(field_obj.name,
False, False,
False, False,
template_language template_language
@ -431,7 +427,7 @@ This is useful for CRM leads for example"),
sub_field_obj = self.pool.get('ir.model.fields').browse(cr, uid, sub_model_object_field, context) sub_field_obj = self.pool.get('ir.model.fields').browse(cr, uid, sub_model_object_field, context)
if res_ids: if res_ids:
result['sub_object'] = res_ids[0] result['sub_object'] = res_ids[0]
result['copyvalue'] = self.compute_pl(field_obj.name, result['copyvalue'] = self.build_expression(field_obj.name,
sub_field_obj.name, sub_field_obj.name,
null_value, null_value,
template_language template_language
@ -441,7 +437,7 @@ This is useful for CRM leads for example"),
else: else:
#Its a simple field... just compute placeholder #Its a simple field... just compute placeholder
result['sub_object'] = False result['sub_object'] = False
result['copyvalue'] = self.compute_pl(field_obj.name, result['copyvalue'] = self.build_expression(field_obj.name,
False, False,
null_value, null_value,
template_language template_language