Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions poweremail_send_wizard.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,17 @@ def _get_template(self, cr, uid, context=None):

def _get_template_value(self, cr, uid, field, context=None):
template = self._get_template(cr, uid, context)
if not template:
return False
if len(context['src_rec_ids']) > 1: # Multiple Mail: Gets original template values for multiple email change
return getattr(template, field)
else: # Simple Mail: Gets computed template values
return self.get_value(cr, uid, template, getattr(template, field), context)
res = False
if template:
if len(context['src_rec_ids']) > 1: # Multiple Mail: Gets original template values for multiple email change
res = getattr(template, field)
else: # Simple Mail: Gets computed template values
template_o = self.pool.get('poweremail.templates')
record_id = context['src_rec_ids'][0]
res = template_o.get_values_from_template(
cr, uid, template.id, record_id, [field], context=context
)[field]
return res

_columns = {
'state':fields.selection([
Expand Down Expand Up @@ -177,14 +182,15 @@ def _get_template_value(self, cr, uid, field, context=None):
'bcc': lambda self,cr,uid,ctx: filter_send_emails(self._get_template_value(cr, uid, 'def_bcc', ctx)),
'subject':lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'def_subject', ctx),
'body_text':lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'def_body_text', ctx),
'body_html':lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'def_body_html', ctx),
# 'body_html':lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'def_body_html', ctx),
'report': lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'file_name', ctx),
'signature': lambda self,cr,uid,ctx: self._get_template(cr, uid, ctx).use_sign,
'ref_template':lambda self,cr,uid,ctx: self._get_template(cr, uid, ctx).id,
'requested':lambda self,cr,uid,ctx: len(ctx['src_rec_ids']),
'full_success': lambda *a: False,
'single_email':lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'single_email', ctx),
'priority': lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'def_priority', ctx),
# TODO
# 'single_email':lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'single_email', ctx),
'priority': lambda self,cr,uid,ctx: self._get_template_value(cr, uid, 'priority', ctx),
}


Expand Down Expand Up @@ -212,7 +218,8 @@ def compute_second_step(self, cr, uid, ids, context=None):
'subject': self._get_template_value(cr, uid, 'def_subject', ctx),
'body_text': self._get_template_value(cr, uid, 'def_body_text', ctx),
'body_html': self._get_template_value(cr, uid, 'def_body_html', ctx),
'report': self._get_template_value(cr, uid, 'file_name', ctx),
# TODO
# 'report': self._get_template_value(cr, uid, 'file_name', ctx),
'signature': self._get_template(cr, uid, ctx).use_sign,
'ref_template': self._get_template(cr, uid, ctx).id,
'state': 'single',
Expand Down
114 changes: 80 additions & 34 deletions poweremail_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,85 @@ class poweremail_templates(osv.osv):
_name = "poweremail.templates"
_description = 'Power Email Templates for Models'

def get_value_from_report_template(self, cursor, uid, template, record_id, context=None):
return get_value(cursor, uid, record_id, template.def_body_text, template, context=context),

def get_mailbox_values(self, cursor, uid, template_id, record_id, context=None):
if context is None:
context = {}
mailbox_template_fieldmap = {
'def_to': 'pem_to',
'def_cc': 'pem_cc',
'def_bcc': 'pem_bcc',
'def_subject': 'pem_subject',
'def_priority': 'priority',
'def_body_text': 'pem_body_text',
}
res = self.get_values_from_template(
cursor, uid, template_id, record_id, template_fields=mailbox_template_fieldmap.keys(),
context=context
)
for tmpl_field, pem_field in mailbox_template_fieldmap.items():
res[pem_field] = res.pop(tmpl_field)

from_account = self.get_from_account_id_from_template(
cursor, uid, template_id, context=context
)

res.update({
'pem_from': tools.ustr(from_account['name']) + "<" + tools.ustr(from_account['email_id']) + ">",
'pem_account_id': from_account['id'],
'state': 'na',
'folder': 'drafts',
'mail_type': 'multipart/alternative',
'template_id': template_id,
})
res.update(context.get("extra_vals", {}))
return res

def get_values_from_template(self, cursor, uid, template_id, record_id, template_fields=None, context=None):
if context is None:
context = {}
if template_fields is None:
template_fields = [
'def_to', 'def_cc', 'def_bcc', 'def_subject',
'def_body_text', 'def_account_id', 'file_name', 'partner_event',
# TODO
# 'report', 'partner_event', 'single_email'
]

user_o = self.pool.get('res.users')

ctx = context.copy()
ctx['prefetch'] = False

template = self.browse(cursor, uid, template_id, context=ctx)
lang = get_value(cursor, uid, record_id, template.lang, template, context=context)
if lang:
ctx = context.copy()
ctx.update({'lang': lang})
template = self.browse(cursor, uid, template_id, context=ctx)

res = {}
for field in template_fields:
if field == 'def_body_text':
res[field] = self.get_value_from_report_template(cursor, uid, template, record_id, context=context)
else:
res[field] = get_value(
cursor, uid, record_id, getattr(template, field), template,
context=context
)
# Use signatures if allowed
if template.use_sign and field in ('def_body_text', 'def_body_html'):
sign = user_o.read(cursor, uid, uid, ['signature'], context=context)['signature']
if sign:
if res['def_body_text']:
res['def_body_text'] += "\n--\n"+sign
if res['def_body_html']:
res['def_body_html'] += sign

return res

def _get_model_name(
self, cursor, uid, template_ids, field_name, arg, context=None):
res = {}
Expand Down Expand Up @@ -950,40 +1029,7 @@ def _generate_mailbox_item_from_template(self, cursor, user, template, record_id
if context is None:
context = {}
mailbox_obj = self.pool.get('poweremail.mailbox')
users_obj = self.pool.get('res.users')

from_account = self.get_from_account_id_from_template(cursor, user, template.id, context=context)

lang = get_value(cursor, user, record_id, template.lang, template, context=context)
if lang:
ctx = context.copy()
ctx.update({'lang': lang})
template = self.browse(cursor, user, template.id, context=ctx)
mailbox_values = {
'pem_from': tools.ustr(from_account['name']) + "<" + tools.ustr(from_account['email_id']) + ">",
'pem_to': get_value(cursor, user, record_id, template.def_to, template, context=context),
'pem_cc': get_value(cursor, user, record_id, template.def_cc, template, context=context),
'pem_bcc': get_value(cursor, user, record_id, template.def_bcc, template, context=context),
'pem_subject': get_value(cursor, user, record_id, template.def_subject, template, context=context),
'pem_body_text': get_value(cursor, user, record_id, template.def_body_text, template, context=context),
'pem_body_html': get_value(cursor, user, record_id, template.def_body_html, template, context=context),
'pem_account_id': from_account['id'],
#This is a mandatory field when automatic emails are sent
'state': 'na',
'folder': 'drafts',
'mail_type': 'multipart/alternative',
'priority': template.def_priority,
'template_id': template.id,
}
#Use signatures if allowed
if template.use_sign:
sign = users_obj.read(cursor, user, user, ['signature'], context=context)['signature']
if sign:
if mailbox_values['pem_body_text']:
mailbox_values['pem_body_text'] += "\n--\n"+sign
if mailbox_values['pem_body_html']:
mailbox_values['pem_body_html'] += sign
mailbox_values.update(context.get("extra_vals", {}))
mailbox_values = self.get_mailbox_values(cursor, user, template.id, record_id, context=context)
mailbox_id = mailbox_obj.create(cursor, user, mailbox_values, context=context)
return mailbox_id

Expand Down
75 changes: 26 additions & 49 deletions wizard/wizard_poweremail_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,37 @@ def _ref_models(self, cursor, uid, context=None):
'state': lambda *a: 'init',
}

def render_body_text(self, cursor, uid, template, record_id, context=None):
return get_value(cursor, uid, record_id, template.def_body_text, template, context)

def on_change_ref(self, cr, uid, ids, model_ref, context=None):
if context is None:
context = {}
if not model_ref:
return {}
template_o = self.pool.get('poweremail.templates')
vals = {}
model_name, record_id = model_ref.split(',')
record_id = int(record_id)
template = self.pool.get('poweremail.templates').browse(cr, uid, context['active_id'], context=context)
# Search translated template
lang = get_value(cr, uid, record_id, template.lang, template, context)
if lang:
ctx = context.copy()
ctx.update({'lang':lang})
template = self.pool.get('poweremail.templates').browse(cr, uid, context['active_id'], ctx)
vals['to'] = get_value(cr, uid, record_id, template.def_to, template, context)
vals['cc'] = get_value(cr, uid, record_id, template.def_cc, template, context)
vals['bcc'] = get_value(cr, uid, record_id, template.def_bcc, template, context)
vals['subject'] = get_value(cr, uid, record_id, template.def_subject, template, context)
vals['body_text'] = get_value(cr, uid, record_id, template.def_body_text, template, context)
vals['body_html'] = get_value(cr, uid, record_id, template.def_body_html, template, context)
vals['report'] = get_value(cr, uid, record_id, template.file_name, template, context)
return {'value': vals}
res = {}
if record_id:
template_id = context.get('active_id')
if not template_id:
raise osv.except_osv('Error !', 'active_id missing from context')

mailbox_values = template_o.get_mailbox_values(
cr, uid, template_id, record_id, context=context
)

vals['to'] = mailbox_values['pem_to']
vals['cc'] = mailbox_values['pem_cc']
vals['bcc'] = mailbox_values['pem_bcc']
vals['subject'] = mailbox_values['pem_subject']
vals['body_text'] = mailbox_values['pem_body_text']
# vals['report'] = get_value(cr, uid, record_id, template.file_name, template, context)
res = {'value': vals}

return res

def action_generate_static_mail(self, cursor, uid, ids, context=None):
if context is None:
Expand All @@ -95,41 +103,10 @@ def action_generate_static_mail(self, cursor, uid, ids, context=None):

mailbox_ids = []
for template_id in template_ids:
template = template_obj.browse(cursor, uid, template_id,
context=context)
if not template:
raise Exception("The requested template could not be loaded")

from_account = template_obj.get_from_account_id_from_template(
cursor, uid, template.id, context=context
)

# Evaluates an expression and returns its value
# recid: ID of the target record under evaluation
# message: The expression to be evaluated
# template: BrowseRecord object of the current template
# return: Computed message (unicode) or u""
body_text = get_value(
cursor, uid, model_id, message=template.def_body_text,
template=template, context=context
template = template_obj.browse(cursor, uid, template_id, context=context)
mailbox_id = template_obj._generate_mailbox_item_from_template(
cursor, uid, template, model_id, context=context
)

mail_vals = {
'pem_from': tools.ustr(from_account['name']) + \
"<" + tools.ustr(from_account['email_id']) + ">",
'pem_to': template.def_to,
'pem_cc': False,
'pem_bcc': False,
'pem_subject': template.name,
'pem_body_text': body_text,
'pem_account_id': from_account['id'],
'priority': '1',
'state': 'na',
'mail_type': 'multipart/alternative',
'template_id': template_id
}

mailbox_id = mailbox_obj.create(cursor, uid, mail_vals)
mailbox_ids.append(mailbox_id)

wizard.write({'state': 'end'}, context=context)
Expand Down