2012-01-31 15:52:57 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
2012-03-13 16:19:02 +00:00
|
|
|
# Copyright (C) 2010-today OpenERP SA (<http://www.openerp.com>)
|
2012-01-31 15:52:57 +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/>
|
|
|
|
#
|
2012-01-31 15:54:12 +00:00
|
|
|
##############################################################################
|
|
|
|
|
2012-03-29 07:32:09 +00:00
|
|
|
import openerp
|
|
|
|
import openerp.tools as tools
|
2012-12-06 14:56:32 +00:00
|
|
|
from openerp.osv import osv
|
|
|
|
from openerp.osv import fields
|
2014-06-20 14:34:27 +00:00
|
|
|
from openerp.tools.safe_eval import safe_eval as eval
|
2012-09-25 09:48:56 +00:00
|
|
|
from openerp import SUPERUSER_ID
|
2014-07-01 10:24:23 +00:00
|
|
|
from openerp.tools.translate import _
|
2012-10-16 11:17:53 +00:00
|
|
|
|
2012-08-14 11:40:21 +00:00
|
|
|
class mail_group(osv.Model):
|
2012-09-04 09:28:20 +00:00
|
|
|
""" A mail_group is a collection of users sharing messages in a discussion
|
|
|
|
group. The group mechanics are based on the followers. """
|
2012-03-29 16:10:00 +00:00
|
|
|
_description = 'Discussion group'
|
2012-01-31 15:54:12 +00:00
|
|
|
_name = 'mail.group'
|
2012-10-15 13:23:13 +00:00
|
|
|
_mail_flat_thread = False
|
2012-08-20 19:10:58 +00:00
|
|
|
_inherit = ['mail.thread']
|
2012-12-06 12:14:09 +00:00
|
|
|
_inherits = {'mail.alias': 'alias_id'}
|
2012-03-07 15:34:45 +00:00
|
|
|
|
2012-06-28 10:27:07 +00:00
|
|
|
def _get_image(self, cr, uid, ids, name, args, context=None):
|
2014-07-06 14:44:26 +00:00
|
|
|
result = {}
|
2012-06-28 10:27:07 +00:00
|
|
|
for obj in self.browse(cr, uid, ids, context=context):
|
2012-08-07 11:10:39 +00:00
|
|
|
result[obj.id] = tools.image_get_resized_images(obj.image)
|
2012-06-27 14:58:58 +00:00
|
|
|
return result
|
2012-08-15 17:08:22 +00:00
|
|
|
|
2012-06-28 10:27:07 +00:00
|
|
|
def _set_image(self, cr, uid, id, name, value, args, context=None):
|
2012-08-07 11:10:39 +00:00
|
|
|
return self.write(cr, uid, [id], {'image': tools.image_resize_image_big(value)}, context=context)
|
2012-08-15 17:08:22 +00:00
|
|
|
|
2012-01-31 15:54:12 +00:00
|
|
|
_columns = {
|
2014-05-21 09:52:05 +00:00
|
|
|
'name': fields.char('Name', required=True, translate=True),
|
2012-01-31 15:54:12 +00:00
|
|
|
'description': fields.text('Description'),
|
2012-08-14 09:07:19 +00:00
|
|
|
'menu_id': fields.many2one('ir.ui.menu', string='Related Menu', required=True, ondelete="cascade"),
|
2012-09-13 09:32:46 +00:00
|
|
|
'public': fields.selection([('public', 'Public'), ('private', 'Private'), ('groups', 'Selected Group Only')], 'Privacy', required=True,
|
2012-08-14 06:51:36 +00:00
|
|
|
help='This group is visible by non members. \
|
2012-07-19 12:00:26 +00:00
|
|
|
Invisible groups can add members through the invite button.'),
|
2012-08-14 11:22:04 +00:00
|
|
|
'group_public_id': fields.many2one('res.groups', string='Authorized Group'),
|
2012-07-19 12:00:26 +00:00
|
|
|
'group_ids': fields.many2many('res.groups', rel='mail_group_res_group_rel',
|
2012-08-14 06:51:36 +00:00
|
|
|
id1='mail_group_id', id2='groups_id', string='Auto Subscription',
|
2012-07-19 12:00:26 +00:00
|
|
|
help="Members of those groups will automatically added as followers. "\
|
2012-08-14 11:52:22 +00:00
|
|
|
"Note that they will be able to manage their subscription manually "\
|
|
|
|
"if necessary."),
|
2012-09-07 09:23:33 +00:00
|
|
|
# image: all image fields are base64 encoded and PIL-supported
|
2012-06-27 14:58:58 +00:00
|
|
|
'image': fields.binary("Photo",
|
2012-09-07 09:23:33 +00:00
|
|
|
help="This field holds the image used as photo for the group, limited to 1024x1024px."),
|
2012-06-28 10:27:07 +00:00
|
|
|
'image_medium': fields.function(_get_image, fnct_inv=_set_image,
|
|
|
|
string="Medium-sized photo", type="binary", multi="_get_image",
|
2012-09-07 09:23:33 +00:00
|
|
|
store={
|
2012-06-27 14:58:58 +00:00
|
|
|
'mail.group': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
|
|
|
|
},
|
|
|
|
help="Medium-sized photo of the group. It is automatically "\
|
2012-09-07 09:51:55 +00:00
|
|
|
"resized as a 128x128px image, with aspect ratio preserved. "\
|
2012-06-27 14:58:58 +00:00
|
|
|
"Use this field in form views or some kanban views."),
|
2012-06-28 10:27:07 +00:00
|
|
|
'image_small': fields.function(_get_image, fnct_inv=_set_image,
|
2012-08-06 15:53:54 +00:00
|
|
|
string="Small-sized photo", type="binary", multi="_get_image",
|
2012-09-07 09:23:33 +00:00
|
|
|
store={
|
2012-06-27 14:58:58 +00:00
|
|
|
'mail.group': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
|
|
|
|
},
|
|
|
|
help="Small-sized photo of the group. It is automatically "\
|
2012-09-07 09:51:55 +00:00
|
|
|
"resized as a 64x64px image, with aspect ratio preserved. "\
|
2012-06-28 10:27:07 +00:00
|
|
|
"Use this field anywhere a small image is required."),
|
2013-08-27 15:07:08 +00:00
|
|
|
'alias_id': fields.many2one('mail.alias', 'Alias', ondelete="restrict", required=True,
|
2012-08-16 07:24:02 +00:00
|
|
|
help="The email address associated with this group. New emails received will automatically "
|
|
|
|
"create new topics."),
|
2012-01-31 15:54:12 +00:00
|
|
|
}
|
|
|
|
|
2012-08-14 11:22:04 +00:00
|
|
|
def _get_default_employee_group(self, cr, uid, context=None):
|
|
|
|
ref = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'base', 'group_user')
|
|
|
|
return ref and ref[1] or False
|
|
|
|
|
2012-09-04 13:36:48 +00:00
|
|
|
def _get_default_image(self, cr, uid, context=None):
|
|
|
|
image_path = openerp.modules.get_module_resource('mail', 'static/src/img', 'groupdefault.png')
|
|
|
|
return tools.image_resize_image_big(open(image_path, 'rb').read().encode('base64'))
|
|
|
|
|
2012-01-31 15:54:12 +00:00
|
|
|
_defaults = {
|
2012-08-14 11:29:42 +00:00
|
|
|
'public': 'groups',
|
2012-08-14 11:22:04 +00:00
|
|
|
'group_public_id': _get_default_employee_group,
|
2012-07-30 13:33:43 +00:00
|
|
|
'image': _get_default_image,
|
2012-01-31 15:54:12 +00:00
|
|
|
}
|
2012-07-19 12:00:26 +00:00
|
|
|
|
2012-12-24 13:48:14 +00:00
|
|
|
def _generate_header_description(self, cr, uid, group, context=None):
|
2012-12-24 12:32:15 +00:00
|
|
|
header = ''
|
2012-12-24 13:48:14 +00:00
|
|
|
if group.description:
|
|
|
|
header = '%s' % group.description
|
2013-04-09 11:11:58 +00:00
|
|
|
if group.alias_id and group.alias_name and group.alias_domain:
|
2012-12-24 12:32:15 +00:00
|
|
|
if header:
|
|
|
|
header = '%s<br/>' % header
|
2013-04-09 11:11:58 +00:00
|
|
|
return '%sGroup email gateway: %s@%s' % (header, group.alias_name, group.alias_domain)
|
2012-12-24 12:32:15 +00:00
|
|
|
return header
|
2012-12-24 11:57:03 +00:00
|
|
|
|
2012-08-16 10:18:48 +00:00
|
|
|
def _subscribe_users(self, cr, uid, ids, context=None):
|
|
|
|
for mail_group in self.browse(cr, uid, ids, context=context):
|
|
|
|
partner_ids = []
|
|
|
|
for group in mail_group.group_ids:
|
|
|
|
partner_ids += [user.partner_id.id for user in group.users]
|
|
|
|
self.message_subscribe(cr, uid, ids, partner_ids, context=context)
|
2012-07-19 12:34:39 +00:00
|
|
|
|
2012-07-19 12:00:26 +00:00
|
|
|
def create(self, cr, uid, vals, context=None):
|
2013-04-09 11:11:58 +00:00
|
|
|
if context is None:
|
|
|
|
context = {}
|
2012-08-14 09:07:19 +00:00
|
|
|
|
2012-12-06 12:14:09 +00:00
|
|
|
# get parent menu
|
|
|
|
menu_parent = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'mail', 'mail_group_root')
|
|
|
|
menu_parent = menu_parent and menu_parent[1] or False
|
|
|
|
|
|
|
|
# Create menu id
|
|
|
|
mobj = self.pool.get('ir.ui.menu')
|
|
|
|
menu_id = mobj.create(cr, SUPERUSER_ID, {'name': vals['name'], 'parent_id': menu_parent}, context=context)
|
|
|
|
vals['menu_id'] = menu_id
|
|
|
|
|
|
|
|
# Create group and alias
|
2014-03-20 15:08:55 +00:00
|
|
|
create_context = dict(context, alias_model_name=self._name, alias_parent_model_name=self._name, mail_create_nolog=True)
|
2013-04-09 11:11:58 +00:00
|
|
|
mail_group_id = super(mail_group, self).create(cr, uid, vals, context=create_context)
|
|
|
|
group = self.browse(cr, uid, mail_group_id, context=context)
|
2013-05-16 16:42:07 +00:00
|
|
|
self.pool.get('mail.alias').write(cr, uid, [group.alias_id.id], {"alias_force_thread_id": mail_group_id, 'alias_parent_thread_id': mail_group_id}, context)
|
2012-12-24 11:57:03 +00:00
|
|
|
group = self.browse(cr, uid, mail_group_id, context=context)
|
2012-08-14 09:07:19 +00:00
|
|
|
|
|
|
|
# Create client action for this group and link the menu to it
|
|
|
|
ref = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'mail', 'action_mail_group_feeds')
|
|
|
|
if ref:
|
2012-08-20 15:51:24 +00:00
|
|
|
search_ref = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'mail', 'view_message_search')
|
2012-08-14 09:07:19 +00:00
|
|
|
params = {
|
|
|
|
'search_view_id': search_ref and search_ref[1] or False,
|
2013-03-27 09:58:22 +00:00
|
|
|
'domain': [
|
|
|
|
('model', '=', 'mail.group'),
|
|
|
|
('res_id', '=', mail_group_id),
|
|
|
|
],
|
|
|
|
'context': {
|
|
|
|
'default_model': 'mail.group',
|
|
|
|
'default_res_id': mail_group_id,
|
|
|
|
},
|
2012-08-22 13:37:23 +00:00
|
|
|
'res_model': 'mail.message',
|
2012-09-03 15:20:25 +00:00
|
|
|
'thread_level': 1,
|
2013-03-27 09:58:22 +00:00
|
|
|
'header_description': self._generate_header_description(cr, uid, group, context=context),
|
|
|
|
'view_mailbox': True,
|
|
|
|
'compose_placeholder': 'Send a message to the group',
|
2012-08-14 09:07:19 +00:00
|
|
|
}
|
|
|
|
cobj = self.pool.get('ir.actions.client')
|
2012-09-25 09:48:56 +00:00
|
|
|
newref = cobj.copy(cr, SUPERUSER_ID, ref[1], default={'params': str(params), 'name': vals['name']}, context=context)
|
2012-12-24 11:57:03 +00:00
|
|
|
mobj.write(cr, SUPERUSER_ID, menu_id, {'action': 'ir.actions.client,' + str(newref), 'mail_group_id': mail_group_id}, context=context)
|
2012-08-15 17:08:22 +00:00
|
|
|
|
2012-07-19 12:00:26 +00:00
|
|
|
if vals.get('group_ids'):
|
2012-08-16 10:18:48 +00:00
|
|
|
self._subscribe_users(cr, uid, [mail_group_id], context=context)
|
2012-07-19 12:00:26 +00:00
|
|
|
return mail_group_id
|
|
|
|
|
2012-07-05 12:54:17 +00:00
|
|
|
def unlink(self, cr, uid, ids, context=None):
|
2012-09-13 09:32:46 +00:00
|
|
|
groups = self.browse(cr, uid, ids, context=context)
|
|
|
|
alias_ids = [group.alias_id.id for group in groups if group.alias_id]
|
2014-07-06 14:44:26 +00:00
|
|
|
menu_ids = [group.menu_id.id for group in groups if group.menu_id]
|
2012-09-13 09:32:46 +00:00
|
|
|
# Delete mail_group
|
2014-07-01 10:24:23 +00:00
|
|
|
try:
|
|
|
|
all_emp_group = self.pool['ir.model.data'].get_object_reference(cr, uid, 'mail', 'group_all_employees')[1]
|
|
|
|
except ValueError:
|
|
|
|
all_emp_group = None
|
|
|
|
if all_emp_group and all_emp_group in ids:
|
|
|
|
raise osv.except_osv(_('Warning!'), _('You cannot delete those groups, as the Whole Company group is required by other modules.'))
|
2012-07-05 12:54:17 +00:00
|
|
|
res = super(mail_group, self).unlink(cr, uid, ids, context=context)
|
2014-07-06 14:44:26 +00:00
|
|
|
# Cascade-delete mail aliases as well, as they should not exist without the mail group.
|
|
|
|
self.pool.get('mail.alias').unlink(cr, SUPERUSER_ID, alias_ids, context=context)
|
2012-12-06 12:14:09 +00:00
|
|
|
# Cascade-delete menu entries as well
|
2014-07-06 14:44:26 +00:00
|
|
|
self.pool.get('ir.ui.menu').unlink(cr, SUPERUSER_ID, menu_ids, context=context)
|
2012-07-05 12:54:17 +00:00
|
|
|
return res
|
|
|
|
|
2012-07-19 12:00:26 +00:00
|
|
|
def write(self, cr, uid, ids, vals, context=None):
|
2012-08-16 10:18:48 +00:00
|
|
|
result = super(mail_group, self).write(cr, uid, ids, vals, context=context)
|
2012-07-19 12:00:26 +00:00
|
|
|
if vals.get('group_ids'):
|
2012-10-01 16:44:14 +00:00
|
|
|
self._subscribe_users(cr, uid, ids, context=context)
|
2012-12-24 13:19:22 +00:00
|
|
|
# if description, name or alias is changed: update client action
|
|
|
|
if vals.get('description') or vals.get('name') or vals.get('alias_id') or vals.get('alias_name'):
|
2012-11-14 16:30:07 +00:00
|
|
|
cobj = self.pool.get('ir.actions.client')
|
2012-12-06 12:14:09 +00:00
|
|
|
for action in [group.menu_id.action for group in self.browse(cr, uid, ids, context=context)]:
|
2012-11-14 16:30:07 +00:00
|
|
|
new_params = action.params
|
2012-12-24 13:48:14 +00:00
|
|
|
new_params['header_description'] = self._generate_header_description(cr, uid, group, context=context)
|
2012-11-14 16:30:07 +00:00
|
|
|
cobj.write(cr, SUPERUSER_ID, [action.id], {'params': str(new_params)}, context=context)
|
2012-12-06 12:14:09 +00:00
|
|
|
# if name is changed: update menu
|
|
|
|
if vals.get('name'):
|
|
|
|
mobj = self.pool.get('ir.ui.menu')
|
2012-12-24 11:57:03 +00:00
|
|
|
mobj.write(cr, SUPERUSER_ID,
|
|
|
|
[group.menu_id.id for group in self.browse(cr, uid, ids, context=context)],
|
2012-12-06 12:14:09 +00:00
|
|
|
{'name': vals.get('name')}, context=context)
|
|
|
|
|
2012-08-16 10:18:48 +00:00
|
|
|
return result
|
2012-07-19 12:00:26 +00:00
|
|
|
|
2012-09-04 13:36:48 +00:00
|
|
|
def action_follow(self, cr, uid, ids, context=None):
|
|
|
|
""" Wrapper because message_subscribe_users take a user_ids=None
|
|
|
|
that receive the context without the wrapper. """
|
|
|
|
return self.message_subscribe_users(cr, uid, ids, context=context)
|
|
|
|
|
|
|
|
def action_unfollow(self, cr, uid, ids, context=None):
|
|
|
|
""" Wrapper because message_unsubscribe_users take a user_ids=None
|
|
|
|
that receive the context without the wrapper. """
|
|
|
|
return self.message_unsubscribe_users(cr, uid, ids, context=context)
|
2013-04-03 15:10:56 +00:00
|
|
|
|
|
|
|
def get_suggested_thread(self, cr, uid, removed_suggested_threads=None, context=None):
|
2013-04-03 15:24:43 +00:00
|
|
|
"""Show the suggestion of groups if display_groups_suggestions if the
|
|
|
|
user perference allows it."""
|
2013-04-03 15:10:56 +00:00
|
|
|
user = self.pool.get('res.users').browse(cr, uid, uid, context)
|
|
|
|
if not user.display_groups_suggestions:
|
|
|
|
return []
|
|
|
|
else:
|
|
|
|
return super(mail_group, self).get_suggested_thread(cr, uid, removed_suggested_threads, context)
|
2014-06-12 11:45:21 +00:00
|
|
|
|
|
|
|
def message_get_email_values(self, cr, uid, id, notif_mail=None, context=None):
|
|
|
|
res = super(mail_group, self).message_get_email_values(cr, uid, id, notif_mail=notif_mail, context=context)
|
|
|
|
group = self.browse(cr, uid, id, context=context)
|
2014-08-11 14:02:08 +00:00
|
|
|
headers = {}
|
|
|
|
if res.get('headers'):
|
|
|
|
try:
|
|
|
|
headers.update(eval(res['headers']))
|
|
|
|
except Exception:
|
|
|
|
pass
|
2014-06-20 14:34:27 +00:00
|
|
|
headers['Precedence'] = 'list'
|
2014-07-17 10:33:55 +00:00
|
|
|
# avoid out-of-office replies from MS Exchange
|
|
|
|
# http://blogs.technet.com/b/exchange/archive/2006/10/06/3395024.aspx
|
|
|
|
headers['X-Auto-Response-Suppress'] = 'OOF'
|
2014-06-20 14:34:27 +00:00
|
|
|
if group.alias_domain and group.alias_name:
|
|
|
|
headers['List-Id'] = '%s.%s' % (group.alias_name, group.alias_domain)
|
|
|
|
headers['List-Post'] = '<mailto:%s@%s>' % (group.alias_name, group.alias_domain)
|
2014-07-16 22:16:12 +00:00
|
|
|
# Avoid users thinking it was a personal message
|
|
|
|
# X-Forge-To: will replace To: after SMTP envelope is determined by ir.mail.server
|
|
|
|
list_to = '"%s" <%s@%s>' % (group.name, group.alias_name, group.alias_domain)
|
|
|
|
headers['X-Forge-To'] = list_to
|
2014-08-11 14:02:08 +00:00
|
|
|
res['headers'] = repr(headers)
|
2014-06-12 11:45:21 +00:00
|
|
|
return res
|