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.

250 lines
11 KiB

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