mail.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2004-today OpenERP SA (<http://www.openerp.com>)
  6. # Copyright (C) 2011-today Synconics Technologies Pvt. Ltd. (<http://www.synconics.com>)
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ##############################################################################
  22. import datetime
  23. from openerp import SUPERUSER_ID, api
  24. from openerp.osv import osv, orm, fields
  25. class mail_notification(osv.Model):
  26. _inherit = 'mail.notification'
  27. _columns = {
  28. 'is_notified': fields.boolean('Notified')
  29. }
  30. _defaults = {
  31. 'is_notified': False
  32. }
  33. #------------------------------------------------------
  34. # Notification
  35. #------------------------------------------------------
  36. @api.cr_uid_ids_context
  37. def get_message_notification(self, cr, uid, context=None):
  38. notification_data = {}
  39. message_obj = self.pool.get('mail.message')
  40. current_date = datetime.datetime.utcnow()
  41. delta = datetime.timedelta(minutes=5)
  42. before_time = (current_date - delta).strftime("%Y-%m-%d %H:%M:%S")
  43. msg_ids = message_obj.search(cr, uid, [('date', '>=', before_time), ('date', '<', current_date.strftime("%Y-%m-%d %H:%M:%S"))], context=context)
  44. if msg_ids:
  45. user_id = self.pool['res.users'].browse(cr, SUPERUSER_ID, uid, context=context)
  46. domain = [('partner_id', '=', user_id.partner_id and user_id.partner_id.id), ('message_id', 'in', msg_ids), ('is_read', '=', False), ('is_notified', '=', False)]
  47. notif_ids = self.search(cr, uid, domain, context=context) or False
  48. for notif_id in self.browse(cr, uid, notif_ids, context=context):
  49. message_id = message_obj.browse(cr, uid, notif_id.message_id and notif_id.message_id.id or False, context=context)
  50. notification_data[notif_id.id] = {
  51. 'id': notif_id.id,
  52. 'email_from': message_id.email_from.split('<')[0],
  53. 'subject': message_id.subject or '-- No Subject --',
  54. 'company_id': user_id.company_id.id or False
  55. }
  56. return notification_data or {}
  57. return {}
  58. # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: