[REF] crm_partner_assign: refactored code a bit, following cleaning of mail, because it inherit from mail.compose.message. Removed send_to field, as the new composer is based on partners and simplify this wizard; removed a lot of code about setting email_from, email_to, ... because this should be managed by the other layers of the mail module. Cleaned the code in a more general way to remove weird things. Updated releveant form view, especially about context on Forward button, and the form view of the wizard that has been much simplified.

bzr revid: tde@openerp.com-20120824142000-0jbgvgdiip5475yp
This commit is contained in:
Thibault Delavallée 2012-08-24 16:20:00 +02:00
parent fb76f9f037
commit d91979671f
4 changed files with 124 additions and 167 deletions

View File

@ -19,8 +19,7 @@
attrs="{'invisible':[('partner_assigned_id','=',False)]}"
name="%(crm_lead_forward_to_partner_act)d"
icon="terp-mail-forward" type="action"
context="{'default_name': 'partner', 'default_partner_id': partner_assigned_id}"
/>
context="{'mail.compose.message.mode': 'forward', 'default_partner_ids': [partner_assigned_id]}"/>
</div>
</group>
<group string="Geo Assignation">
@ -37,8 +36,6 @@
</field>
</record>
<record id="view_crm_opportunity_geo_assign_tree" model="ir.ui.view">
<field name="name">crm.lead.geo_assign.tree.inherit</field>
<field name="model">crm.lead</field>
@ -49,6 +46,7 @@
</field>
</field>
</record>
<record model="ir.ui.view" id="crm_opportunity_partner_filter">
<field name="name">crm.opportunity.partner.filter.assigned</field>
<field name="model">crm.lead</field>
@ -63,5 +61,6 @@
</field>
</field>
</record>
</data>
</openerp>

View File

@ -29,8 +29,8 @@
I forward this opportunity to its nearest partner.
-
!python {model: crm.lead.forward.to.partner}: |
context.update({'active_model': 'crm.lead', 'active_id': ref('crm.crm_case_19'), 'active_ids': [ref('crm.crm_case_19')]})
forward_id = self.create(cr, uid, {'email_from': 'test@openerp.com', 'send_to': 'partner'}, context=context)
context.update({'default_model': 'crm.lead', 'default_res_id': ref('crm.crm_case_19'), 'active_ids': [ref('crm.crm_case_19')]})
forward_id = self.create(cr, uid, {}, context=context)
try:
self.action_forward(cr, uid, [forward_id], context=context)
except:

View File

@ -28,107 +28,110 @@ from osv import osv, fields
from tools.translate import _
class crm_lead_forward_to_partner(osv.osv_memory):
"""Forwards lead history"""
""" Forward info history to partners. """
_name = 'crm.lead.forward.to.partner'
_inherit = "mail.compose.message"
def default_get(self, cr, uid, fields, context=None):
if context is None:
context = {}
# set as comment, perform overrided document-like action that calls get_record_data
old_mode = context.get('mail.compose.message.mode', 'forward')
context['mail.compose.message.mode'] = 'comment'
res = super(crm_lead_forward_to_partner, self).default_get(cr, uid, fields, context=context)
# back to forward mode
context['mail.compose.message.mode'] = old_mode
res['composition_mode'] = context['mail.compose.message.mode']
return res
def _get_composition_mode_selection(self, cr, uid, context=None):
composition_mode = super(crm_lead_forward_to_partner, self)._get_composition_mode_selection(cr, uid, context=context)
composition_mode.append(('forward', 'Forward'))
return composition_mode
_columns = {
'send_to': fields.selection([('user', 'User'), ('partner', 'Partner'), \
('email', 'Email Address')], 'Send to', required=True),
'user_id': fields.many2one('res.users', "User"),
'attachment_ids': fields.many2many('ir.attachment','lead_forward_to_partner_attachment_rel', 'wizard_id', 'attachment_id', 'Attachments'),
'partner_id' : fields.many2one('res.partner', 'Partner'),
'history': fields.selection([('info', 'Case Information'), ('latest', 'Latest email'), ('whole', 'Whole Story')], 'Send history', required=True),
'partner_ids': fields.many2many('res.partner',
'lead_forward_to_partner_res_partner_rel',
'wizard_id', 'partner_id', 'Additional contacts'),
'attachment_ids': fields.many2many('ir.attachment',
'lead_forward_to_partner_attachment_rel',
'wizard_id', 'attachment_id', 'Attachments'),
'history_mode': fields.selection([('info', 'Case Information'),
('latest', 'Latest email'), ('whole', 'Whole Story')],
'Send history', required=True),
}
_defaults = {
'send_to' : 'email',
'history': 'latest',
'history_mode': 'latest',
'content_subtype': lambda self,cr, uid, context={}: 'html',
}
def on_change_email(self, cr, uid, ids, user, context=None):
if not user:
return {'value': {'email_to': False}}
return {'value': {'email_to': self.pool.get('res.users').browse(cr, uid, uid, context=context).email}}
def on_change_history(self, cr, uid, ids, history_type, context=None):
"""Gives message body according to type of history selected
* info: Forward the case information
* whole: Send the whole history
* latest: Send the latest histoy
def get_record_data(self, cr, uid, model, res_id, context=None):
""" Override of mail.compose.message, to add default values coming
form the related lead.
"""
#TODO: ids and context are not comming
res = {}
res_id = context.get('active_id')
model = context.get('active_model')
lead = self.pool.get(model).browse(cr, uid, res_id, context)
body = self._get_message_body(cr, uid, lead, history_type, context=context)
if body:
res = {'value': {'body' : body}}
res = super(crm_lead_forward_to_partner, self).get_record_data(cr, uid, model, res_id, context=context)
if model not in ('crm.lead') or not res_id:
return res
lead_obj = self.pool.get(model)
lead = lead_obj.browse(cr, uid, res_id, context=context)
subject = '%s: %s - %s' % (_('Fwd'), _('Lead forward'), lead.name)
body = self._get_message_body(cr, uid, lead, 'info', context=context)
res.update({
'subject': subject,
'body': body,
})
return res
def on_change_history_mode(self, cr, uid, ids, history_mode, model, res_id, context=None):
""" Update body when changing history_mode """
if model and model == 'crm.lead' and res_id:
lead = self.pool.get(model).browse(cr, uid, res_id, context=context)
body = self._get_message_body(cr, uid, lead, history_mode, context=context)
return {'value': {'body': body}}
def on_change_partners(self, cr, uid, ids, partner_id):
"""This function fills address information based on partner/user selected
"""
def on_change_partner_ids(self, cr, uid, ids, partner_ids, context=None):
""" TDE-TODO: Keep void; maybe we could check that partner_ids have
email defined. """
return {}
# if not partner_id:
# return {'value' : {'email_to' : False}}
# partner_obj = self.pool.get('res.partner')
# data = {}
# partner = partner_obj.browse(cr, uid, [partner_id])
# user_id = partner and partner[0].user_id or False
# data.update({'email_from': partner and partner[0].email or "",
# 'email_cc' : user_id and user_id.user or '',
# 'user_id': user_id and user_id.id or False})
# return {'value' : data}
def create(self, cr, uid, values, context=None):
""" TDE-HACK: remove 'type' from context, because when viewing an
opportunity form view, a default_type is set and propagated
to the wizard, that has a not matching type field. """
default_type = context.pop('default_type', None)
new_id = super(crm_lead_forward_to_partner, self).create(cr, uid, values, context=context)
if default_type:
context['default_type'] = default_type
return new_id
def action_forward(self, cr, uid, ids, context=None):
"""
Forward the lead to a partner
Forward the lead to a partner
"""
if context is None:
context = {}
res = {'type': 'ir.actions.act_window_close'}
model = context.get('active_model')
if model not in ('crm.lead'):
wizard = self.browse(cr, uid, ids[0], context=context)
if wizard.model not in ('crm.lead'):
return res
this = self.browse(cr, uid, ids[0], context=context)
lead = self.pool.get(model)
lead_id = context and context.get('active_id', False) or False
lead_ids = lead_id and [lead_id] or []
mode = context.get('mail.compose.message.mode')
if mode == 'mass_mail':
lead = self.pool.get(wizard.model)
lead_ids = wizard.res_id and [wizard.res_id] or []
if wizard.composition_mode == 'mass_mail':
lead_ids = context and context.get('active_ids', []) or []
value = self.default_get(cr, uid, ['body', 'email_to', 'email_cc', 'subject', 'history'], context=context)
value = self.default_get(cr, uid, ['body', 'email_to', 'email_cc', 'subject', 'history_mode'], context=context)
self.write(cr, uid, ids, value, context=context)
context['mail.compose.message.mode'] = mode
self.send_mail(cr, uid, ids, context=context)
for case in lead.browse(cr, uid, lead_ids, context=context):
if (this.send_to == 'partner' and this.partner_id):
lead.assign_partner(cr, uid, [case.id], this.partner_id.id, context=context)
# for case in lead.browse(cr, uid, lead_ids, context=context):
# TDE: WHAT TO DO WITH THAT ?
# if (this.send_to == 'partner' and this.partner_id):
# lead.assign_partner(cr, uid, [case.id], this.partner_id.id, context=context)
if this.send_to == 'user':
lead.allocate_salesman(cr, uid, [case.id], [this.user_id.id], context=context)
email_cc = tools.email_split(case.email_cc)
email_cc = email_cc and email_cc[0] or ''
new_cc = []
if email_cc:
new_cc.append(email_cc)
for to in this.email_to.split(','):
email_to = tools.email_split(to)
email_to = email_to and email_to[0] or ''
if email_to not in new_cc:
new_cc.append(to)
update_vals = {'email_cc' : ', '.join(new_cc) }
lead.write(cr, uid, [case.id], update_vals, context=context)
# if this.send_to == 'user':
# lead.allocate_salesman(cr, uid, [case.id], [this.user_id.id], context=context)
return res
def _get_info_body(self, cr, uid, lead, context=None):
@ -137,75 +140,40 @@ class crm_lead_forward_to_partner(osv.osv_memory):
if lead.type == 'opportunity':
field_names += ['partner_id']
field_names += [
'partner_name' , 'title', 'function', 'street', 'street2',
'partner_name' , 'title', 'function', 'street', 'street2',
'zip', 'city', 'country_id', 'state_id', 'email_from',
'phone', 'fax', 'mobile', 'categ_id', 'description',
]
return proxy._mail_body(cr, uid, lead, field_names, context=context)
def _get_message_body(self, cr, uid, lead, mode='whole', context=None):
"""This function gets whole communication history and returns as top posting style
def _get_message_body(self, cr, uid, lead, history_mode='whole', context=None):
""" This function gets whole communication history and returns as top
posting style
#1: form a body, based on the lead
#2: append to the body the communication history, based on the
history_mode parameter
- info: Forward the case information
- latest: Send the latest history
- whole: Send the whole history
:param lead: browse_record on crm.lead
:param history_mode: 'whole' or 'latest'
"""
mail_message = self.pool.get('mail.message')
message_ids = []
body = self._get_info_body(cr, uid, lead, context=context)
if mode in ('whole', 'latest'):
message_ids = lead.message_ids
message_ids = map(lambda x: x.id, filter(lambda x: x.email_from, message_ids))
if mode == 'latest' and len(message_ids):
message_ids = [message_ids[0]]
for message in mail_message.browse(cr, uid, message_ids, context=context):
header = '-------- Original Message --------'
sender = 'From: %s' %(message.email_from or '')
to = 'To: %s' % (message.email_to or '')
sentdate = 'Date: %s' % (message.date or '')
desc = '\n%s'%(message.body)
original = [header, sender, to, sentdate, desc, '\n']
original = '\n'.join(original)
body += original
if history_mode not in ('whole', 'latest'):
return body or ''
for message in lead.message_ids:
header = '-------- Original Message --------'
sentdate = 'Date: %s' % (message.date or '')
desc = '\n%s'%(message.body)
original = [header, sentdate, desc, '\n']
original = '\n'.join(original)
body += original
if history_mode == 'latest':
break
return body or ''
def get_value(self, cr, uid, model, res_id, context=None):
if context is None:
context = {}
res = super(crm_lead_forward_to_partner, self).get_value(cr, uid, model, res_id, context=context)
if model not in ("crm.lead"):
return res
proxy = self.pool.get(model)
partner = self.pool.get('res.partner')
lead = proxy.browse(cr, uid, res_id, context=context)
mode = context.get('mail.compose.message.mode')
if mode == "forward":
body_type = context.get('mail.compose.message.body')
email_cc = res.get('email_cc', "")
email = res.get('email_to', "")
subject = '%s: %s - %s' % (_('Fwd'), 'Lead forward', lead.name)
body = self._get_message_body(cr, uid, lead, body_type, context=context)
partner_assigned_id = lead.partner_assigned_id and lead.partner_assigned_id.id or False
user_id = False
if not partner_assigned_id:
partner_assigned_id = proxy.search_geo_partner(cr, uid, [lead.id], context=None).get(lead.id, False)
if partner_assigned_id:
assigned_partner = partner.browse(cr, uid, partner_assigned_id, context=context)
user_id = assigned_partner.user_id and assigned_partner.user_id.id or False
email_cc = assigned_partner.user_id and assigned_partner.user_id.email or ''
email = assigned_partner.email
res.update({
'subject' : subject,
'body' : body,
'email_cc' : email_cc,
'email_to' : email,
'partner_assigned_id': partner_assigned_id,
'user_id': user_id,
})
return res
def default_get(self, cr, uid, fields, context=None):
if context is None:
context = {}
context['mail.compose.message.mode'] = 'forward'
context['mail.compose.message.body'] = 'info'
return super(crm_lead_forward_to_partner, self).default_get(cr, uid, fields, context=context)
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -7,37 +7,31 @@
<field name="model">crm.lead.forward.to.partner</field>
<field name="arch" type="xml">
<form string="Send Mail" version="7.0">
<field name="composition_mode" colspan="2" nolabel="1" invisible="1"/>
<field name="model" colspan="2" nolabel="1" invisible="1"/>
<field name="res_id" colspan="2" nolabel="1" invisible="1"/>
<separator string="Forward to Partner" colspan="4"/>
<group col="4" colspan="6">
<field name="history" colspan="2" on_change="on_change_history(history, context)"/>
<field name="send_to" colspan="2"/>
<group col="2" colspan="2" attrs="{ 'invisible' : [('send_to','!=','user')]}">
<field name="user_id"
attrs="{ 'required' : [('send_to','=','user')]}"
on_change="on_change_email(user_id)"/>
</group>
<group col="4" colspan="4" attrs="{'invisible' : [('send_to','!=','partner')]}">
<field name="partner_ids" attrs="{'required' : [('send_to','=','partner')]}" on_change="on_change_partners(partner_ids)" colspan="2"/>
</group>
<group col="4">
<field name="history_mode" colspan="4"
on_change="on_change_history_mode(history_mode, model, res_id)"/>
<field name="subject" colspan="4"/>
<field name="partner_ids" colspan="4" widget="many2many_tags"
on_change="on_change_partner_ids(partner_ids)"/>
<notebook colspan="4">
<page string="Body">
<field name="body"/>
</page>
<page string="Attachments">
<field name="attachment_ids"/>
</page>
</notebook>
</group>
<separator string="" colspan="4"/>
<group col="6" colspan="4">
<field name="subject" colspan="4" widget="char" size="512"/>
</group>
<separator string="" colspan="4"/>
<notebook colspan="4">
<page string="Body">
<field name="body" colspan="4" nolabel="1"/>
</page>
<page string="Attachments">
<label string="Add here all attachments of the current document you want to include in the Email." colspan="4"/>
<field name="attachment_ids" colspan="4" nolabel="1"/>
</page>
</notebook>
<footer>
<button name="action_forward" string="Send" type="object" class="oe_highlight"/>
<button name="action_forward" string="Send" type="object"
class="oe_highlight"/>
or
<button string="Cancel" class="oe_link" special="cancel"/>
<button string="Cancel" special="cancel"
class="oe_link"/>
</footer>
</form>
</field>
@ -52,8 +46,6 @@
<field name="target">new</field>
</record>
<act_window id="action_crm_send_mass_forward"
multi="True"
key2="client_action_multi" name="Mass forward to partner"
@ -61,9 +53,7 @@
view_mode="form" target="new" view_type="form"
context="{'mail.compose.message.mode' : 'mass_mail'}"
view_id="crm_lead_forward_to_partner_form"
/>
/>
</data>
</openerp>