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 io
|
|
|
|
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-07 15:34:45 +00:00
|
|
|
from PIL import Image
|
2012-03-29 07:32:09 +00:00
|
|
|
import StringIO
|
|
|
|
import tools
|
|
|
|
from tools.translate import _
|
2012-03-07 15:34:45 +00:00
|
|
|
|
2012-01-31 15:54:12 +00:00
|
|
|
class mail_group(osv.osv):
|
|
|
|
"""
|
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
|
|
|
|
in common wih res.users.group.
|
|
|
|
Additional information on fields:
|
|
|
|
- ``member_ids``: user member of the groups are calculated with
|
|
|
|
``message_get_subscribers`` method from mail.thread
|
|
|
|
- ``member_count``: calculated with member_ids
|
|
|
|
- ``is_subscriber``: calculated with member_ids
|
|
|
|
|
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-02-02 09:48:45 +00:00
|
|
|
_inherit = ['mail.thread']
|
2012-03-07 15:34:45 +00:00
|
|
|
|
2012-04-04 14:11:19 +00:00
|
|
|
def action_group_join(self, cr, uid, ids, context={}):
|
|
|
|
return self.message_subscribe(cr, uid, ids, context=context);
|
|
|
|
|
|
|
|
def action_group_leave(self, cr, uid, ids, context={}):
|
|
|
|
return self.message_unsubscribe(cr, uid, ids, context=context);
|
|
|
|
|
2012-06-27 14:58:58 +00:00
|
|
|
def _get_image_resized(self, cr, uid, ids, name, args, context=None):
|
|
|
|
result = dict.fromkeys(ids, False)
|
|
|
|
for mail_group in self.browse(cr, uid, ids, context=context):
|
|
|
|
result[mail_group.id] = {'image_medium': False, 'image_small': False}
|
|
|
|
if mail_group.image:
|
|
|
|
result[mail_group.id]['image_medium'] = tools.resize_image_medium(mail_group.image)
|
|
|
|
result[mail_group.id]['image_small'] = tools.resize_image_small(mail_group.image)
|
|
|
|
return result
|
2012-03-07 15:34:45 +00:00
|
|
|
|
2012-06-27 14:58:58 +00:00
|
|
|
def _set_image_resized(self, cr, uid, id, name, value, args, context=None):
|
|
|
|
if not value:
|
|
|
|
vals = {'image': value}
|
2012-03-29 07:32:09 +00:00
|
|
|
else:
|
2012-06-27 14:58:58 +00:00
|
|
|
vals = {'image': tools.resize_image_big(value)}
|
|
|
|
return self.write(cr, uid, [id], vals, context=context)
|
2012-02-03 11:21:16 +00:00
|
|
|
|
2012-06-27 14:58:58 +00:00
|
|
|
def onchange_image(self, cr, uid, ids, value, context=None):
|
|
|
|
if not value:
|
|
|
|
return {'value': {
|
|
|
|
'image': value,
|
|
|
|
'image_medium': value,
|
|
|
|
'image_small': value,
|
|
|
|
}}
|
|
|
|
return {'value': {
|
|
|
|
'image': tools.resize_image_big(value),
|
|
|
|
'image_medium': tools.resize_image_medium(value),
|
|
|
|
'image_small': tools.resize_image_small(value),
|
|
|
|
}}
|
2012-03-13 16:19:02 +00:00
|
|
|
|
2012-03-29 16:10:00 +00:00
|
|
|
def get_member_ids(self, cr, uid, ids, field_names, args, context=None):
|
|
|
|
if context is None:
|
|
|
|
context = {}
|
|
|
|
result = dict.fromkeys(ids)
|
2012-03-07 17:02:54 +00:00
|
|
|
for id in ids:
|
2012-03-29 16:10:00 +00:00
|
|
|
result[id] = {}
|
|
|
|
result[id]['member_ids'] = self.message_get_subscribers_ids(cr, uid, [id], context=context)
|
2012-04-02 13:43:54 +00:00
|
|
|
result[id]['member_count'] = len(result[id]['member_ids'])
|
|
|
|
result[id]['is_subscriber'] = uid in result[id]['member_ids']
|
2012-03-07 17:02:54 +00:00
|
|
|
return result
|
|
|
|
|
2012-03-29 16:10:00 +00:00
|
|
|
def search_member_ids(self, cr, uid, obj, name, args, context=None):
|
|
|
|
if context is None:
|
|
|
|
context = {}
|
|
|
|
sub_obj = self.pool.get('mail.subscription')
|
|
|
|
sub_ids = sub_obj.search(cr, uid, ['&', ('res_model', '=', obj._name), ('user_id', '=', args[0][2])], context=context)
|
|
|
|
subs = sub_obj.read(cr, uid, sub_ids, context=context)
|
2012-04-02 13:43:54 +00:00
|
|
|
return [('id', 'in', map(itemgetter('res_id'), subs))]
|
2012-03-29 16:10:00 +00:00
|
|
|
|
2012-03-29 07:32:09 +00:00
|
|
|
def get_last_month_msg_nbr(self, cr, uid, ids, name, args, context=None):
|
2012-03-07 17:23:50 +00:00
|
|
|
result = {}
|
2012-03-29 07:32:09 +00:00
|
|
|
message_obj = self.pool.get('mail.message')
|
2012-03-07 17:23:50 +00:00
|
|
|
for id in ids:
|
2012-03-29 07:32:09 +00:00
|
|
|
lower_date = (DT.datetime.now() - DT.timedelta(days=30)).strftime(tools.DEFAULT_SERVER_DATE_FORMAT)
|
|
|
|
result[id] = message_obj.search(cr, uid, ['&', '&', ('model', '=', self._name), ('res_id', 'in', ids), ('date', '>=', lower_date)], count=True, context=context)
|
2012-03-07 17:23:50 +00:00
|
|
|
return result
|
|
|
|
|
2012-06-27 14:58:58 +00:00
|
|
|
def _get_photo(self, cr, uid, context=None):
|
|
|
|
image_path = openerp.modules.get_module_resource('mail', 'static/src/img', 'groupdefault.png')
|
|
|
|
return tools.resize_image_big(open(image_path, 'rb').read().encode('base64'))
|
2012-03-29 07:32:09 +00:00
|
|
|
|
2012-01-31 15:54:12 +00:00
|
|
|
_columns = {
|
2012-02-02 09:48:45 +00:00
|
|
|
'name': fields.char('Name', size=64, required=True),
|
2012-01-31 15:54:12 +00:00
|
|
|
'description': fields.text('Description'),
|
|
|
|
'responsible_id': fields.many2one('res.users', string='Responsible',
|
2012-04-02 13:43:54 +00:00
|
|
|
ondelete='set null', required=True, select=1,
|
|
|
|
help="Responsible of the group that has all rights on the record."),
|
|
|
|
'public': fields.boolean('Public', help='This group is visible by non members. Invisible groups can add members through the invite button.'),
|
2012-06-27 14:58:58 +00:00
|
|
|
'image': fields.binary("Photo",
|
|
|
|
help="This field holds the photo used as image for the "\
|
|
|
|
"group. The avatar field is used as an interface to "\
|
|
|
|
"access this field. The image is base64 encoded, "\
|
|
|
|
"and PIL-supported. It is stored as a 540x450 px "\
|
|
|
|
"image, in case a bigger image must be used."),
|
|
|
|
'image_medium': fields.function(_get_image_resized, fnct_inv=_set_image_resized,
|
|
|
|
string="Medium-sized photo", type="binary", multi="_get_image_resized",
|
|
|
|
store = {
|
|
|
|
'mail.group': (lambda self, cr, uid, ids, c={}: ids, ['image'], 10),
|
|
|
|
},
|
|
|
|
help="Medium-sized photo of the group. It is automatically "\
|
|
|
|
"resized as a 180x180px image, with aspect ratio keps. "\
|
|
|
|
"Use this field in form views or some kanban views."),
|
|
|
|
'image_small': fields.function(_get_image_resized, fnct_inv=_set_image_resized,
|
|
|
|
string="Smal-sized photo", type="binary", multi="_get_image_resized",
|
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 "\
|
|
|
|
"resized as a 50x50px image, with aspect ratio keps. "\
|
|
|
|
"Use this field in form views or some kanban views."),
|
2012-03-29 16:10:00 +00:00
|
|
|
'member_ids': fields.function(get_member_ids, fnct_search=search_member_ids, type='many2many',
|
2012-04-02 13:43:54 +00:00
|
|
|
relation='res.users', string='Group members', multi='get_member_ids'),
|
|
|
|
'member_count': fields.function(get_member_ids, type='integer', string='Member count', multi='get_member_ids'),
|
|
|
|
'is_subscriber': fields.function(get_member_ids, type='boolean', string='Joined', multi='get_member_ids'),
|
2012-03-29 07:32:09 +00:00
|
|
|
'last_month_msg_nbr': fields.function(get_last_month_msg_nbr, type='integer', string='Messages count for last month'),
|
2012-01-31 15:54:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_defaults = {
|
|
|
|
'public': True,
|
2012-03-13 16:19:02 +00:00
|
|
|
'responsible_id': (lambda s, cr, uid, ctx: uid),
|
2012-06-27 14:58:58 +00:00
|
|
|
'image_medium': _get_photo,
|
2012-01-31 15:54:12 +00:00
|
|
|
}
|