[IMP] mail.group: add the group email gateway in the client action description.
[odoo/odoo.git] / addons / mail / mail_group.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2010-today OpenERP SA (<http://www.openerp.com>)
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>
19 #
20 ##############################################################################
21
22 import openerp
23 import openerp.tools as tools
24 from openerp.osv import osv
25 from openerp.osv import fields
26 from openerp import SUPERUSER_ID
27
28
29 class mail_group(osv.Model):
30     """ A mail_group is a collection of users sharing messages in a discussion
31         group. The group mechanics are based on the followers. """
32     _description = 'Discussion group'
33     _name = 'mail.group'
34     _mail_flat_thread = False
35     _inherit = ['mail.thread']
36     _inherits = {'mail.alias': 'alias_id'}
37
38     def _get_image(self, cr, uid, ids, name, args, context=None):
39         result = dict.fromkeys(ids, False)
40         for obj in self.browse(cr, uid, ids, context=context):
41             result[obj.id] = tools.image_get_resized_images(obj.image)
42         return result
43
44     def _set_image(self, cr, uid, id, name, value, args, context=None):
45         return self.write(cr, uid, [id], {'image': tools.image_resize_image_big(value)}, context=context)
46
47     _columns = {
48         'name': fields.char('Name', size=64, required=True, translate=True),
49         'description': fields.text('Description'),
50         'menu_id': fields.many2one('ir.ui.menu', string='Related Menu', required=True, ondelete="cascade"),
51         'public': fields.selection([('public', 'Public'), ('private', 'Private'), ('groups', 'Selected Group Only')], 'Privacy', required=True,
52             help='This group is visible by non members. \
53             Invisible groups can add members through the invite button.'),
54         'group_public_id': fields.many2one('res.groups', string='Authorized Group'),
55         'group_ids': fields.many2many('res.groups', rel='mail_group_res_group_rel',
56             id1='mail_group_id', id2='groups_id', string='Auto Subscription',
57             help="Members of those groups will automatically added as followers. "\
58                  "Note that they will be able to manage their subscription manually "\
59                  "if necessary."),
60         # image: all image fields are base64 encoded and PIL-supported
61         'image': fields.binary("Photo",
62             help="This field holds the image used as photo for the group, limited to 1024x1024px."),
63         'image_medium': fields.function(_get_image, fnct_inv=_set_image,
64             string="Medium-sized photo", type="binary", multi="_get_image",
65             store={
66                 'mail.group': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
67             },
68             help="Medium-sized photo of the group. It is automatically "\
69                  "resized as a 128x128px image, with aspect ratio preserved. "\
70                  "Use this field in form views or some kanban views."),
71         'image_small': fields.function(_get_image, fnct_inv=_set_image,
72             string="Small-sized photo", type="binary", multi="_get_image",
73             store={
74                 'mail.group': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
75             },
76             help="Small-sized photo of the group. It is automatically "\
77                  "resized as a 64x64px image, with aspect ratio preserved. "\
78                  "Use this field anywhere a small image is required."),
79         'alias_id': fields.many2one('mail.alias', 'Alias', ondelete="cascade", required=True,
80             help="The email address associated with this group. New emails received will automatically "
81                  "create new topics."),
82     }
83
84     def _get_default_employee_group(self, cr, uid, context=None):
85         ref = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'base', 'group_user')
86         return ref and ref[1] or False
87
88     def _get_default_image(self, cr, uid, context=None):
89         image_path = openerp.modules.get_module_resource('mail', 'static/src/img', 'groupdefault.png')
90         return tools.image_resize_image_big(open(image_path, 'rb').read().encode('base64'))
91
92     _defaults = {
93         'public': 'groups',
94         'group_public_id': _get_default_employee_group,
95         'image': _get_default_image,
96         'alias_domain': False,  # always hide alias during creation
97     }
98
99     def _generate_header_description(self, cr, uid, description, group, context=None):
100         if group.alias_id and group.alias_id.alias_name and group.alias_id.alias_domain:
101             return '%s<br />Group email gateway: %s@%s' % (description, group.alias_id.alias_name, group.alias_id.alias_domain)
102         else:
103             return '%s' % description
104
105     def _subscribe_users(self, cr, uid, ids, context=None):
106         for mail_group in self.browse(cr, uid, ids, context=context):
107             partner_ids = []
108             for group in mail_group.group_ids:
109                 partner_ids += [user.partner_id.id for user in group.users]
110             self.message_subscribe(cr, uid, ids, partner_ids, context=context)
111
112     def create(self, cr, uid, vals, context=None):
113         mail_alias = self.pool.get('mail.alias')
114         if not vals.get('alias_id'):
115             vals.pop('alias_name', None)  # prevent errors during copy()
116             alias_id = mail_alias.create_unique_alias(cr, uid,
117                           # Using '+' allows using subaddressing for those who don't
118                           # have a catchall domain setup.
119                           {'alias_name': "group+" + vals['name']},
120                           model_name=self._name, context=context)
121             vals['alias_id'] = alias_id
122
123         # get parent menu
124         menu_parent = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'mail', 'mail_group_root')
125         menu_parent = menu_parent and menu_parent[1] or False
126
127         # Create menu id
128         mobj = self.pool.get('ir.ui.menu')
129         menu_id = mobj.create(cr, SUPERUSER_ID, {'name': vals['name'], 'parent_id': menu_parent}, context=context)
130         vals['menu_id'] = menu_id
131
132         # Create group and alias
133         mail_group_id = super(mail_group, self).create(cr, uid, vals, context=context)
134         mail_alias.write(cr, uid, [vals['alias_id']], {"alias_force_thread_id": mail_group_id}, context)
135         group = self.browse(cr, uid, mail_group_id, context=context)
136
137         # Create client action for this group and link the menu to it
138         ref = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'mail', 'action_mail_group_feeds')
139         if ref:
140             search_ref = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'mail', 'view_message_search')
141             params = {
142                 'search_view_id': search_ref and search_ref[1] or False,
143                 'domain': [('model', '=', 'mail.group'), ('res_id', '=', mail_group_id)],
144                 'context': {'default_model': 'mail.group', 'default_res_id': mail_group_id, 'search_default_message_unread': True},
145                 'res_model': 'mail.message',
146                 'thread_level': 1,
147                 'header_description': self._generate_header_description(cr, uid, vals.get('description'), group, context=context)
148             }
149             cobj = self.pool.get('ir.actions.client')
150             newref = cobj.copy(cr, SUPERUSER_ID, ref[1], default={'params': str(params), 'name': vals['name']}, context=context)
151             mobj.write(cr, SUPERUSER_ID, menu_id, {'action': 'ir.actions.client,' + str(newref), 'mail_group_id': mail_group_id}, context=context)
152
153         if vals.get('group_ids'):
154             self._subscribe_users(cr, uid, [mail_group_id], context=context)
155         return mail_group_id
156
157     def unlink(self, cr, uid, ids, context=None):
158         groups = self.browse(cr, uid, ids, context=context)
159         # Cascade-delete mail aliases as well, as they should not exist without the mail group.
160         mail_alias = self.pool.get('mail.alias')
161         alias_ids = [group.alias_id.id for group in groups if group.alias_id]
162         # Delete mail_group
163         res = super(mail_group, self).unlink(cr, uid, ids, context=context)
164         # Delete alias
165         mail_alias.unlink(cr, SUPERUSER_ID, alias_ids, context=context)
166         # Cascade-delete menu entries as well
167         self.pool.get('ir.ui.menu').unlink(cr, SUPERUSER_ID, [group.menu_id.id for group in groups if group.menu_id], context=context)
168         return res
169
170     def write(self, cr, uid, ids, vals, context=None):
171         result = super(mail_group, self).write(cr, uid, ids, vals, context=context)
172         if vals.get('group_ids'):
173             self._subscribe_users(cr, uid, ids, context=context)
174         # if description is changed: update client action
175         if vals.get('description'):
176             cobj = self.pool.get('ir.actions.client')
177             for action in [group.menu_id.action for group in self.browse(cr, uid, ids, context=context)]:
178                 new_params = action.params
179                 new_params['header_description'] = self._generate_header_description(cr, uid, vals.get('description'), group, context=context)
180                 cobj.write(cr, SUPERUSER_ID, [action.id], {'params': str(new_params)}, context=context)
181         # if name is changed: update menu
182         if vals.get('name'):
183             mobj = self.pool.get('ir.ui.menu')
184             mobj.write(cr, SUPERUSER_ID,
185                 [group.menu_id.id for group in self.browse(cr, uid, ids, context=context)],
186                 {'name': vals.get('name')}, context=context)
187
188         return result
189
190     def action_follow(self, cr, uid, ids, context=None):
191         """ Wrapper because message_subscribe_users take a user_ids=None
192             that receive the context without the wrapper. """
193         return self.message_subscribe_users(cr, uid, ids, context=context)
194
195     def action_unfollow(self, cr, uid, ids, context=None):
196         """ Wrapper because message_unsubscribe_users take a user_ids=None
197             that receive the context without the wrapper. """
198         return self.message_unsubscribe_users(cr, uid, ids, context=context)