2012-02-01 16:49:02 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
2012-03-13 15:24:52 +00:00
|
|
|
# Copyright (C) 2009-today OpenERP SA (<http://www.openerp.com>)
|
2012-02-01 16:49:02 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
from osv import osv
|
|
|
|
from osv import fields
|
2012-08-30 08:51:16 +00:00
|
|
|
import tools
|
2012-02-01 16:49:02 +00:00
|
|
|
|
2012-08-13 19:10:06 +00:00
|
|
|
class mail_followers(osv.Model):
|
|
|
|
""" mail_followers holds the data related to the follow mechanism inside
|
2012-08-28 11:31:28 +00:00
|
|
|
OpenERP. Partners can choose to follow documents (records) of any kind
|
|
|
|
that inherits from mail.thread. Following documents allow to receive
|
|
|
|
notifications for new messages.
|
|
|
|
A subscription is characterized by:
|
|
|
|
:param: res_model: model of the followed objects
|
|
|
|
:param: res_id: ID of resource (may be 0 for every objects)
|
2012-02-01 16:49:02 +00:00
|
|
|
"""
|
2012-08-13 19:10:06 +00:00
|
|
|
_name = 'mail.followers'
|
2012-08-16 09:02:43 +00:00
|
|
|
_rec_name = 'partner_id'
|
2012-08-09 14:43:45 +00:00
|
|
|
_log_access = False
|
2012-08-16 09:02:43 +00:00
|
|
|
_description = 'Document Followers'
|
2012-02-01 16:49:02 +00:00
|
|
|
_columns = {
|
2012-02-06 11:26:23 +00:00
|
|
|
'res_model': fields.char('Related Document Model', size=128,
|
2012-04-02 12:59:26 +00:00
|
|
|
required=True, select=1,
|
|
|
|
help='Model of the followed resource'),
|
|
|
|
'res_id': fields.integer('Related Document ID', select=1,
|
|
|
|
help='Id of the followed resource'),
|
2012-08-16 09:02:43 +00:00
|
|
|
'partner_id': fields.many2one('res.partner', string='Related Partner',
|
2012-02-10 12:42:53 +00:00
|
|
|
ondelete='cascade', required=True, select=1),
|
2012-02-01 16:49:02 +00:00
|
|
|
}
|
2012-02-01 17:05:05 +00:00
|
|
|
|
2012-08-17 11:19:36 +00:00
|
|
|
|
2012-08-09 14:43:45 +00:00
|
|
|
class mail_notification(osv.Model):
|
2012-08-22 17:23:46 +00:00
|
|
|
""" Class holding notifications pushed to partners. Followers and partners
|
2012-08-28 11:31:28 +00:00
|
|
|
added in 'contacts to notify' receive notifications. """
|
2012-02-01 17:05:05 +00:00
|
|
|
_name = 'mail.notification'
|
2012-08-16 09:02:43 +00:00
|
|
|
_rec_name = 'partner_id'
|
2012-02-06 09:06:18 +00:00
|
|
|
_log_access = False
|
2012-08-16 09:02:43 +00:00
|
|
|
_description = 'Notifications'
|
2012-08-22 17:23:46 +00:00
|
|
|
|
2012-02-01 17:05:05 +00:00
|
|
|
_columns = {
|
2012-08-15 18:44:03 +00:00
|
|
|
'partner_id': fields.many2one('res.partner', string='Contact',
|
2012-08-31 11:52:02 +00:00
|
|
|
ondelete='cascade', required=True),
|
|
|
|
'read': fields.boolean('Read'),
|
2012-08-31 09:01:20 +00:00
|
|
|
'message_id': fields.many2one('mail.message', string='Message',
|
2012-08-31 11:52:02 +00:00
|
|
|
ondelete='cascade', required=True),
|
2012-02-01 17:05:05 +00:00
|
|
|
}
|
2012-08-22 17:23:46 +00:00
|
|
|
|
2012-02-01 17:05:05 +00:00
|
|
|
_defaults = {
|
2012-02-02 11:26:57 +00:00
|
|
|
'read': False,
|
2012-02-01 17:05:05 +00:00
|
|
|
}
|
2012-08-28 11:31:28 +00:00
|
|
|
|
2012-08-31 11:52:02 +00:00
|
|
|
def init(self, cr):
|
|
|
|
cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('mail_notification_partner_id_read_message_id',))
|
|
|
|
if not cr.fetchone():
|
|
|
|
cr.execute('CREATE INDEX mail_notification_partner_id_read_message_id ON mail_notification (partner_id, read, message_id)')
|
|
|
|
|
2012-08-28 11:31:28 +00:00
|
|
|
def create(self, cr, uid, vals, context=None):
|
|
|
|
""" Override of create to check that we can not create a notification
|
|
|
|
for a message the user can not read. """
|
|
|
|
if self.pool.get('mail.message').check_access_rights(cr, uid, 'read'):
|
2012-08-28 15:02:44 +00:00
|
|
|
return super(mail_notification, self).create(cr, uid, vals, context=context)
|
2012-08-29 16:46:14 +00:00
|
|
|
return False
|
2012-08-15 20:02:46 +00:00
|
|
|
|
2012-08-31 09:01:20 +00:00
|
|
|
def set_message_read(self, cr, uid, msg_id, context=None):
|
|
|
|
partner_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).partner_id.id
|
|
|
|
notif_ids = self.search(cr, uid, [('partner_id', '=', partner_id), ('message_id', '=', msg_id)], context=context)
|
|
|
|
return self.write(cr, uid, notif_ids, {'read': True}, context=context)
|
|
|
|
|
2012-08-16 16:43:11 +00:00
|
|
|
def notify(self, cr, uid, partner_ids, msg_id, context=None):
|
2012-08-23 18:06:48 +00:00
|
|
|
""" Send by email the notification depending on the user preferences """
|
2012-08-16 16:43:11 +00:00
|
|
|
context = context or {}
|
2012-08-29 15:00:02 +00:00
|
|
|
# mail_noemail (do not send email) or no partner_ids: do not send, return
|
|
|
|
if context.get('mail_noemail') or not partner_ids:
|
|
|
|
return True
|
|
|
|
|
2012-08-30 12:21:12 +00:00
|
|
|
mail_mail = self.pool.get('mail.mail')
|
|
|
|
msg = self.pool.get('mail.message').browse(cr, uid, msg_id, context=context)
|
2012-08-15 13:36:43 +00:00
|
|
|
|
2012-08-30 08:51:16 +00:00
|
|
|
# add signature
|
|
|
|
body_html = msg.body
|
|
|
|
signature = msg.author_id and msg.author_id.user_ids[0].signature or ''
|
2012-08-30 12:21:12 +00:00
|
|
|
if signature:
|
|
|
|
signature_block = u'\n<pre>%s</pre>\n' % signature
|
2012-08-30 08:51:16 +00:00
|
|
|
insertion_point = body_html.find('</html>')
|
2012-08-30 12:21:12 +00:00
|
|
|
if insertion_point > -1:
|
|
|
|
body_html = body_html[:insertion_point] + signature_block + body_html[insertion_point:]
|
|
|
|
else:
|
|
|
|
body_html += signature_block
|
2012-08-30 08:51:16 +00:00
|
|
|
|
2012-08-29 15:00:02 +00:00
|
|
|
towrite = {
|
|
|
|
'mail_message_id': msg.id,
|
|
|
|
'email_to': [],
|
2012-08-30 08:51:16 +00:00
|
|
|
'auto_delete': False,
|
|
|
|
'body_html': body_html,
|
2012-08-29 15:00:02 +00:00
|
|
|
}
|
2012-08-23 18:06:48 +00:00
|
|
|
|
2012-08-24 16:10:38 +00:00
|
|
|
for partner in self.pool.get('res.partner').browse(cr, uid, partner_ids, context=context):
|
2012-08-23 18:06:48 +00:00
|
|
|
# Do not send an email to the writer
|
2012-08-30 08:51:16 +00:00
|
|
|
if partner.user_ids and partner.user_ids[0].id == uid:
|
|
|
|
continue
|
|
|
|
# Do not send to partners without email address defined
|
|
|
|
if not partner.email:
|
2012-08-23 15:57:47 +00:00
|
|
|
continue
|
2012-08-23 18:06:48 +00:00
|
|
|
# Partner does not want to receive any emails
|
2012-08-30 08:51:16 +00:00
|
|
|
if partner.notification_email_send == 'none':
|
2012-08-15 13:36:43 +00:00
|
|
|
continue
|
2012-08-30 08:51:16 +00:00
|
|
|
# Partner wants to receive only emails and comments
|
|
|
|
if partner.notification_email_send == 'comment' and msg.type not in ('email', 'comment'):
|
|
|
|
continue
|
|
|
|
# Partner wants to receive only emails
|
|
|
|
if partner.notification_email_send == 'email' and msg.type != 'email':
|
2012-08-15 13:36:43 +00:00
|
|
|
continue
|
2012-08-21 16:00:36 +00:00
|
|
|
if partner.email not in towrite['email_to']:
|
|
|
|
towrite['email_to'].append(partner.email)
|
2012-08-30 12:21:12 +00:00
|
|
|
if towrite['email_to']:
|
|
|
|
towrite['email_to'] = ', '.join(towrite['email_to'])
|
|
|
|
email_notif_id = mail_mail.create(cr, uid, towrite, context=context)
|
|
|
|
mail_mail.send(cr, uid, [email_notif_id], context=context)
|
2012-08-15 13:36:43 +00:00
|
|
|
return True
|