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-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
subscription / follow mechanism of OpenSocial .
2012-01-31 15:54:12 +00:00
"""
_name = ' mail.group '
2012-02-02 09:48:45 +00:00
_inherit = [ ' mail.thread ' ]
2012-01-31 15:54:12 +00:00
def action_group_join ( self , cr , uid , ids , context = { } ) :
2012-03-07 17:02:54 +00:00
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-03-07 15:34:45 +00:00
2012-03-29 07:32:09 +00:00
def onchange_photo ( self , cr , uid , ids , value , context = None ) :
if not value :
return { ' value ' : { ' avatar_big ' : value , ' avatar ' : value } }
return { ' value ' : { ' photo_big ' : value , ' photo ' : self . _photo_resize ( cr , uid , value ) } }
2012-03-07 15:34:45 +00:00
2012-03-29 07:32:09 +00:00
def _set_photo ( self , cr , uid , id , name , value , args , context = None ) :
if value :
return self . write ( cr , uid , [ id ] , { ' photo_big ' : value } , context = context )
else :
return self . write ( cr , uid , [ id ] , { ' photo_big ' : value } , context = context )
2012-02-03 11:21:16 +00:00
2012-03-29 07:32:09 +00:00
def _photo_resize ( self , cr , uid , photo , width = 128 , height = 128 , context = None ) :
2012-03-13 16:19:02 +00:00
image_stream = io . BytesIO ( photo . decode ( ' base64 ' ) )
img = Image . open ( image_stream )
2012-03-29 07:32:09 +00:00
img . thumbnail ( ( width , height ) , Image . ANTIALIAS )
2012-03-13 16:19:02 +00:00
img_stream = StringIO . StringIO ( )
img . save ( img_stream , " JPEG " )
return img_stream . getvalue ( ) . encode ( ' base64 ' )
2012-03-29 07:32:09 +00:00
def _get_photo ( self , cr , uid , ids , name , args , context = None ) :
result = dict . fromkeys ( ids , False )
2012-03-13 16:19:02 +00:00
for group in self . browse ( cr , uid , ids , context = context ) :
2012-03-29 07:32:09 +00:00
if group . photo_big :
result [ group . id ] = self . _photo_resize ( cr , uid , group . photo_big , context = context )
2012-03-13 16:19:02 +00:00
return result
2012-03-07 17:02:54 +00:00
def is_subscriber ( self , cr , uid , ids , name , args , context = None ) :
result = { }
for id in ids :
result [ id ] = self . message_is_subscriber ( cr , uid , [ id ] , context = context )
return result
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
def get_members_nbr ( self , cr , uid , ids , name , args , context = None ) :
result = { }
for id in ids :
2012-03-13 16:19:02 +00:00
result [ id ] = len ( self . message_get_subscribers_ids ( cr , uid , [ id ] , context = context ) )
2012-03-07 17:23:50 +00:00
return result
2012-03-29 07:32:09 +00:00
def _get_default_photo ( self , cr , uid , context = None ) :
avatar_path = openerp . modules . get_module_resource ( ' mail ' , ' static/src/img ' , ' groupdefault.png ' )
return self . _photo_resize ( cr , uid , open ( avatar_path , ' rb ' ) . read ( ) . encode ( ' base64 ' ) , context = context )
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-03-07 15:34:45 +00:00
ondelete = ' set null ' , required = True , select = 1 ) ,
' public ' : fields . boolean ( ' Public ' , help = ' This group is visible by non members ' ) ,
2012-03-29 07:32:09 +00:00
' photo_big ' : fields . binary ( ' Full-size photo ' , help = ' Field holding the full-sized PIL-supported and base64 encoded version of the group image. The photo field is used as an interface for this field. ' ) ,
' photo ' : fields . function ( _get_photo , fnct_inv = _set_photo , string = ' Photo ' , type = " binary " ,
2012-03-07 15:34:45 +00:00
store = {
2012-03-29 07:32:09 +00:00
' mail.group ' : ( lambda self , cr , uid , ids , c = { } : ids , [ ' photo_big ' ] , 10 ) ,
} , help = ' Field holding the automatically resized (128x128) PIL-supported and base64 encoded version of the group image. ' ) ,
2012-03-07 17:23:50 +00:00
' joined ' : fields . function ( is_subscriber , type = ' boolean ' , string = ' Joined ' ) ,
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-03-07 17:23:50 +00:00
' members_nbr ' : fields . function ( get_members_nbr , type = ' integer ' , string = ' Members count ' ) ,
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-03-29 07:32:09 +00:00
' photo ' : _get_default_photo ,
2012-01-31 15:54:12 +00:00
}
mail_group ( )