Browse Source

Default computation optimized, added create new records js functionallity

pull/1/head
Veronika Kotovich 9 years ago
parent
commit
c3f7fd6d20
  1. 57
      mail_move_message_models.py
  2. 9
      mail_move_message_views.xml
  3. 46
      static/src/js/mail_move_message.js

57
mail_move_message_models.py

@ -1,4 +1,6 @@
import json
from openerp import api, models, fields, SUPERUSER_ID
from openerp.tools import email_split
from openerp.tools.translate import _
class wizard(models.TransientModel):
@ -13,14 +15,27 @@ class wizard(models.TransientModel):
selection = [(m.model, m.display_name) for m in self.env['ir.model'].search([('model', 'in', model_names)])]
return selection
def _default_model(self):
@api.model
def default_get(self, fields_list):
res = super(wizard, self).default_get(fields_list)
model_fields = self.fields_get()
return model_fields['model']['selection'] and model_fields['model']['selection'][0][0]
res['model'] = model_fields['model']['selection'] and model_fields['model']['selection'][0][0]
def _default_res(self):
model = self._default_model()
res = self.env[model].search([], order='id desc', limit=1)
return res and res[0].id
res_id = self.env[res['model']].search([], order='id desc', limit=1)
res['res_id'] = res_id and res_id[0].id
email_from = self.env['mail.message'].browse(res['message_id']).email_from
parts = email_split(email_from.replace(' ',','))
if parts:
email = parts[0]
name = email_from[:email_from.index(email)].replace('"', '').replace('<', '').strip() or email_from
else:
name, email = email_from
res['message_name_from'] = name
res['message_email_from'] = email
return res
message_id = fields.Many2one('mail.message', string='Message')
message_body = fields.Html(related='message_id.body', string='Message to move', readonly=True)
@ -28,13 +43,15 @@ class wizard(models.TransientModel):
message_moved_by_user_id = fields.Many2one('res.users', related='message_id.moved_by_user_id', string='Moved by', readonly=True)
message_is_moved = fields.Boolean(string='Is Moved', related='message_id.is_moved', readonly=True)
parent_id = fields.Many2one('mail.message', string='Search by name', )
model = fields.Selection(_model_selection, string='Model', default=_default_model)
res_id = fields.Integer(string='Record ID', default=_default_res)
model = fields.Selection(_model_selection, string='Model')
res_id = fields.Integer(string='Record ID')
record_url = fields.Char('Link to record', readonly=True)
can_move = fields.Boolean('Can move', compute='get_can_move')
move_back = fields.Boolean('Move to origin', help='Move message and submessages to original place')
partner_id = fields.Many2one('res.partner', string='Author', related='message_id.author_id')
filter_by_partner = fields.Boolean('Filter Records by partner')
message_email_from = fields.Char()
message_name_from = fields.Char()
@api.depends('message_id')
@api.one
@ -153,6 +170,30 @@ class wizard(models.TransientModel):
self.message_id.unlink()
return {}
@api.model
def create_partner(self, message_id, relation, partner_id, message_name_from, message_email_from):
model = self.env[relation]
message = self.env['mail.message'].browse(message_id)
if not partner_id and message_name_from:
partner_id = self.env['res.partner'].create({
'name': message_name_from,
'email': message_email_from
}).id
message.write({'author_id': partner_id})
context = {'partner_id': partner_id}
if model._rec_name:
context.update({'default_%s' % model._rec_name: message.subject})
fields = model.fields_get()
contact_field = False
for n, f in fields.iteritems():
if f['type'] == 'many2one' and f['relation'] == 'res.partner':
contact_field = n
break
if contact_field:
context.update({'default_%s' % contact_field: partner_id})
return context
class mail_message(models.Model):
_inherit = 'mail.message'

9
mail_move_message_views.xml

@ -16,6 +16,8 @@
<field name="can_move" invisible="1"/>
<field name="message_is_moved" invisible="1"/>
<field name="message_name_from" invisible="1"/>
<field name="message_email_from" invisible="1"/>
<p attrs="{'invisible':[('can_move', '!=', False)]}">You cannot move this message. It was already moved with a message bellow. Open one and apply changes there.</p>
<group attrs="{'invisible':[('can_move', '!=', False)]}">
@ -33,13 +35,13 @@
<div>
<field name="filter_by_partner"/>
<field name="partner_id" style="width:50%"/>
<button name="create_partner" type="object" string="Create Partner" attrs="{'invisible':[('partner_id','!=',False)]}" class="oe_highlight"/>
<button string="Create Partner" attrs="{'invisible':[('partner_id','!=',False)]}" class="oe_highlight" special="quick_create" field="partner_id" context="{'force_email':True,'default_email':message_email_from,'default_name':message_name_from}"/>
</div>
<label for="res_id"/>
<div>
<field name="res_id" context="{'extended_name_with_contact':1}" widget="many2one" attrs="{'readonly': [('model','=',False)]}" style="width:50%"/>
<field name="record_url" widget="url" class="oe_inline" text="Open in new tab"/>
<button string="Create new record" name="create_record" type="object" class="oe_highlight" attrs="{'invisible':['|',('model','=',False),('res_id','!=',False)]}"/>
<button string="Create new record" name="create_record" type="object" class="oe_highlight" attrs="{'invisible':['|',('model','=',False),('res_id','!=',False)]}" special="quick_create" field="res_id" use_for_mail_move_message="True"/>
</div>
</group>
@ -52,8 +54,9 @@
<field name="message_id" invisible="1"/>
<field name="message_body"/>
</div>
<separator/>
<footer>
<button name="delete" string="Delete message" type="object" class="oe_highlight" confirm="Do you really want to delete this message?"/>
</footer>
</form>
</field>
</record>

46
static/src/js/mail_move_message.js

@ -39,4 +39,50 @@ openerp.mail_move_message = function (session) {
this.is_moved = datasets.is_moved || false;
}
})
session.web.form.WidgetButton.include({
on_click: function() {
if(this.node.attrs.special == 'quick_create'){
var self = this;
var related_field = this.field_manager.fields[this.node.attrs['field']];
var context_built = $.Deferred();
if(this.node.attrs.use_for_mail_move_message) {
var model = new session.web.Model(this.view.dataset.model);
var partner_id = self.field_manager.fields['partner_id'].get_value();
var message_name_from = self.field_manager.fields['message_name_from'].get_value();
var message_email_from = self.field_manager.fields['message_email_from'].get_value();
context_built = model.call('create_partner', [self.view.dataset.context.default_message_id,
related_field.field.relation, partner_id, message_name_from, message_email_from]);
}
else {
context_built.resolve(this.build_context());
}
$.when(context_built).pipe(function (context) {
if(self.node.attrs.use_for_mail_move_message) {
self.field_manager.fields['partner_id'].set_value(context['partner_id']);
}
var pop = new session.web.form.FormOpenPopup(this);
pop.show_element(
related_field.field.relation,
false,
context,
{
title: _t("Create new record"),
}
);
pop.on('closed', self, function () {
self.force_disabled = false;
self.check_disable();
});
pop.on('create_completed', self, function(id) {
related_field.set_value(id);
});
});
}
else {
this._super.apply(this, arguments);
}
},
});
}
Loading…
Cancel
Save