mail_inbox.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # -*- coding: utf-8 -*-
  2. from lxml import etree
  3. from openerp import tools, models, fields, api
  4. from openerp.tools.translate import _
  5. class vieterp_mail_inbox(models.Model):
  6. _inherit = ['mail.mail']
  7. _name = 'mail.inbox'
  8. fetchmail_server_id = fields.Many2one('fetchmail.server.inbox', "Inbound Mail Server", readonly=True, index=True,
  9. oldname='server_id')
  10. template_id = fields.Many2one('email.template', string='Mail Template', select=True)
  11. state = fields.Selection([
  12. ('inbox', 'Inbox'),
  13. ('outgoing', 'Outgoing'),
  14. ('sent', 'Sent'),
  15. ('received', 'Received'),
  16. ('exception', 'Delivery Failed'),
  17. ('cancel', 'Cancelled'),
  18. ], 'Status', readonly=True, copy=False, default='outgoing')
  19. @api.model
  20. def message_new(self, msg_dict, custom_values=None):
  21. self = self.with_context(default_user_id=False)
  22. mail_data = {
  23. 'subject': msg_dict.get('subject') or _("No Subject"),
  24. 'email_from': msg_dict.get('from'),
  25. 'email_to': msg_dict.get('to'),
  26. 'email_cc': msg_dict.get('cc'),
  27. 'partner_id': msg_dict.get('author_id', False),
  28. 'body_html': msg_dict.get('body', ''),
  29. 'attachment_ids': msg_dict.get('attachments', []),
  30. 'state': 'inbox',
  31. }
  32. result = self.create(mail_data)
  33. return result
  34. @api.multi
  35. @api.returns('mail.message', lambda value: value.id)
  36. def message_post(self, subtype=None, **kwargs):
  37. message_data = {
  38. 'subject': kwargs.get('subject'),
  39. 'date': kwargs.get('date'),
  40. 'body': kwargs.get('body'),
  41. 'email_from': kwargs.get('email_from'),
  42. 'reply_to': kwargs.get('email_from'),
  43. }
  44. return self.env['mail.message'].create(message_data)
  45. # @api.model
  46. # def fields_view_get(self, view_id=None, view_type=False, toolbar=False, submenu=False):
  47. # context = self._context
  48. # result = super(vieterp_mail_inbox, self).fields_view_get(view_id, view_type, toolbar, submenu)
  49. # if view_type == 'form':
  50. # current_id = context.get('active_id', False)
  51. # my_state = self.browse(current_id).state
  52. # if my_state in ['inbox', 'outgoing']:
  53. # doc = etree.XML(result['arch'])
  54. # for node in doc.xpath('//form'):
  55. # node.set('edit', 'true')
  56. # result['arch'] = etree.tostring(doc)
  57. # return result
  58. @api.onchange('template_id') # if template are changed, call method
  59. def check_template_change(self):
  60. """ - mass_mailing: we cannot render, so return the template values
  61. - normal mode: return rendered values """
  62. if self.template_id and self.template_id.id:
  63. self.subject = self.template_id.subject
  64. self.body_html = self.template_id.body_html
  65. self.reply_to = self.template_id.reply_to
  66. self.mail_server_id = self.template_id.mail_server_id
  67. if self.template_id.attachment_ids:
  68. self.attachment_ids = [att.id for att in template.attachment_ids]
  69. if self.template_id.mail_server_id:
  70. self.mail_server_id = self.template_id.mail_server_id.id
  71. if self.template_id.user_signature and self.body_html:
  72. signature = self.env['res.users'].browse(self._uid).signature
  73. self.body = tools.append_content_to_html(self.body, signature, plaintext=False)
  74. else:
  75. if not self.body_html:
  76. signature = self.env['res.users'].browse(self._uid).signature
  77. self.body_html = signature