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.

299 lines
12 KiB

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