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.

188 lines
8.0 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. message_moved_by_message_id = fields.Many2one('mail.message', related='message_id.moved_by_message_id', string='Moved with', readonly=True)
  8. message_moved_by_user_id = fields.Many2one('res.users', related='message_id.moved_by_user_id', string='Moved by', readonly=True)
  9. parent_id = fields.Many2one('mail.message', string='Search by name')
  10. model_id = fields.Many2one('ir.model', string='Record type')
  11. res_id = fields.Integer('Record ID')
  12. record_url = fields.Char('Link to record', readonly=True)
  13. can_move = fields.Boolean('Can move', compute='get_can_move')
  14. move_back = fields.Boolean('Move to origin')
  15. @api.depends('message_id')
  16. @api.one
  17. def get_can_move(self):
  18. # message was not moved before OR message is a top message of previous move
  19. self.can_move = not self.message_id.moved_by_message_id or self.message_id.moved_by_message_id.id == self.message_id.id
  20. @api.onchange('move_back')
  21. def on_change_move_back(self):
  22. if not self.move_back:
  23. return
  24. self.parent_id = self.message_id.moved_from_parent_id
  25. self.res_id = self.message_id.moved_from_res_id
  26. model = self.message_id.moved_from_model
  27. self.model_id = model and self.env['ir.model'].search([('model','=', model)])
  28. @api.onchange('parent_id', 'res_id', 'model_id')
  29. def update_move_back(self):
  30. model = self.message_id.moved_from_model
  31. model_id = model and self.env['ir.model'].search([('model','=', model)])
  32. self.move_back = self.parent_id == self.message_id.moved_from_parent_id \
  33. and self.res_id == self.message_id.moved_from_res_id \
  34. and (self.model_id == model_id or (not self.model_id and not model_id))
  35. @api.onchange('parent_id')
  36. def on_change_parent_id(self):
  37. if self.parent_id and self.parent_id.model:
  38. self.model_id = self.env['ir.model'].search([('model', '=', self.parent_id.model)])[0]
  39. self.res_id = self.parent_id.res_id
  40. else:
  41. self.model_id = None
  42. self.res_id = None
  43. @api.onchange('model_id', 'res_id')
  44. def on_change_res(self):
  45. if not ( self.model_id and self.res_id ):
  46. self.record_url = ''
  47. return
  48. self.record_url = '/web#id=%s&model=%s' % (self.res_id, self.model_id.model)
  49. @api.one
  50. def check_access(self):
  51. cr = self._cr
  52. uid = self.env.user.id
  53. operation = 'write'
  54. context = self._context
  55. if not ( self.model_id and self.res_id ):
  56. return True
  57. model_obj = self.pool[self.model_id.model]
  58. mids = model_obj.exists(cr, uid, [self.res_id])
  59. if hasattr(model_obj, 'check_mail_message_access'):
  60. model_obj.check_mail_message_access(cr, uid, mids, operation, context=context)
  61. else:
  62. self.pool['mail.thread'].check_mail_message_access(cr, uid, mids, operation, model_obj=model_obj, context=context)
  63. @api.multi
  64. def open_moved_by_message_id(self):
  65. message_id = None
  66. for r in self:
  67. message_id = r.message_moved_by_message_id.id
  68. return {
  69. 'type': 'ir.actions.act_window',
  70. 'res_model': 'mail_move_message.wizard',
  71. 'view_mode': 'form',
  72. 'view_type': 'form',
  73. 'views': [[False, 'form']],
  74. 'target': 'new',
  75. 'context': {'default_message_id': message_id},
  76. }
  77. @api.multi
  78. def move(self):
  79. for r in self:
  80. r.check_access()
  81. if r.parent_id:
  82. if not (r.parent_id.model == r.model_id.model and
  83. r.parent_id.res_id == r.res_id):
  84. r.parent_id = None
  85. r.message_id.move(r.parent_id.id, r.res_id, r.model_id.model, r.move_back)
  86. if not ( r.model_id and r.res_id ):
  87. obj = self.pool.get('ir.model.data').get_object_reference(self._cr, SUPERUSER_ID, 'mail', 'mail_archivesfeeds')[1]
  88. return {
  89. 'type' : 'ir.actions.client',
  90. 'name' : 'Archive',
  91. 'tag' : 'reload',
  92. 'params' : {'menu_id': obj},
  93. }
  94. return {
  95. 'name': _('Record'),
  96. 'view_type': 'form',
  97. 'view_mode': 'form',
  98. 'res_model': r.model_id.model,
  99. 'res_id': r.res_id,
  100. 'views': [(False, 'form')],
  101. 'type': 'ir.actions.act_window',
  102. }
  103. class mail_message(models.Model):
  104. _inherit = 'mail.message'
  105. moved_from_res_id = fields.Integer('Related Document ID (Original)')
  106. moved_from_model = fields.Char('Related Document Model (Original)')
  107. moved_from_parent_id = fields.Many2one('mail.message', 'Parent Message (Original)', ondelete='set null')
  108. moved_by_message_id = fields.Many2one('mail.message', 'Moved by message', ondelete='set null', help='Top message, that initate moving this message')
  109. moved_by_user_id = fields.Many2one('res.users', 'Moved by user', ondelete='set null')
  110. all_child_ids = fields.One2many('mail.message', string='All childs', compute='_get_all_childs', help='all childs, including subchilds')
  111. @api.one
  112. def _get_all_childs(self, include_myself=True):
  113. ids = []
  114. if include_myself:
  115. ids.append(self.id)
  116. while True:
  117. new_ids = self.search([('parent_id', 'in', ids), ('id', 'not in', ids)]).ids
  118. if new_ids:
  119. ids = ids + new_ids
  120. continue
  121. break
  122. self.all_child_ids = ids
  123. @api.one
  124. def move(self, parent_id, res_id, model, move_back):
  125. moved_by_message_id = self.id
  126. moved_by_user_id = self.env.user.id
  127. first_move = not self.moved_by_user_id
  128. vals = {'parent_id': parent_id}
  129. if first_move:
  130. # moved_from_* variables contain not last, but original
  131. # reference
  132. vals['moved_from_parent_id'] = self.parent_id.id
  133. elif move_back:
  134. # clear moved_from_* variabls if we move everything back
  135. vals['moved_from_parent_id'] = None
  136. self.sudo().write(vals)
  137. for r in self.all_child_ids:
  138. vals = {'res_id': res_id,
  139. 'model': model,
  140. 'moved_by_user_id': moved_by_user_id,
  141. 'moved_by_message_id': moved_by_message_id}
  142. if move_back:
  143. vals['moved_by_user_id'] = None
  144. vals['moved_by_message_id'] = None
  145. if not r.moved_by_user_id or r.id == self.id and first_move:
  146. vals['moved_from_res_id'] = r.res_id
  147. vals['moved_from_model'] = r.model
  148. elif move_back:
  149. vals['moved_from_res_id'] = None
  150. vals['moved_from_model'] = None
  151. r.sudo().write(vals)
  152. def name_get(self, cr, uid, ids, context=None):
  153. if not (context or {}).get('extended_name'):
  154. return super(mail_message, self).name_get(cr, uid, ids, context=context)
  155. if isinstance(ids, (list, tuple)) and not len(ids):
  156. return []
  157. if isinstance(ids, (long, int)):
  158. ids = [ids]
  159. reads = self.read(cr, uid, ids, ['record_name','model', 'res_id'], context=context)
  160. res = []
  161. for record in reads:
  162. name = record['record_name'] or ''
  163. extended_name = ' [%s] ID %s' % (record.get('model', 'UNDEF'), record.get('res_id', 'UNDEF'))
  164. res.append((record['id'], name + extended_name))
  165. return res
  166. def _message_read_dict(self, cr, uid, message, parent_id=False, context=None):
  167. res = super(mail_message, self)._message_read_dict(cr, uid, message, parent_id, context)
  168. res['is_moved'] = bool(message.moved_by_user_id)
  169. return res