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.

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