2012-02-22 11:59:24 +00:00
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2009-Today OpenERP SA (<http://www.openerp.com>)
#
# 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-03-26 13:38:52 +00:00
from osv import osv , fields
from tools . translate import _
2012-06-27 10:20:05 +00:00
from lxml import etree
2012-02-22 11:59:24 +00:00
class res_users ( osv . osv ) :
2012-04-04 14:15:38 +00:00
""" Update of res.users class
- add a preference about sending emails about notificatoins
- make a new user follow itself
"""
2012-02-22 11:59:24 +00:00
_name = ' res.users '
_inherit = [ ' res.users ' , ' mail.thread ' ]
2012-06-26 06:09:06 +00:00
_inherits = { ' mail.alias ' : ' alias_id ' }
2012-02-22 11:59:24 +00:00
2012-03-26 13:38:52 +00:00
_columns = {
2012-04-04 07:46:52 +00:00
' notification_email_pref ' : fields . selection ( [
2012-04-03 17:34:49 +00:00
( ' all ' , ' All feeds ' ) ,
( ' comments ' , ' Only comments ' ) ,
( ' to_me ' , ' Only when sent directly to me ' ) ,
2012-04-04 07:46:52 +00:00
( ' none ' , ' Never ' )
2012-06-25 13:42:53 +00:00
] , ' Receive Feeds by Email ' , required = True ,
2012-04-04 14:15:38 +00:00
help = " Choose in which case you want to receive an email when you receive new feeds. " ) ,
2012-07-05 13:56:26 +00:00
' alias_id ' : fields . many2one ( ' mail.alias ' , ' Alias ' , ondelete = " restrict " , required = True ,
2012-07-02 11:39:13 +00:00
help = " This Unique Mail Box Alias of the User allows to manage the Seamless email communication between Mail Box and OpenERP, "
" This Alias MailBox manage the Users email communication. " ) ,
2012-03-26 13:38:52 +00:00
}
2012-04-04 12:27:38 +00:00
_defaults = {
2012-04-13 12:16:50 +00:00
' notification_email_pref ' : ' none ' ,
2012-03-26 13:38:52 +00:00
}
2012-04-06 07:40:18 +00:00
def __init__ ( self , pool , cr ) :
""" Override of __init__ to add access rights on notification_email_pref
field . Access rights are disabled by default , but allowed on
fields defined in self . SELF_WRITEABLE_FIELDS .
"""
init_res = super ( res_users , self ) . __init__ ( pool , cr )
# duplicate list to avoid modifying the original reference
self . SELF_WRITEABLE_FIELDS = list ( self . SELF_WRITEABLE_FIELDS )
self . SELF_WRITEABLE_FIELDS . append ( ' notification_email_pref ' )
return init_res
2012-02-22 11:59:24 +00:00
def create ( self , cr , uid , data , context = None ) :
2012-06-26 06:09:06 +00:00
# create default alias same as the login
alias_pool = self . pool . get ( ' mail.alias ' )
2012-07-04 09:38:19 +00:00
alias_id = alias_pool . create_unique_alias ( cr , uid , { ' alias_name ' : data [ ' login ' ] , ' alias_model_id ' : self . _name } , context = context )
2012-07-04 06:20:24 +00:00
data . update ( { ' alias_id ' : alias_id } )
2012-02-22 11:59:24 +00:00
user_id = super ( res_users , self ) . create ( cr , uid , data , context = context )
2012-07-04 06:20:24 +00:00
alias_pool . write ( cr , uid , [ alias_id ] , { " alias_force_thread_id " : user_id } , context )
2012-06-27 06:46:17 +00:00
user = self . browse ( cr , uid , user_id , context = context )
2012-02-22 11:59:24 +00:00
# make user follow itself
self . message_subscribe ( cr , uid , [ user_id ] , [ user_id ] , context = context )
2012-03-29 10:11:13 +00:00
# create a welcome message to broadcast
company_name = user . company_id . name if user . company_id else ' the company '
2012-04-04 14:15:38 +00:00
message = _ ( ' %s has joined %s ! You may leave him/her a message to celebrate a new arrival in the company ! You can help him/her doing its first steps on OpenERP. ' ) % ( user . name , company_name )
2012-04-13 12:53:18 +00:00
# TODO: clean the broadcast feature. As this is not cleany specified, temporarily remove the message broadcasting that is not buggy but not very nice.
#self.message_broadcast(cr, uid, [user.id], 'Welcome notification', message, context=context)
2012-02-22 11:59:24 +00:00
return user_id
2012-06-26 06:09:06 +00:00
def write ( self , cr , uid , ids , vals , context = None ) :
# if login of user have been changed then change alias of user also.
2012-07-04 11:06:17 +00:00
if vals . get ( ' login ' ) :
2012-06-27 05:59:28 +00:00
for user in self . browse ( cr , uid , ids , context = context ) :
2012-06-29 08:46:21 +00:00
self . pool . get ( ' mail.alias ' ) . write ( cr , uid , [ user . alias_id . id ] , { ' alias_name ' : vals [ ' login ' ] } , context = context )
2012-06-27 05:59:28 +00:00
return super ( res_users , self ) . write ( cr , uid , ids , vals , context = context )
2012-02-22 11:59:24 +00:00
2012-06-26 06:09:06 +00:00
2012-03-26 14:35:38 +00:00
def message_load_ids ( self , cr , uid , ids , limit = 100 , offset = 0 , domain = [ ] , ascent = False , root_ids = [ False ] , context = None ) :
2012-04-05 21:04:12 +00:00
""" Override of message_load_ids
User discussion page :
- messages posted on res . users , res_id = user . id
- messages directly sent to user with @user_login
"""
2012-03-26 14:35:38 +00:00
if context is None :
context = { }
msg_obj = self . pool . get ( ' mail.message ' )
msg_ids = [ ]
2012-04-05 21:04:12 +00:00
for user in self . browse ( cr , uid , ids , context = context ) :
msg_ids + = msg_obj . search ( cr , uid , [ ' | ' , ' | ' , ( ' body_text ' , ' like ' , ' @ %s ' % ( user . login ) ) , ( ' body_html ' , ' like ' , ' @ %s ' % ( user . login ) ) , ' & ' , ( ' res_id ' , ' = ' , user . id ) , ( ' model ' , ' = ' , self . _name ) ] + domain ,
2012-03-26 14:35:38 +00:00
limit = limit , offset = offset , context = context )
2012-04-04 14:15:38 +00:00
if ( ascent ) : msg_ids = self . _message_add_ancestor_ids ( cr , uid , ids , msg_ids , root_ids , context = context )
2012-03-26 14:35:38 +00:00
return msg_ids