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

View File

@ -29,8 +29,8 @@
I forward this opportunity to its nearest partner. I forward this opportunity to its nearest partner.
- -
!python {model: crm.lead.forward.to.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')]}) 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, {'email_from': 'test@openerp.com', 'send_to': 'partner'}, context=context) forward_id = self.create(cr, uid, {}, context=context)
try: try:
self.action_forward(cr, uid, [forward_id], context=context) self.action_forward(cr, uid, [forward_id], context=context)
except: except:

View File

@ -28,107 +28,110 @@ from osv import osv, fields
from tools.translate import _ from tools.translate import _
class crm_lead_forward_to_partner(osv.osv_memory): class crm_lead_forward_to_partner(osv.osv_memory):
"""Forwards lead history""" """ Forward info history to partners. """
_name = 'crm.lead.forward.to.partner' _name = 'crm.lead.forward.to.partner'
_inherit = "mail.compose.message" _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): 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 = super(crm_lead_forward_to_partner, self)._get_composition_mode_selection(cr, uid, context=context)
composition_mode.append(('forward', 'Forward')) composition_mode.append(('forward', 'Forward'))
return composition_mode return composition_mode
_columns = { _columns = {
'send_to': fields.selection([('user', 'User'), ('partner', 'Partner'), \ 'partner_ids': fields.many2many('res.partner',
('email', 'Email Address')], 'Send to', required=True), 'lead_forward_to_partner_res_partner_rel',
'user_id': fields.many2one('res.users', "User"), 'wizard_id', 'partner_id', 'Additional contacts'),
'attachment_ids': fields.many2many('ir.attachment','lead_forward_to_partner_attachment_rel', 'wizard_id', 'attachment_id', 'Attachments'), 'attachment_ids': fields.many2many('ir.attachment',
'partner_id' : fields.many2one('res.partner', 'Partner'), 'lead_forward_to_partner_attachment_rel',
'history': fields.selection([('info', 'Case Information'), ('latest', 'Latest email'), ('whole', 'Whole Story')], 'Send history', required=True), 'wizard_id', 'attachment_id', 'Attachments'),
'history_mode': fields.selection([('info', 'Case Information'),
('latest', 'Latest email'), ('whole', 'Whole Story')],
'Send history', required=True),
} }
_defaults = { _defaults = {
'send_to' : 'email', 'history_mode': 'latest',
'history': 'latest', 'content_subtype': lambda self,cr, uid, context={}: 'html',
} }
def on_change_email(self, cr, uid, ids, user, context=None): def get_record_data(self, cr, uid, model, res_id, context=None):
if not user: """ Override of mail.compose.message, to add default values coming
return {'value': {'email_to': False}} form the related lead.
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
""" """
#TODO: ids and context are not comming res = super(crm_lead_forward_to_partner, self).get_record_data(cr, uid, model, res_id, context=context)
res = {} if model not in ('crm.lead') or not res_id:
res_id = context.get('active_id') return res
model = context.get('active_model') lead_obj = self.pool.get(model)
lead = self.pool.get(model).browse(cr, uid, res_id, context) lead = lead_obj.browse(cr, uid, res_id, context=context)
body = self._get_message_body(cr, uid, lead, history_type, context=context) subject = '%s: %s - %s' % (_('Fwd'), _('Lead forward'), lead.name)
if body: body = self._get_message_body(cr, uid, lead, 'info', context=context)
res = {'value': {'body' : body}} res.update({
'subject': subject,
'body': body,
})
return res 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): def on_change_partner_ids(self, cr, uid, ids, partner_ids, context=None):
"""This function fills address information based on partner/user selected """ TDE-TODO: Keep void; maybe we could check that partner_ids have
""" email defined. """
return {} return {}
# if not partner_id:
# return {'value' : {'email_to' : False}} def create(self, cr, uid, values, context=None):
# partner_obj = self.pool.get('res.partner') """ TDE-HACK: remove 'type' from context, because when viewing an
# data = {} opportunity form view, a default_type is set and propagated
# partner = partner_obj.browse(cr, uid, [partner_id]) to the wizard, that has a not matching type field. """
# user_id = partner and partner[0].user_id or False default_type = context.pop('default_type', None)
# data.update({'email_from': partner and partner[0].email or "", new_id = super(crm_lead_forward_to_partner, self).create(cr, uid, values, context=context)
# 'email_cc' : user_id and user_id.user or '', if default_type:
# 'user_id': user_id and user_id.id or False}) context['default_type'] = default_type
# return {'value' : data} return new_id
def action_forward(self, cr, uid, ids, context=None): 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: if context is None:
context = {} context = {}
res = {'type': 'ir.actions.act_window_close'} res = {'type': 'ir.actions.act_window_close'}
model = context.get('active_model') wizard = self.browse(cr, uid, ids[0], context=context)
if model not in ('crm.lead'): if wizard.model not in ('crm.lead'):
return res return res
this = self.browse(cr, uid, ids[0], context=context) lead = self.pool.get(wizard.model)
lead = self.pool.get(model) lead_ids = wizard.res_id and [wizard.res_id] or []
lead_id = context and context.get('active_id', False) or False
lead_ids = lead_id and [lead_id] or [] if wizard.composition_mode == 'mass_mail':
mode = context.get('mail.compose.message.mode')
if mode == 'mass_mail':
lead_ids = context and context.get('active_ids', []) or [] 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) self.write(cr, uid, ids, value, context=context)
context['mail.compose.message.mode'] = mode
self.send_mail(cr, uid, ids, context=context) self.send_mail(cr, uid, ids, context=context)
for case in lead.browse(cr, uid, lead_ids, context=context): # for case in lead.browse(cr, uid, lead_ids, context=context):
if (this.send_to == 'partner' and this.partner_id): # TDE: WHAT TO DO WITH THAT ?
lead.assign_partner(cr, uid, [case.id], this.partner_id.id, context=context) # 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': # if this.send_to == 'user':
lead.allocate_salesman(cr, uid, [case.id], [this.user_id.id], context=context) # 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)
return res return res
def _get_info_body(self, cr, uid, lead, context=None): 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': if lead.type == 'opportunity':
field_names += ['partner_id'] field_names += ['partner_id']
field_names += [ field_names += [
'partner_name' , 'title', 'function', 'street', 'street2', 'partner_name' , 'title', 'function', 'street', 'street2',
'zip', 'city', 'country_id', 'state_id', 'email_from', 'zip', 'city', 'country_id', 'state_id', 'email_from',
'phone', 'fax', 'mobile', 'categ_id', 'description', 'phone', 'fax', 'mobile', 'categ_id', 'description',
] ]
return proxy._mail_body(cr, uid, lead, field_names, context=context) return proxy._mail_body(cr, uid, lead, field_names, context=context)
def _get_message_body(self, cr, uid, lead, mode='whole', context=None): 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 """ 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') mail_message = self.pool.get('mail.message')
message_ids = []
body = self._get_info_body(cr, uid, lead, context=context) body = self._get_info_body(cr, uid, lead, context=context)
if mode in ('whole', 'latest'): if history_mode not in ('whole', 'latest'):
message_ids = lead.message_ids return body or ''
message_ids = map(lambda x: x.id, filter(lambda x: x.email_from, message_ids)) for message in lead.message_ids:
if mode == 'latest' and len(message_ids): header = '-------- Original Message --------'
message_ids = [message_ids[0]] sentdate = 'Date: %s' % (message.date or '')
for message in mail_message.browse(cr, uid, message_ids, context=context): desc = '\n%s'%(message.body)
header = '-------- Original Message --------' original = [header, sentdate, desc, '\n']
sender = 'From: %s' %(message.email_from or '') original = '\n'.join(original)
to = 'To: %s' % (message.email_to or '') body += original
sentdate = 'Date: %s' % (message.date or '') if history_mode == 'latest':
desc = '\n%s'%(message.body) break
original = [header, sender, to, sentdate, desc, '\n']
original = '\n'.join(original)
body += original
return body or '' 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: # 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="model">crm.lead.forward.to.partner</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<form string="Send Mail" version="7.0"> <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"/> <separator string="Forward to Partner" colspan="4"/>
<group col="4" colspan="6"> <group col="4">
<field name="history" colspan="2" on_change="on_change_history(history, context)"/> <field name="history_mode" colspan="4"
<field name="send_to" colspan="2"/> on_change="on_change_history_mode(history_mode, model, res_id)"/>
<group col="2" colspan="2" attrs="{ 'invisible' : [('send_to','!=','user')]}"> <field name="subject" colspan="4"/>
<field name="user_id" <field name="partner_ids" colspan="4" widget="many2many_tags"
attrs="{ 'required' : [('send_to','=','user')]}" on_change="on_change_partner_ids(partner_ids)"/>
on_change="on_change_email(user_id)"/> <notebook colspan="4">
</group> <page string="Body">
<group col="4" colspan="4" attrs="{'invisible' : [('send_to','!=','partner')]}"> <field name="body"/>
<field name="partner_ids" attrs="{'required' : [('send_to','=','partner')]}" on_change="on_change_partners(partner_ids)" colspan="2"/> </page>
</group> <page string="Attachments">
<field name="attachment_ids"/>
</page>
</notebook>
</group> </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> <footer>
<button name="action_forward" string="Send" type="object" class="oe_highlight"/> <button name="action_forward" string="Send" type="object"
class="oe_highlight"/>
or or
<button string="Cancel" class="oe_link" special="cancel"/> <button string="Cancel" special="cancel"
class="oe_link"/>
</footer> </footer>
</form> </form>
</field> </field>
@ -52,8 +46,6 @@
<field name="target">new</field> <field name="target">new</field>
</record> </record>
<act_window id="action_crm_send_mass_forward" <act_window id="action_crm_send_mass_forward"
multi="True" multi="True"
key2="client_action_multi" name="Mass forward to partner" key2="client_action_multi" name="Mass forward to partner"
@ -61,9 +53,7 @@
view_mode="form" target="new" view_type="form" view_mode="form" target="new" view_type="form"
context="{'mail.compose.message.mode' : 'mass_mail'}" context="{'mail.compose.message.mode' : 'mass_mail'}"
view_id="crm_lead_forward_to_partner_form" view_id="crm_lead_forward_to_partner_form"
/> />
</data> </data>
</openerp> </openerp>