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.

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