You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.9 KiB

  1. from openerp import api, models, fields, SUPERUSER_ID
  2. from openerp.tools.translate import _
  3. class wizard(models.TransientModel):
  4. _name = 'mail_move_message.wizard'
  5. message_id = fields.Many2one('mail.message', string='Message')
  6. message_body = fields.Html(related='message_id.body', string='Message to move', readonly=True)
  7. parent_id = fields.Many2one('mail.message', string='Search by name')
  8. model_id = fields.Many2one('ir.model', string='Record type')
  9. res_id = fields.Integer('Record ID')
  10. record_url = fields.Char('Link to record', readonly=True)
  11. @api.onchange('parent_id')
  12. def on_change_parent_id(self):
  13. if self.parent_id and self.parent_id.model:
  14. self.model_id = self.env['ir.model'].search([('model', '=', self.parent_id.model)])[0]
  15. self.res_id = self.parent_id.res_id
  16. else:
  17. self.model_id = None
  18. self.res_id = None
  19. @api.onchange('model_id', 'res_id')
  20. def on_change_res(self):
  21. if not ( self.model_id and self.res_id ):
  22. self.record_url = ''
  23. return
  24. self.record_url = '/web#id=%s&model=%s' % (self.res_id, self.model_id.model)
  25. @api.one
  26. def check_access(self):
  27. cr = self._cr
  28. uid = self.env.user.id
  29. operation = 'write'
  30. context = self._context
  31. if not ( self.model_id and self.res_id ):
  32. return True
  33. model_obj = self.pool[self.model_id.model]
  34. mids = model_obj.exists(cr, uid, [self.res_id])
  35. if hasattr(model_obj, 'check_mail_message_access'):
  36. model_obj.check_mail_message_access(cr, uid, mids, operation, context=context)
  37. else:
  38. self.pool['mail.thread'].check_mail_message_access(cr, uid, mids, operation, model_obj=model_obj, context=context)
  39. @api.multi
  40. def move(self):
  41. for r in self:
  42. r.check_access()
  43. if r.parent_id:
  44. if not (r.parent_id.model == r.model_id.model and
  45. r.parent_id.res_id == r.res_id):
  46. r.parent_id = None
  47. ids = [r.message_id.id]
  48. while True:
  49. new_ids = self.env['mail.message'].search([('parent_id', 'in', ids), ('id', 'not in', ids)]).ids
  50. if new_ids:
  51. ids = ids + new_ids
  52. continue
  53. break
  54. r.message_id.sudo().write({'parent_id': r.parent_id.id})
  55. self.env['mail.message'].sudo().search([('id', 'in', ids)]).write({'res_id': r.res_id, 'model': r.model_id.model})
  56. if not ( r.model_id and r.res_id ):
  57. obj = self.pool.get('ir.model.data').get_object_reference(self._cr, SUPERUSER_ID, 'mail', 'mail_archivesfeeds')[1]
  58. return {
  59. 'type' : 'ir.actions.client',
  60. 'name' : 'Archive',
  61. 'tag' : 'reload',
  62. 'params' : {'menu_id': obj},
  63. }
  64. return {
  65. 'name': _('Record'),
  66. 'view_type': 'form',
  67. 'view_mode': 'form',
  68. 'res_model': r.model_id.model,
  69. 'res_id': r.res_id,
  70. 'views': [(False, 'form')],
  71. 'type': 'ir.actions.act_window',
  72. }
  73. class mail_message(models.Model):
  74. _inherit = 'mail.message'
  75. def name_get(self, cr, uid, ids, context=None):
  76. if not (context or {}).get('extended_name'):
  77. return super(mail_message, self).name_get(cr, uid, ids, context=context)
  78. if isinstance(ids, (list, tuple)) and not len(ids):
  79. return []
  80. if isinstance(ids, (long, int)):
  81. ids = [ids]
  82. reads = self.read(cr, uid, ids, ['record_name','model', 'res_id'], context=context)
  83. res = []
  84. for record in reads:
  85. name = record['record_name'] or ''
  86. extended_name = ' [%s] ID %s' % (record.get('model', 'UNDEF'), record.get('res_id', 'UNDEF'))
  87. res.append((record['id'], name + extended_name))
  88. return res