2006-12-07 13:41:40 +00:00
##############################################################################
#
2008-06-16 11:00:21 +00:00
# Copyright (c) 2004-2008 TINY SPRL. (http://tiny.be) All Rights Reserved.
#
# $Id$
2006-12-07 13:41:40 +00:00
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
from osv import fields , osv
import tools
2008-06-16 18:27:17 +00:00
import pytz
2006-12-07 13:41:40 +00:00
class groups ( osv . osv ) :
2008-07-23 10:25:20 +00:00
_name = " res.groups "
_columns = {
' name ' : fields . char ( ' Group Name ' , size = 64 , required = True ) ,
' model_access ' : fields . one2many ( ' ir.model.access ' , ' group_id ' , ' Access Controls ' ) ,
' rule_groups ' : fields . many2many ( ' ir.rule.group ' , ' group_rule_group_rel ' ,
' group_id ' , ' rule_group_id ' , ' Rules ' , domain = " [( ' global ' , ' <> ' , True)] " ) ,
' menu_access ' : fields . many2many ( ' ir.ui.menu ' , ' ir_ui_menu_group_rel ' , ' gid ' , ' menu_id ' , ' Access Menu ' ) ,
' comment ' : fields . text ( ' Comment ' , size = 250 ) ,
}
_sql_constraints = [
( ' name_uniq ' , ' unique (name) ' , ' The name of the group must be unique ! ' )
]
2007-08-09 06:06:27 +00:00
2008-07-23 10:25:20 +00:00
def write ( self , cr , uid , ids , vals , context = None ) :
if ' name ' in vals :
if vals [ ' name ' ] . startswith ( ' - ' ) :
raise osv . except_osv ( _ ( ' Error ' ) ,
_ ( ' The name of the group can not start with " - " ' ) )
res = super ( groups , self ) . write ( cr , uid , ids , vals , context = context )
# Restart the cache on the company_get method
self . pool . get ( ' ir.rule ' ) . domain_get ( )
return res
2007-08-09 06:06:27 +00:00
2008-07-23 10:25:20 +00:00
def create ( self , cr , uid , vals , context = None ) :
if ' name ' in vals :
if vals [ ' name ' ] . startswith ( ' - ' ) :
raise osv . except_osv ( _ ( ' Error ' ) ,
_ ( ' The name of the group can not start with " - " ' ) )
return super ( groups , self ) . create ( cr , uid , vals , context = context )
2007-08-09 06:06:27 +00:00
2006-12-07 13:41:40 +00:00
groups ( )
class roles ( osv . osv ) :
2008-07-23 10:25:20 +00:00
_name = " res.roles "
_columns = {
' name ' : fields . char ( ' Role Name ' , size = 64 , required = True ) ,
' parent_id ' : fields . many2one ( ' res.roles ' , ' Parent ' , select = True ) ,
2008-07-28 15:43:35 +00:00
' child_id ' : fields . one2many ( ' res.roles ' , ' parent_id ' , ' Childs ' ) ,
' users ' : fields . many2many ( ' res.users ' , ' res_roles_users_rel ' , ' rid ' , ' uid ' , ' Users ' ) ,
2008-07-23 10:25:20 +00:00
}
_defaults = {
}
def check ( self , cr , uid , ids , role_id ) :
if role_id in ids :
return True
cr . execute ( ' select parent_id from res_roles where id= %d ' , ( role_id , ) )
roles = cr . fetchone ( ) [ 0 ]
if roles :
return self . check ( cr , uid , ids , roles )
return False
2006-12-07 13:41:40 +00:00
roles ( )
2008-06-16 18:27:17 +00:00
def _lang_get ( self , cr , uid , context = { } ) :
2008-07-23 10:25:20 +00:00
obj = self . pool . get ( ' res.lang ' )
ids = obj . search ( cr , uid , [ ] )
res = obj . read ( cr , uid , ids , [ ' code ' , ' name ' ] , context )
res = [ ( r [ ' code ' ] , r [ ' name ' ] ) for r in res ]
return res
2008-06-18 06:17:51 +00:00
def _tz_get ( self , cr , uid , context = { } ) :
2008-07-23 10:25:20 +00:00
return [ ( x , x ) for x in pytz . all_timezones ]
2008-06-16 18:27:17 +00:00
2006-12-07 13:41:40 +00:00
class users ( osv . osv ) :
2008-07-23 10:25:20 +00:00
_name = " res.users "
_log_access = False
_columns = {
' name ' : fields . char ( ' Name ' , size = 64 , required = True , select = True ) ,
' login ' : fields . char ( ' Login ' , size = 64 , required = True ) ,
' password ' : fields . char ( ' Password ' , size = 64 , invisible = True ) ,
' signature ' : fields . text ( ' Signature ' , size = 64 ) ,
' address_id ' : fields . many2one ( ' res.partner.address ' , ' Address ' ) ,
' active ' : fields . boolean ( ' Active ' ) ,
' action_id ' : fields . many2one ( ' ir.actions.actions ' , ' Home Action ' ) ,
' menu_id ' : fields . many2one ( ' ir.actions.actions ' , ' Menu Action ' ) ,
' groups_id ' : fields . many2many ( ' res.groups ' , ' res_groups_users_rel ' , ' uid ' , ' gid ' , ' Groups ' ) ,
' roles_id ' : fields . many2many ( ' res.roles ' , ' res_roles_users_rel ' , ' uid ' , ' rid ' , ' Roles ' ) ,
2008-07-28 15:43:35 +00:00
' rules_id ' : fields . many2many ( ' ir.rule.group ' , ' user_rule_group_rel ' , ' rule_group_id ' , ' user_id ' , ' Rules ' ) ,
2008-07-23 10:25:20 +00:00
' company_id ' : fields . many2one ( ' res.company ' , ' Company ' ) ,
' context_lang ' : fields . selection ( _lang_get , ' Language ' , required = True ) ,
' context_tz ' : fields . selection ( _tz_get , ' Timezone ' , size = 64 )
}
_sql_constraints = [
( ' login_key ' , ' UNIQUE (login) ' , ' You can not have two users with the same login ! ' )
]
def _get_action ( self , cr , uid , context = { } ) :
ids = self . pool . get ( ' ir.ui.menu ' ) . search ( cr , uid , [ ( ' usage ' , ' = ' , ' menu ' ) ] )
return ids and ids [ 0 ] or False
2008-06-30 15:33:15 +00:00
2008-07-23 10:25:20 +00:00
def _get_company ( self , cr , uid , context = { } ) :
return self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid , context ) . company_id . id
2008-06-30 15:33:15 +00:00
2008-07-23 10:25:20 +00:00
def _get_menu ( self , cr , uid , context = { } ) :
ids = self . pool . get ( ' ir.actions.act_window ' ) . search ( cr , uid , [ ( ' usage ' , ' = ' , ' menu ' ) ] )
return ids and ids [ 0 ] or False
2008-06-30 15:33:15 +00:00
2008-07-23 10:25:20 +00:00
_defaults = {
' password ' : lambda obj , cr , uid , context = { } : ' ' ,
' context_lang ' : lambda * args : ' en_US ' ,
' active ' : lambda obj , cr , uid , context = { } : True ,
' menu_id ' : _get_menu ,
' action_id ' : _get_menu ,
' company_id ' : _get_company ,
}
def company_get ( self , cr , uid , uid2 ) :
company_id = self . pool . get ( ' res.users ' ) . browse ( cr , uid , uid ) . company_id . id
return company_id
company_get = tools . cache ( ) ( company_get )
2006-12-07 13:41:40 +00:00
2008-07-23 10:25:20 +00:00
def write ( self , cr , uid , ids , values , * args , * * argv ) :
if ( ids == [ uid ] ) :
ok = True
for k in values . keys ( ) :
2008-07-30 12:42:48 +00:00
if k not in ( ' password ' , ' signature ' , ' action_id ' , ' context_lang ' , ' context_tz ' ) :
2008-07-23 10:25:20 +00:00
ok = False
if ok :
uid = 1
res = super ( users , self ) . write ( cr , uid , ids , values , * args , * * argv )
self . company_get ( )
# Restart the cache on the company_get method
self . pool . get ( ' ir.rule ' ) . domain_get ( )
return res
2006-12-07 13:41:40 +00:00
2008-07-30 12:42:48 +00:00
def read ( self , cr , uid , ids , fields = None , context = None , load = ' _classic_read ' ) :
result = super ( users , self ) . read ( cr , uid , ids , fields , context , load )
canwrite = self . pool . get ( ' ir.model.access ' ) . check ( cr , uid , ' res.users ' , ' write ' , raise_exception = False )
if not canwrite :
for r in result :
if ' password ' in r :
r [ ' password ' ] = ' ******** '
return result
2008-07-23 10:25:20 +00:00
def unlink ( self , cr , uid , ids ) :
if 1 in ids :
2008-07-30 12:42:48 +00:00
raise osv . except_osv ( _ ( ' Can not remove root user! ' ) , _ ( ' You can not remove the root user as it is used internally for resources created by Open ERP (updates, module installation, ...) ' ) )
2008-07-23 10:25:20 +00:00
return super ( users , self ) . unlink ( cr , uid , ids )
2008-06-20 11:49:23 +00:00
2008-07-23 10:25:20 +00:00
def name_search ( self , cr , user , name = ' ' , args = None , operator = ' ilike ' , context = None , limit = 80 ) :
if not args :
args = [ ]
if not context :
context = { }
ids = [ ]
if name :
ids = self . search ( cr , user , [ ( ' login ' , ' = ' , name ) ] + args , limit = limit )
if not ids :
ids = self . search ( cr , user , [ ( ' name ' , operator , name ) ] + args , limit = limit )
return self . name_get ( cr , user , ids )
2008-06-20 11:49:23 +00:00
2008-07-23 10:25:20 +00:00
def copy ( self , cr , uid , id , default = None , context = { } ) :
login = self . read ( cr , uid , [ id ] , [ ' login ' ] ) [ 0 ] [ ' login ' ]
default . update ( { ' login ' : login + ' (copy) ' } )
return super ( users , self ) . copy ( cr , uid , id , default , context )
2008-06-16 18:27:17 +00:00
2008-07-23 10:25:20 +00:00
def context_get ( self , cr , uid , context = { } ) :
user = self . browse ( cr , uid , uid , context )
result = { }
for k in self . _columns . keys ( ) :
if k . startswith ( ' context_ ' ) :
result [ k [ 8 : ] ] = getattr ( user , k )
return result
2008-06-16 18:27:17 +00:00
2008-07-23 10:25:20 +00:00
def action_get ( self , cr , uid , context = { } ) :
dataobj = self . pool . get ( ' ir.model.data ' )
data_id = dataobj . _get_id ( cr , 1 , ' base ' , ' action_res_users_my ' )
return dataobj . browse ( cr , uid , data_id , context ) . res_id
2008-06-20 11:49:23 +00:00
2008-07-23 10:25:20 +00:00
def action_next ( self , cr , uid , ids , context = None ) :
return {
' view_type ' : ' form ' ,
" view_mode " : ' form ' ,
' res_model ' : ' ir.module.module.configuration.wizard ' ,
' type ' : ' ir.actions.act_window ' ,
' target ' : ' new ' ,
}
2008-07-02 08:42:47 +00:00
2008-07-23 10:25:20 +00:00
def action_continue ( self , cr , uid , ids , context = { } ) :
return {
' view_type ' : ' form ' ,
" view_mode " : ' form ' ,
' res_model ' : ' ir.module.module.configuration.wizard ' ,
' type ' : ' ir.actions.act_window ' ,
' target ' : ' new ' ,
}
def action_new ( self , cr , uid , ids , context = { } ) :
return {
' view_type ' : ' form ' ,
" view_mode " : ' form ' ,
' res_model ' : ' res.users ' ,
' view_id ' : self . pool . get ( ' ir.ui.view ' ) . search ( cr , uid , [ ( ' name ' , ' = ' , ' res.users.confirm.form ' ) ] ) ,
' type ' : ' ir.actions.act_window ' ,
' target ' : ' new ' ,
}
def action_cancel ( self , cr , uid , ids , conect = { } ) :
return {
' view_type ' : ' form ' ,
" view_mode " : ' form ' ,
' res_model ' : ' ir.module.module.configuration.wizard ' ,
' type ' : ' ir.actions.act_window ' ,
' target ' : ' new ' ,
}
2006-12-07 13:41:40 +00:00
users ( )
2008-07-28 15:43:35 +00:00
class groups2 ( osv . osv ) : ##FIXME: Is there a reason to inherit this object ?
2008-07-23 10:25:20 +00:00
_inherit = ' res.groups '
_columns = {
' users ' : fields . many2many ( ' res.users ' , ' res_groups_users_rel ' , ' gid ' , ' uid ' , ' Users ' ) ,
}
2007-07-19 13:47:25 +00:00
groups2 ( )
2008-06-21 09:54:30 +00:00
class res_config_view ( osv . osv_memory ) :
2008-07-23 10:25:20 +00:00
_name = ' res.config.view '
_columns = {
' name ' : fields . char ( ' Name ' , size = 64 ) ,
' view ' : fields . selection ( [ ( ' simple ' , ' Simple ' ) , ( ' extended ' , ' Extended ' ) ] , ' View ' , required = True ) ,
2008-06-21 09:54:30 +00:00
}
2008-07-23 10:25:20 +00:00
_defaults = {
' view ' : lambda * args : ' simple ' ,
}
2008-07-07 09:41:01 +00:00
2008-07-23 10:25:20 +00:00
def action_cancel ( self , cr , uid , ids , conect = None ) :
print ' Cancel action '
return {
' view_type ' : ' form ' ,
" view_mode " : ' form ' ,
' res_model ' : ' ir.module.module.configuration.wizard ' ,
' type ' : ' ir.actions.act_window ' ,
' target ' : ' new ' ,
2008-07-07 09:41:01 +00:00
}
2008-07-23 10:25:20 +00:00
def action_set ( self , cr , uid , ids , context = None ) :
res = self . read ( cr , uid , ids ) [ 0 ]
users_obj = self . pool . get ( ' res.users ' )
group_obj = self . pool . get ( ' res.groups ' )
if ' view ' in res and res [ ' view ' ] and res [ ' view ' ] == ' extended ' :
group_ids = group_obj . search ( cr , uid , [ ( ' name ' , ' = ' , ' Extended View ' ) ] )
if group_ids and len ( group_ids ) :
users_obj . write ( cr , uid , [ 3 ] , {
' groups_id ' : [ ( 4 , group_ids [ 0 ] ) ]
} , context = context )
return {
' view_type ' : ' form ' ,
" view_mode " : ' form ' ,
' res_model ' : ' ir.module.module.configuration.wizard ' ,
' type ' : ' ir.actions.act_window ' ,
' target ' : ' new ' ,
}
2008-06-21 09:54:30 +00:00
res_config_view ( )