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 datetime as DT
|
|
|
|
import openerp
|
|
|
|
import openerp.tools as tools
|
2012-04-02 13:43:54 +00:00
|
|
|
from operator import itemgetter
|
2012-01-31 15:54:12 +00:00
|
|
|
from osv import osv
|
|
|
|
from osv import fields
|
2012-03-29 07:32:09 +00:00
|
|
|
from tools.translate import _
|
2012-03-07 15:34:45 +00:00
|
|
|
|
2012-08-14 11:40:21 +00:00
|
|
|
class mail_group(osv.Model):
|
2012-01-31 15:54:12 +00:00
|
|
|
"""
|
2012-03-13 16:19:02 +00:00
|
|
|
A mail_group is a collection of users sharing messages in a discussion
|
|
|
|
group. Group users are users that follow the mail group, using the
|
2012-04-04 14:11:19 +00:00
|
|
|
subscription/follow mechanism of OpenSocial. A mail group has nothing
|
2012-07-19 12:00:26 +00:00
|
|
|
in common with res.users.group.
|
2012-01-31 15:54:12 +00:00
|
|
|
"""
|
2012-03-29 16:10:00 +00:00
|
|
|
_description = 'Discussion group'
|
2012-01-31 15:54:12 +00:00
|
|
|
_name = 'mail.group'
|
2012-08-20 19:10:58 +00:00
|
|
|
_inherit = ['mail.thread']
|
2012-08-14 09:07:19 +00:00
|
|
|
_inherits = {'mail.alias': 'alias_id', 'ir.ui.menu': 'menu_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):
|
2012-06-27 14:58:58 +00:00
|
|
|
result = dict.fromkeys(ids, False)
|
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-07-30 13:33:43 +00:00
|
|
|
def _get_default_image(self, cr, uid, context=None):
|
2012-06-27 14:58:58 +00:00
|
|
|
image_path = openerp.modules.get_module_resource('mail', 'static/src/img', 'groupdefault.png')
|
2012-08-07 11:10:39 +00:00
|
|
|
return tools.image_resize_image_big(open(image_path, 'rb').read().encode('base64'))
|
2012-08-15 17:08:22 +00:00
|
|
|
|
2012-01-31 15:54:12 +00:00
|
|
|
_columns = {
|
|
|
|
'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-08-14 11:22:04 +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-06-27 14:58:58 +00:00
|
|
|
'image': fields.binary("Photo",
|
2012-06-28 10:27:07 +00:00
|
|
|
help="This field holds the image used as photo for the "\
|
|
|
|
"user. The image is base64 encoded, and PIL-supported. "\
|
2012-08-16 09:02:43 +00:00
|
|
|
"It is limited to a 1024x1024 px image."),
|
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-06-27 14:58:58 +00:00
|
|
|
store = {
|
|
|
|
'mail.group': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
|
|
|
|
},
|
|
|
|
help="Medium-sized photo of the group. It is automatically "\
|
2012-07-30 13:33:43 +00:00
|
|
|
"resized as a 180x180px 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-03-07 15:34:45 +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-07-30 13:33:43 +00:00
|
|
|
"resized as a 50x50px image, with aspect ratio preserved. "\
|
2012-06-28 10:27:07 +00:00
|
|
|
"Use this field anywhere a small image is required."),
|
2012-08-17 07:02:18 +00:00
|
|
|
'alias_id': fields.many2one('mail.alias', 'Alias', ondelete="cascade", 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-08-14 09:07:19 +00:00
|
|
|
def _get_menu_parent(self, cr, uid, context=None):
|
|
|
|
ref = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'mail', 'mail_group_root')
|
|
|
|
return ref and ref[1] or False
|
|
|
|
|
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-08-14 09:07:19 +00:00
|
|
|
'parent_id': _get_menu_parent,
|
2012-08-15 17:08:22 +00:00
|
|
|
'alias_domain': False, # always hide alias during creation
|
2012-01-31 15:54:12 +00:00
|
|
|
}
|
2012-07-19 12:00:26 +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):
|
2012-08-07 14:08:21 +00:00
|
|
|
mail_alias = self.pool.get('mail.alias')
|
2012-07-04 07:19:02 +00:00
|
|
|
if not vals.get('alias_id'):
|
2012-08-07 14:08:21 +00:00
|
|
|
vals.pop('alias_name', None) # prevent errors during copy()
|
2012-08-15 17:08:22 +00:00
|
|
|
alias_id = mail_alias.create_unique_alias(cr, uid,
|
2012-08-07 14:08:21 +00:00
|
|
|
# Using '+' allows using subaddressing for those who don't
|
|
|
|
# have a catchall domain setup.
|
|
|
|
{'alias_name': "group+"+vals['name']},
|
|
|
|
model_name=self._name, context=context)
|
2012-08-06 00:44:17 +00:00
|
|
|
vals['alias_id'] = alias_id
|
2012-08-14 09:07:19 +00:00
|
|
|
|
2012-08-03 14:24:50 +00:00
|
|
|
mail_group_id = super(mail_group, self).create(cr, uid, vals, 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,
|
|
|
|
'domain': [('model','=','mail.group'),('res_id','=',mail_group_id)],
|
2012-08-22 13:37:23 +00:00
|
|
|
'res_model': 'mail.message',
|
2012-08-14 09:07:19 +00:00
|
|
|
'thread_level': 2
|
|
|
|
}
|
|
|
|
cobj = self.pool.get('ir.actions.client')
|
|
|
|
newref = cobj.copy(cr, uid, ref[1], default={'params': str(params), 'name': vals['name']}, context=context)
|
|
|
|
self.write(cr, uid, [mail_group_id], {'action': 'ir.actions.client,'+str(newref), 'mail_group_id': mail_group_id}, context=context)
|
|
|
|
|
2012-08-07 14:08:21 +00:00
|
|
|
mail_alias.write(cr, uid, [vals['alias_id']], {"alias_force_thread_id": mail_group_id}, 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-07-05 14:17:06 +00:00
|
|
|
# Cascade-delete mail aliases as well, as they should not exist without the mail group.
|
|
|
|
mail_alias = self.pool.get('mail.alias')
|
|
|
|
alias_ids = [group.alias_id.id for group in self.browse(cr, uid, ids, context=context) if group.alias_id]
|
2012-07-05 12:54:17 +00:00
|
|
|
res = super(mail_group, self).unlink(cr, uid, ids, context=context)
|
2012-07-05 14:17:06 +00:00
|
|
|
mail_alias.unlink(cr, uid, alias_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-08-16 10:18:48 +00:00
|
|
|
self._subscribe_users(cr, uid, ids, vals.get('group_ids'), context=context)
|
|
|
|
return result
|
2012-07-19 12:00:26 +00:00
|
|
|
|