Browse Source

Code optimization

pull/1/head
Veronika Kotovich 9 years ago
parent
commit
3082bc3e0d
  1. 3
      controllers/main.py
  2. 70
      mail_move_message_models.py
  3. 5
      mail_move_message_views.xml

3
controllers/main.py

@ -6,7 +6,7 @@ class DataSetCustom(DataSet):
def _call_kw(self, model, method, args, kwargs): def _call_kw(self, model, method, args, kwargs):
res = super(DataSetCustom, self)._call_kw(model, method, args, kwargs) res = super(DataSetCustom, self)._call_kw(model, method, args, kwargs)
if 'context' in kwargs and kwargs['context'].get('extended_name_with_contact') and res:
if method == 'name_search' and 'context' in kwargs and kwargs['context'].get('extended_name_with_contact') and res:
model = request.session.model(model) model = request.session.model(model)
fields = model.fields_get(False, request.context) fields = model.fields_get(False, request.context)
contact_field = False contact_field = False
@ -16,6 +16,7 @@ class DataSetCustom(DataSet):
break break
partner_info = {} partner_info = {}
if contact_field: if contact_field:
print res
partner_info = model.read([r[0] for r in res], [contact_field]) partner_info = model.read([r[0] for r in res], [contact_field])
partner_info = dict([(p['id'], p[contact_field]) for p in partner_info]) partner_info = dict([(p['id'], p[contact_field]) for p in partner_info])
final_res = [] final_res = []

70
mail_move_message_models.py

@ -4,18 +4,27 @@ from openerp.tools.translate import _
class wizard(models.TransientModel): class wizard(models.TransientModel):
_name = 'mail_move_message.wizard' _name = 'mail_move_message.wizard'
@api.model
def _model_selection(self):
selection = []
config_parameters = self.env['ir.config_parameter']
model_names = config_parameters.get_param('mail_relocation_models')
if model_names:
model_names = model_names.split(',')
selection = [(m.model, m.display_name) for m in self.env['ir.model'].search([('model', 'in', model_names)])]
return selection
message_id = fields.Many2one('mail.message', string='Message') message_id = fields.Many2one('mail.message', string='Message')
message_body = fields.Html(related='message_id.body', string='Message to move', readonly=True) message_body = fields.Html(related='message_id.body', string='Message to move', readonly=True)
message_moved_by_message_id = fields.Many2one('mail.message', related='message_id.moved_by_message_id', string='Moved with', readonly=True) message_moved_by_message_id = fields.Many2one('mail.message', related='message_id.moved_by_message_id', string='Moved with', readonly=True)
message_moved_by_user_id = fields.Many2one('res.users', related='message_id.moved_by_user_id', string='Moved by', readonly=True) 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) 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') parent_id = fields.Many2one('mail.message', string='Search by name')
model_id = fields.Many2one('ir.model', string='Model')
res_id = fields.Many2one('ir.model', string='Record ID')
res_id = fields.Integer(string='Record ID')
record_url = fields.Char('Link to record', readonly=True) record_url = fields.Char('Link to record', readonly=True)
can_move = fields.Boolean('Can move', compute='get_can_move') 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') move_back = fields.Boolean('Move to origin', help='Move message and submessages to original place')
model = fields.Char('Model', related='model_id.model')
model = fields.Selection(_model_selection, string='Model')
partner_id = fields.Many2one('res.partner', string='Author') partner_id = fields.Many2one('res.partner', string='Author')
filter_by_partner = fields.Boolean('Filter Records by partner') filter_by_partner = fields.Boolean('Filter Records by partner')
@ -31,33 +40,32 @@ class wizard(models.TransientModel):
return return
self.parent_id = self.message_id.moved_from_parent_id self.parent_id = self.message_id.moved_from_parent_id
self.res_id = self.message_id.moved_from_res_id self.res_id = self.message_id.moved_from_res_id
model = self.message_id.moved_from_model
self.model_id = model and self.env['ir.model'].search([('model','=', model)])
@api.onchange('parent_id', 'res_id', 'model_id')
self.model = self.message_id.moved_from_model
@api.onchange('parent_id', 'res_id', 'model')
def update_move_back(self): def update_move_back(self):
model = self.message_id.moved_from_model model = self.message_id.moved_from_model
model_id = model and self.env['ir.model'].search([('model','=', model)])
self.move_back = self.parent_id == self.message_id.moved_from_parent_id \ self.move_back = self.parent_id == self.message_id.moved_from_parent_id \
and self.res_id == self.message_id.moved_from_res_id \ and self.res_id == self.message_id.moved_from_res_id \
and (self.model_id == model_id or (not self.model_id and not model_id))
and (self.model == model or (not self.model and not model))
@api.onchange('parent_id') @api.onchange('parent_id')
def on_change_parent_id(self): def on_change_parent_id(self):
if self.parent_id and self.parent_id.model: if self.parent_id and self.parent_id.model:
self.model_id = self.env['ir.model'].search([('model', '=', self.parent_id.model)])[0]
self.model = self.parent_id.model
self.res_id = self.parent_id.res_id self.res_id = self.parent_id.res_id
else: else:
self.model_id = None
self.model = None
self.res_id = None self.res_id = None
@api.onchange('model_id', 'res_id')
@api.onchange('model', 'res_id')
def on_change_res(self): def on_change_res(self):
if not ( self.model_id and self.res_id ):
if not ( self.model and self.res_id ):
self.record_url = '' self.record_url = ''
return return
self.record_url = '/web#id=%s&model=%s' % (self.res_id, self.model_id.model)
self.record_url = '/web#id=%s&model=%s' % (self.res_id, self.model)
@api.onchange('filter_by_partner', 'partner_id') @api.onchange('filter_by_partner', 'partner_id')
def on_change_partner(self): def on_change_partner(self):
@ -80,9 +88,9 @@ class wizard(models.TransientModel):
operation = 'write' operation = 'write'
context = self._context context = self._context
if not ( self.model_id and self.res_id ):
if not ( self.model and self.res_id ):
return True return True
model_obj = self.pool[self.model_id.model]
model_obj = self.pool[self.model]
mids = model_obj.exists(cr, uid, [self.res_id]) mids = model_obj.exists(cr, uid, [self.res_id])
if hasattr(model_obj, 'check_mail_message_access'): if hasattr(model_obj, 'check_mail_message_access'):
model_obj.check_mail_message_access(cr, uid, mids, operation, context=context) model_obj.check_mail_message_access(cr, uid, mids, operation, context=context)
@ -108,12 +116,12 @@ class wizard(models.TransientModel):
for r in self: for r in self:
r.check_access() r.check_access()
if r.parent_id: if r.parent_id:
if not (r.parent_id.model == r.model_id.model and
if not (r.parent_id.model == r.model and
r.parent_id.res_id == r.res_id): r.parent_id.res_id == r.res_id):
r.parent_id = None r.parent_id = None
r.message_id.move(r.parent_id.id, r.res_id, r.model_id.model, r.move_back)
r.message_id.move(r.parent_id.id, r.res_id, r.model, r.move_back)
if not ( r.model_id and r.res_id ):
if not ( r.model and r.res_id ):
obj = self.pool.get('ir.model.data').get_object_reference(self._cr, SUPERUSER_ID, 'mail', 'mail_archivesfeeds')[1] obj = self.pool.get('ir.model.data').get_object_reference(self._cr, SUPERUSER_ID, 'mail', 'mail_archivesfeeds')[1]
return { return {
'type' : 'ir.actions.client', 'type' : 'ir.actions.client',
@ -125,22 +133,26 @@ class wizard(models.TransientModel):
'name': _('Record'), 'name': _('Record'),
'view_type': 'form', 'view_type': 'form',
'view_mode': 'form', 'view_mode': 'form',
'res_model': r.model_id.model,
'res_model': r.model,
'res_id': r.res_id, 'res_id': r.res_id,
'views': [(False, 'form')], 'views': [(False, 'form')],
'type': 'ir.actions.act_window', 'type': 'ir.actions.act_window',
} }
@api.model
def fields_get(self, fields=None, write_access=True, attributes=None):
config_parameters = self.env['ir.config_parameter']
res = super(wizard, self).fields_get(fields, write_access=write_access, attributes=attributes)
if 'model_id' in res:
model_names = config_parameters.get_param('mail_relocation_models')
if model_names:
model_names = model_names.split(',')
res['model_id']['domain'] = [('model', 'in', model_names)]
return res
# @api.model
# def fields_get(self, fields=None, write_access=True, attributes=None):
# config_parameters = self.env['ir.config_parameter']
# res = super(wizard, self).fields_get(fields, write_access=write_access, attributes=attributes)
# print res['res_id']
# print res['partner_id']
# if 'res_id' in res:
# res['res_id'].update({
# 'type': 'many2one',
# })
# return res
class mail_message(models.Model): class mail_message(models.Model):
_inherit = 'mail.message' _inherit = 'mail.message'

5
mail_move_message_views.xml

@ -15,7 +15,6 @@
<form string="Move Message"> <form string="Move Message">
<field name="can_move" invisible="1"/> <field name="can_move" invisible="1"/>
<field name="message_is_moved" invisible="1"/> <field name="message_is_moved" invisible="1"/>
<field name="model" widget="polymorphic" polymorphic="res_id" 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> <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)]}"> <group attrs="{'invisible':[('can_move', '!=', False)]}">
@ -25,10 +24,10 @@
</group> </group>
<group attrs="{'invisible':[('can_move', '=', False)]}" col="4"> <group attrs="{'invisible':[('can_move', '=', False)]}" col="4">
<field name="move_back" attrs="{'invisible':[('message_is_moved','=',False)]}" colspan="4"/> <field name="move_back" attrs="{'invisible':[('message_is_moved','=',False)]}" colspan="4"/>
<field name="model_id" class="oe_inline"/>
<field name="model" class="oe_inline" widget="polymorphic" polymorphic="res_id"/>
<field name="filter_by_partner"/> <field name="filter_by_partner"/>
<field name="partner_id"/> <field name="partner_id"/>
<field name="res_id" colspan="2" context="{'extended_name_with_contact':1}"/>
<field name="res_id" colspan="2" context="{'extended_name_with_contact':1}" widget="many2one" attrs="{'readonly': [('model','=',False)]}"/>
<field name="record_url" colspan="4" widget="url"/> <field name="record_url" colspan="4" widget="url"/>
</group> </group>
<button name="move" string="Move" type="object" class="oe_highlight" attrs="{'invisible':[('can_move', '=', False)]}"/> <button name="move" string="Move" type="object" class="oe_highlight" attrs="{'invisible':[('can_move', '=', False)]}"/>

Loading…
Cancel
Save