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 ##############################################################################
24 import openerp.tools as tools
25 from operator import itemgetter
27 from osv import fields
29 from tools.translate import _
31 class mail_group(osv.osv):
33 A mail_group is a collection of users sharing messages in a discussion
34 group. Group users are users that follow the mail group, using the
35 subscription/follow mechanism of OpenSocial. A mail group has nothing
36 in common wih res.users.group.
37 Additional information on fields:
38 - ``member_ids``: user member of the groups are calculated with
39 ``message_get_subscribers`` method from mail.thread
40 - ``member_count``: calculated with member_ids
41 - ``is_subscriber``: calculated with member_ids
45 _description = 'Discussion group'
47 _inherit = ['mail.thread']
49 def action_group_join(self, cr, uid, ids, context={}):
50 return self.message_subscribe(cr, uid, ids, context=context);
52 def action_group_leave(self, cr, uid, ids, context={}):
53 return self.message_unsubscribe(cr, uid, ids, context=context);
55 def _get_image(self, cr, uid, ids, name, args, context=None):
56 result = dict.fromkeys(ids, False)
57 for obj in self.browse(cr, uid, ids, context=context):
58 resized_image_dict = tools.get_resized_images(obj.image)
60 'image_medium': resized_image_dict['image_medium'],
61 'image_small': resized_image_dict['image_small'],
65 def _set_image(self, cr, uid, id, name, value, args, context=None):
66 return self.write(cr, uid, [id], {'image': tools.resize_image_big(value)}, context=context)
68 def onchange_image(self, cr, uid, ids, value, context=None):
69 return {'value': tools.get_resized_images(value)}
71 def get_member_ids(self, cr, uid, ids, field_names, args, context=None):
74 result = dict.fromkeys(ids)
77 result[id]['member_ids'] = self.message_get_subscribers_ids(cr, uid, [id], context=context)
78 result[id]['member_count'] = len(result[id]['member_ids'])
79 result[id]['is_subscriber'] = uid in result[id]['member_ids']
82 def search_member_ids(self, cr, uid, obj, name, args, context=None):
85 sub_obj = self.pool.get('mail.subscription')
86 sub_ids = sub_obj.search(cr, uid, ['&', ('res_model', '=', obj._name), ('user_id', '=', args[0][2])], context=context)
87 subs = sub_obj.read(cr, uid, sub_ids, context=context)
88 return [('id', 'in', map(itemgetter('res_id'), subs))]
90 def get_last_month_msg_nbr(self, cr, uid, ids, name, args, context=None):
92 message_obj = self.pool.get('mail.message')
94 lower_date = (DT.datetime.now() - DT.timedelta(days=30)).strftime(tools.DEFAULT_SERVER_DATE_FORMAT)
95 result[id] = message_obj.search(cr, uid, ['&', '&', ('model', '=', self._name), ('res_id', 'in', ids), ('date', '>=', lower_date)], count=True, context=context)
98 def _get_photo(self, cr, uid, context=None):
99 image_path = openerp.modules.get_module_resource('mail', 'static/src/img', 'groupdefault.png')
100 return tools.resize_image_big(open(image_path, 'rb').read().encode('base64'))
103 'name': fields.char('Name', size=64, required=True),
104 'description': fields.text('Description'),
105 'responsible_id': fields.many2one('res.users', string='Responsible',
106 ondelete='set null', required=True, select=1,
107 help="Responsible of the group that has all rights on the record."),
108 'public': fields.boolean('Public', help='This group is visible by non members. Invisible groups can add members through the invite button.'),
109 'image': fields.binary("Photo",
110 help="This field holds the image used as photo for the "\
111 "user. The image is base64 encoded, and PIL-supported. "\
112 "It is limited to a 12024x1024 px image."),
113 'image_medium': fields.function(_get_image, fnct_inv=_set_image,
114 string="Medium-sized photo", type="binary", multi="_get_image",
116 'mail.group': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
118 help="Medium-sized photo of the group. It is automatically "\
119 "resized as a 180x180px image, with aspect ratio kept. "\
120 "Use this field in form views or some kanban views."),
121 'image_small': fields.function(_get_image, fnct_inv=_set_image,
122 string="Smal-sized photo", type="binary", multi="_get_image",
124 'mail.group': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
126 help="Small-sized photo of the group. It is automatically "\
127 "resized as a 50x50px image, with aspect ratio keps. "\
128 "Use this field anywhere a small image is required."),
129 'member_ids': fields.function(get_member_ids, fnct_search=search_member_ids, type='many2many',
130 relation='res.users', string='Group members', multi='get_member_ids'),
131 'member_count': fields.function(get_member_ids, type='integer', string='Member count', multi='get_member_ids'),
132 'is_subscriber': fields.function(get_member_ids, type='boolean', string='Joined', multi='get_member_ids'),
133 'last_month_msg_nbr': fields.function(get_last_month_msg_nbr, type='integer', string='Messages count for last month'),
138 'responsible_id': (lambda s, cr, uid, ctx: uid),