1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2010-today OpenERP SA (<http://www.openerp.com>)
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
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
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/>
20 ##############################################################################
23 import openerp.tools as tools
25 from osv import fields
28 class mail_group(osv.Model):
29 """ A mail_group is a collection of users sharing messages in a discussion
30 group. The group mechanics are based on the followers. """
31 _description = 'Discussion group'
33 _inherit = ['mail.thread']
34 _inherits = {'mail.alias': 'alias_id', 'ir.ui.menu': 'menu_id'}
36 def _get_image(self, cr, uid, ids, name, args, context=None):
37 result = dict.fromkeys(ids, False)
38 for obj in self.browse(cr, uid, ids, context=context):
39 result[obj.id] = tools.image_get_resized_images(obj.image)
42 def _set_image(self, cr, uid, id, name, value, args, context=None):
43 return self.write(cr, uid, [id], {'image': tools.image_resize_image_big(value)}, context=context)
46 'description': fields.text('Description'),
47 'menu_id': fields.many2one('ir.ui.menu', string='Related Menu', required=True, ondelete="cascade"),
48 'public': fields.selection([('public', 'Public'), ('private', 'Private'), ('groups', 'Selected Group Only')], 'Privacy', required=True,
49 help='This group is visible by non members. \
50 Invisible groups can add members through the invite button.'),
51 'group_public_id': fields.many2one('res.groups', string='Authorized Group'),
52 'group_ids': fields.many2many('res.groups', rel='mail_group_res_group_rel',
53 id1='mail_group_id', id2='groups_id', string='Auto Subscription',
54 help="Members of those groups will automatically added as followers. "\
55 "Note that they will be able to manage their subscription manually "\
57 # image: all image fields are base64 encoded and PIL-supported
58 'image': fields.binary("Photo",
59 help="This field holds the image used as photo for the group, limited to 1024x1024px."),
60 'image_medium': fields.function(_get_image, fnct_inv=_set_image,
61 string="Medium-sized photo", type="binary", multi="_get_image",
63 'mail.group': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
65 help="Medium-sized photo of the group. It is automatically "\
66 "resized as a 128x128px image, with aspect ratio preserved. "\
67 "Use this field in form views or some kanban views."),
68 'image_small': fields.function(_get_image, fnct_inv=_set_image,
69 string="Small-sized photo", type="binary", multi="_get_image",
71 'mail.group': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
73 help="Small-sized photo of the group. It is automatically "\
74 "resized as a 64x64px image, with aspect ratio preserved. "\
75 "Use this field anywhere a small image is required."),
76 'alias_id': fields.many2one('mail.alias', 'Alias', ondelete="cascade", required=True,
77 help="The email address associated with this group. New emails received will automatically "
78 "create new topics."),
81 def _get_default_employee_group(self, cr, uid, context=None):
82 ref = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'base', 'group_user')
83 return ref and ref[1] or False
85 def _get_default_image(self, cr, uid, context=None):
86 image_path = openerp.modules.get_module_resource('mail', 'static/src/img', 'groupdefault.png')
87 return tools.image_resize_image_big(open(image_path, 'rb').read().encode('base64'))
89 def _get_menu_parent(self, cr, uid, context=None):
90 ref = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'mail', 'mail_group_root')
91 return ref and ref[1] or False
95 'group_public_id': _get_default_employee_group,
96 'image': _get_default_image,
97 'parent_id': _get_menu_parent,
98 'alias_domain': False, # always hide alias during creation
101 def _subscribe_users(self, cr, uid, ids, context=None):
102 for mail_group in self.browse(cr, uid, ids, context=context):
104 for group in mail_group.group_ids:
105 partner_ids += [user.partner_id.id for user in group.users]
106 self.message_subscribe(cr, uid, ids, partner_ids, context=context)
108 def create(self, cr, uid, vals, context=None):
109 mail_alias = self.pool.get('mail.alias')
110 if not vals.get('alias_id'):
111 vals.pop('alias_name', None) # prevent errors during copy()
112 alias_id = mail_alias.create_unique_alias(cr, uid,
113 # Using '+' allows using subaddressing for those who don't
114 # have a catchall domain setup.
115 {'alias_name': "group+"+vals['name']},
116 model_name=self._name, context=context)
117 vals['alias_id'] = alias_id
119 mail_group_id = super(mail_group, self).create(cr, uid, vals, context)
121 # Create client action for this group and link the menu to it
122 ref = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'mail', 'action_mail_group_feeds')
124 search_ref = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'mail', 'view_message_search')
126 'search_view_id': search_ref and search_ref[1] or False,
127 'domain': [('model', '=', 'mail.group'), ('res_id', '=', mail_group_id)],
128 'context': {'default_model': 'mail.group', 'default_res_id': mail_group_id, 'mail_keep_unread':True},
129 'res_model': 'mail.message',
132 cobj = self.pool.get('ir.actions.client')
133 newref = cobj.copy(cr, uid, ref[1], default={'params': str(params), 'name': vals['name']}, context=context)
134 self.write(cr, uid, [mail_group_id], {'action': 'ir.actions.client,' + str(newref), 'mail_group_id': mail_group_id}, context=context)
136 mail_alias.write(cr, uid, [vals['alias_id']], {"alias_force_thread_id": mail_group_id}, context)
138 if vals.get('group_ids'):
139 self._subscribe_users(cr, uid, [mail_group_id], context=context)
142 def unlink(self, cr, uid, ids, context=None):
143 groups = self.browse(cr, uid, ids, context=context)
144 # Cascade-delete mail aliases as well, as they should not exist without the mail group.
145 mail_alias = self.pool.get('mail.alias')
146 alias_ids = [group.alias_id.id for group in groups if group.alias_id]
147 # Cascade-delete menu entries as well
148 self.pool.get('ir.ui.menu').unlink(cr, uid, [group.menu_id.id for group in groups if group.menu_id], context=context)
150 res = super(mail_group, self).unlink(cr, uid, ids, context=context)
151 mail_alias.unlink(cr, uid, alias_ids, context=context)
154 def write(self, cr, uid, ids, vals, context=None):
155 result = super(mail_group, self).write(cr, uid, ids, vals, context=context)
156 if vals.get('group_ids'):
157 self._subscribe_users(cr, uid, ids, vals.get('group_ids'), context=context)
160 def action_follow(self, cr, uid, ids, context=None):
161 """ Wrapper because message_subscribe_users take a user_ids=None
162 that receive the context without the wrapper. """
163 return self.message_subscribe_users(cr, uid, ids, context=context)
165 def action_unfollow(self, cr, uid, ids, context=None):
166 """ Wrapper because message_unsubscribe_users take a user_ids=None
167 that receive the context without the wrapper. """
168 return self.message_unsubscribe_users(cr, uid, ids, context=context)