mail_message.py 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. from odoo import api, fields, models
  2. class Message(models.Model):
  3. _inherit = 'mail.message'
  4. snailmail_error = fields.Boolean("Snailmail message in error", compute="_compute_snailmail_error", search="_search_snailmail_error")
  5. letter_ids = fields.One2many(comodel_name='snailmail.letter', inverse_name='message_id')
  6. message_type = fields.Selection(selection_add=[
  7. ('snailmail', 'Snailmail')
  8. ], ondelete={'snailmail': lambda recs: recs.write({'message_type': 'email'})})
  9. @api.depends('letter_ids', 'letter_ids.state')
  10. def _compute_snailmail_error(self):
  11. for message in self:
  12. if message.message_type == 'snailmail' and message.letter_ids:
  13. message.snailmail_error = message.letter_ids[0].state == 'error'
  14. else:
  15. message.snailmail_error = False
  16. def _search_snailmail_error(self, operator, operand):
  17. if operator == '=' and operand:
  18. return ['&', ('letter_ids.state', '=', 'error'), ('letter_ids.user_id', '=', self.env.user.id)]
  19. return ['!', '&', ('letter_ids.state', '=', 'error'), ('letter_ids.user_id', '=', self.env.user.id)]
  20. def cancel_letter(self):
  21. self.mapped('letter_ids').cancel()
  22. def send_letter(self):
  23. self.mapped('letter_ids')._snailmail_print()