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.

231 lines
9.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. 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='Model')
  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. @api.model
  105. def fields_get(self, fields=None, write_access=True, attributes=None):
  106. config_parameters = self.env['ir.config_parameter']
  107. res = super(wizard, self).fields_get(fields, write_access=write_access, attributes=attributes)
  108. if 'model_id' in res:
  109. model_names = config_parameters.get_param('mail_relocation_models')
  110. if model_names:
  111. model_names = model_names.split(',')
  112. res['model_id']['domain'] = [('model', 'in', model_names)]
  113. return res
  114. class mail_message(models.Model):
  115. _inherit = 'mail.message'
  116. is_moved = fields.Boolean('Is moved')
  117. moved_from_res_id = fields.Integer('Related Document ID (Original)')
  118. moved_from_model = fields.Char('Related Document Model (Original)')
  119. moved_from_parent_id = fields.Many2one('mail.message', 'Parent Message (Original)', ondelete='set null')
  120. moved_by_message_id = fields.Many2one('mail.message', 'Moved by message', ondelete='set null', help='Top message, that initate moving this message')
  121. moved_by_user_id = fields.Many2one('res.users', 'Moved by user', ondelete='set null')
  122. all_child_ids = fields.One2many('mail.message', string='All childs', compute='_get_all_childs', help='all childs, including subchilds')
  123. @api.one
  124. def _get_all_childs(self, include_myself=True):
  125. ids = []
  126. if include_myself:
  127. ids.append(self.id)
  128. while True:
  129. new_ids = self.search([('parent_id', 'in', ids), ('id', 'not in', ids)]).ids
  130. if new_ids:
  131. ids = ids + new_ids
  132. continue
  133. break
  134. moved_childs = self.search([('moved_by_message_id', '=', self.id)]).ids
  135. self.all_child_ids = ids + moved_childs
  136. @api.one
  137. def move(self, parent_id, res_id, model, move_back):
  138. vals = {}
  139. if move_back:
  140. # clear variables if we move everything back
  141. vals['is_moved'] = False
  142. vals['moved_by_user_id'] = None
  143. vals['moved_by_message_id'] = None
  144. vals['moved_from_res_id'] = None
  145. vals['moved_from_model'] = None
  146. vals['moved_from_parent_id'] = None
  147. else:
  148. vals['parent_id'] = parent_id
  149. vals['res_id'] = res_id
  150. vals['model'] = model
  151. vals['is_moved'] = True
  152. vals['moved_by_user_id'] = self.env.user.id
  153. vals['moved_by_message_id'] = self.id
  154. for r in self.all_child_ids:
  155. r_vals = vals.copy()
  156. if not r.is_moved:
  157. # moved_from_* variables contain not last, but original
  158. # reference
  159. r_vals['moved_from_parent_id'] = r.parent_id.id
  160. r_vals['moved_from_res_id'] = r.res_id
  161. r_vals['moved_from_model'] = r.model
  162. elif move_back:
  163. r_vals['parent_id'] = r.moved_from_parent_id.id
  164. r_vals['res_id'] = r.moved_from_res_id
  165. r_vals['model'] = r.moved_from_model
  166. print 'update message', r, r_vals
  167. r.sudo().write(r_vals)
  168. def name_get(self, cr, uid, ids, context=None):
  169. if not (context or {}).get('extended_name'):
  170. return super(mail_message, self).name_get(cr, uid, ids, context=context)
  171. if isinstance(ids, (list, tuple)) and not len(ids):
  172. return []
  173. if isinstance(ids, (long, int)):
  174. ids = [ids]
  175. reads = self.read(cr, uid, ids, ['record_name','model', 'res_id'], context=context)
  176. res = []
  177. for record in reads:
  178. name = record['record_name'] or ''
  179. extended_name = ' [%s] ID %s' % (record.get('model', 'UNDEF'), record.get('res_id', 'UNDEF'))
  180. res.append((record['id'], name + extended_name))
  181. return res
  182. def _message_read_dict(self, cr, uid, message, parent_id=False, context=None):
  183. res = super(mail_message, self)._message_read_dict(cr, uid, message, parent_id, context)
  184. res['is_moved'] = message.is_moved
  185. return res
  186. class mail_move_message_configuration(models.TransientModel):
  187. _name = 'mail_move_message.config.settings'
  188. _inherit = 'res.config.settings'
  189. model_ids = fields.Many2many(comodel_name='ir.model', string='Models')
  190. def get_default_model_ids(self, cr, uid, fields, context=None):
  191. config_parameters = self.pool.get('ir.config_parameter')
  192. model_obj = self.pool.get('ir.model')
  193. model_names = config_parameters.get_param(cr, uid, 'mail_relocation_models', context=context)
  194. if not model_names:
  195. return {}
  196. model_names = model_names.split(',')
  197. model_ids = model_obj.search(cr, uid, [('model', 'in', model_names)], context=context)
  198. return {'model_ids': model_ids}
  199. @api.multi
  200. def set_model_ids(self):
  201. config_parameters = self.env['ir.config_parameter']
  202. model_names = ''
  203. for record in self:
  204. model_names = ','.join([m.model for m in record.model_ids])
  205. config_parameters.set_param('mail_relocation_models', model_names)