Browse Source

[IMP] "move back" feature

pull/2/head
Ivan Yelizariev 10 years ago
parent
commit
2e2cdeccdb
  1. 105
      mail_move_message_models.py
  2. 24
      mail_move_message_views.xml

105
mail_move_message_models.py

@ -6,10 +6,36 @@ class wizard(models.TransientModel):
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_user_id = fields.Many2one('res.users', related='message_id.moved_by_user_id', string='Moved by', 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='Record type') model_id = fields.Many2one('ir.model', string='Record type')
res_id = fields.Integer('Record ID') res_id = fields.Integer('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')
move_back = fields.Boolean('Move to origin')
@api.depends('message_id')
@api.one
def get_can_move(self):
# message was not moved before OR message is a top message of previous move
self.can_move = not self.message_id.moved_by_message_id or self.message_id.moved_by_message_id.id == self.message_id.id
@api.onchange('move_back')
def on_change_move_back(self):
if not self.move_back:
return
self.parent_id = self.message_id.moved_from_parent_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')
def update_move_back(self):
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 \
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))
@api.onchange('parent_id') @api.onchange('parent_id')
def on_change_parent_id(self): def on_change_parent_id(self):
@ -45,6 +71,20 @@ class wizard(models.TransientModel):
self.pool['mail.thread'].check_mail_message_access(cr, uid, mids, operation, model_obj=model_obj, context=context) self.pool['mail.thread'].check_mail_message_access(cr, uid, mids, operation, model_obj=model_obj, context=context)
@api.multi @api.multi
def open_moved_by_message_id(self):
message_id = None
for r in self:
message_id = r.message_moved_by_message_id.id
return {
'type': 'ir.actions.act_window',
'res_model': 'mail_move_message.wizard',
'view_mode': 'form',
'view_type': 'form',
'views': [[False, 'form']],
'target': 'new',
'context': {'default_message_id': message_id},
}
@api.multi
def move(self): def move(self):
for r in self: for r in self:
r.check_access() r.check_access()
@ -52,16 +92,8 @@ class wizard(models.TransientModel):
if not (r.parent_id.model == r.model_id.model and if not (r.parent_id.model == r.model_id.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
ids = [r.message_id.id]
while True:
new_ids = self.env['mail.message'].search([('parent_id', 'in', ids), ('id', 'not in', ids)]).ids
if new_ids:
ids = ids + new_ids
continue
break
r.message_id.sudo().write({'parent_id': r.parent_id.id})
self.env['mail.message'].sudo().search([('id', 'in', ids)]).write({'res_id': r.res_id, 'model': r.model_id.model})
r.message_id.move(r.parent_id.id, r.res_id, r.model_id.model, r.move_back)
if not ( r.model_id and r.res_id ): if not ( r.model_id 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 {
@ -83,6 +115,59 @@ class wizard(models.TransientModel):
class mail_message(models.Model): class mail_message(models.Model):
_inherit = 'mail.message' _inherit = 'mail.message'
moved_from_res_id = fields.Integer('Related Document ID (Original)')
moved_from_model = fields.Char('Related Document Model (Original)')
moved_from_parent_id = fields.Many2one('mail.message', 'Parent Message (Original)', ondelete='set null')
moved_by_message_id = fields.Many2one('mail.message', 'Moved by message', ondelete='set null', help='Top message, that initate moving this message')
moved_by_user_id = fields.Many2one('res.users', 'Moved by user', ondelete='set null')
all_child_ids = fields.One2many('mail.message', string='All childs', compute='_get_all_childs', help='all childs, including subchilds')
@api.one
def _get_all_childs(self, include_myself=True):
ids = []
if include_myself:
ids.append(self.id)
while True:
new_ids = self.search([('parent_id', 'in', ids), ('id', 'not in', ids)]).ids
if new_ids:
ids = ids + new_ids
continue
break
self.all_child_ids = ids
@api.one
def move(self, parent_id, res_id, model, move_back):
moved_by_message_id = self.id
moved_by_user_id = self.env.user.id
first_move = not self.moved_by_user_id
vals = {'parent_id': parent_id,
'moved_by_user_id': moved_by_user_id}
if first_move:
# moved_from_* variables contain not last, but original
# reference
vals['moved_from_parent_id'] = self.parent_id.id
elif move_back:
# clear moved_from_* variabls if we move everything back
vals['moved_from_parent_id'] = None
self.write(vals)
for r in self.all_child_ids:
vals = {'res_id': res_id,
'model': model,
'moved_by_user_id': moved_by_user_id,
'moved_by_message_id': moved_by_message_id}
if move_back:
vals['moved_by_user_id'] = None
vals['moved_by_message_id'] = None
if not r.moved_by_user_id or r.id == self.id and first_move:
vals['moved_from_res_id'] = r.res_id
vals['moved_from_model'] = r.model
elif move_back:
vals['moved_from_res_id'] = None
vals['moved_from_model'] = None
r.write(vals)
def name_get(self, cr, uid, ids, context=None): def name_get(self, cr, uid, ids, context=None):
if not (context or {}).get('extended_name'): if not (context or {}).get('extended_name'):
return super(mail_message, self).name_get(cr, uid, ids, context=context) return super(mail_message, self).name_get(cr, uid, ids, context=context)

24
mail_move_message_views.xml

@ -13,21 +13,29 @@
<field name="model">mail_move_message.wizard</field> <field name="model">mail_move_message.wizard</field>
<field name="arch" type="xml"> <field name="arch" type="xml">
<form string="Move Message"> <form string="Move Message">
<group>
<field name="parent_id" domain="[('parent_id','=', False)]" context="{'extended_name':1}"/>
<field name="model_id" class="oe_inline"/>
<field name="res_id" class="oe_inline"/>
<field name="can_move" 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)]}">
<field name="message_moved_by_message_id" context="{'extended_name':1}"/>
<field name="message_moved_by_user_id"/>
<button name="open_moved_by_message_id" string="Open message" type="object" class="oe_highlight"/>
</group>
<group attrs="{'invisible':[('can_move', '=', False)]}">
<field name="parent_id" domain="[('parent_id','=', False)]" context="{'extended_name':1}" />
<field name="move_back" attrs="{'invisible':[('message_moved_by_message_id','=',False)]}"/>
<field name="model_id" class="oe_inline" />
<field name="res_id" class="oe_inline" />
<field name="record_url" class="oe_inline" widget="url"/> <field name="record_url" class="oe_inline" widget="url"/>
</group> </group>
<separator string="Message to move"/>
<separator string="Message"/>
<div style="height:200px;overflow-y:auto;"> <div style="height:200px;overflow-y:auto;">
<field name="message_id" invisible="1"/> <field name="message_id" invisible="1"/>
<field name="message_body"/> <field name="message_body"/>
</div> </div>
<footer> <footer>
<button name="move" string="Move" type="object" class="oe_highlight"/>
or
<button string="Cancel" class="oe_link" special="cancel" />
<button name="move" string="Move" type="object" class="oe_highlight" attrs="{'invisible':[('can_move', '=', False)]}"/>
<button string="Cancel" class="" special="cancel" />
</footer> </footer>
</form> </form>
</field> </field>

Loading…
Cancel
Save