2009-10-20 10:52:23 +00:00
# -*- coding: utf-8 -*-
2006-12-07 13:41:40 +00:00
##############################################################################
2010-03-09 14:28:07 +00:00
#
2009-01-07 14:52:34 +00:00
# OpenERP, Open Source Management Solution
2009-11-26 14:12:40 +00:00
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
2008-06-16 07:24:04 +00:00
#
2008-11-03 18:27:16 +00:00
# This program is free software: you can redistribute it and/or modify
2009-11-26 14:12:40 +00:00
# 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.
2006-12-07 13:41:40 +00:00
#
2008-11-03 18:27:16 +00:00
# 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
2009-11-26 14:12:40 +00:00
# GNU Affero General Public License for more details.
2006-12-07 13:41:40 +00:00
#
2009-11-26 14:12:40 +00:00
# You should have received a copy of the GNU Affero General Public License
2010-03-09 14:28:07 +00:00
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2006-12-07 13:41:40 +00:00
#
2008-11-03 18:27:16 +00:00
##############################################################################
2006-12-07 13:41:40 +00:00
2011-06-23 09:03:57 +00:00
#.apidoc title: Objects Services (OSV)
2006-12-07 13:41:40 +00:00
2011-09-22 17:20:27 +00:00
import logging
from psycopg2 import IntegrityError , errorcodes
2008-08-13 08:42:42 +00:00
import orm
2011-08-12 11:33:55 +00:00
import openerp
2011-02-07 12:57:23 +00:00
import openerp . netsvc as netsvc
import openerp . pooler as pooler
2011-04-20 14:14:07 +00:00
import openerp . sql_db as sql_db
2011-02-07 12:57:23 +00:00
from openerp . tools . func import wraps
from openerp . tools . translate import translate
2011-09-23 12:45:25 +00:00
from openerp . osv . orm import MetaModel , Model , TransientModel , AbstractModel
2011-09-26 12:53:58 +00:00
import openerp . exceptions
2011-06-14 14:22:26 +00:00
2011-09-30 08:50:12 +00:00
# Deprecated.
class except_osv ( Exception ) :
def __init__ ( self , name , value ) :
self . name = name
self . value = value
self . args = ( name , value )
2008-10-07 14:51:30 +00:00
2011-07-29 08:38:24 +00:00
service = None
2006-12-07 13:41:40 +00:00
2011-07-29 08:38:24 +00:00
class object_proxy ( ) :
2010-12-13 10:51:04 +00:00
def __init__ ( self ) :
self . logger = logging . getLogger ( ' web-services ' )
2011-07-29 08:38:24 +00:00
global service
service = self
2010-03-09 14:28:07 +00:00
2009-01-03 00:29:30 +00:00
def check ( f ) :
@wraps ( f )
def wrapper ( self , dbname , * args , * * kwargs ) :
2010-11-18 18:46:38 +00:00
""" Wraps around OSV functions and normalises a few exceptions
"""
def tr ( src , ttype ) :
# We try to do the same as the _(), but without the frame
# inspection, since we aready are wrapping an osv function
# trans_obj = self.get('ir.translation') cannot work yet :(
ctx = { }
if not kwargs :
if args and isinstance ( args [ - 1 ] , dict ) :
ctx = args [ - 1 ]
elif isinstance ( kwargs , dict ) :
ctx = kwargs . get ( ' context ' , { } )
uid = 1
if args and isinstance ( args [ 0 ] , ( long , int ) ) :
uid = args [ 0 ]
2010-11-24 16:40:41 +00:00
lang = ctx and ctx . get ( ' lang ' )
2010-11-18 18:46:43 +00:00
if not ( lang or hasattr ( src , ' __call__ ' ) ) :
2010-11-18 18:46:38 +00:00
return src
# We open a *new* cursor here, one reason is that failed SQL
# queries (as in IntegrityError) will invalidate the current one.
cr = False
2011-06-01 09:49:08 +00:00
2010-11-18 18:46:43 +00:00
if hasattr ( src , ' __call__ ' ) :
# callable. We need to find the right parameters to call
# the orm._sql_message(self, cr, uid, ids, context) function,
# or we skip..
# our signature is f(osv_pool, dbname [,uid, obj, method, args])
try :
if args and len ( args ) > 1 :
obj = self . get ( args [ 1 ] )
if len ( args ) > 3 and isinstance ( args [ 3 ] , ( long , int , list ) ) :
ids = args [ 3 ]
else :
ids = [ ]
2011-06-01 09:49:08 +00:00
cr = sql_db . db_connect ( dbname ) . cursor ( )
2010-11-18 18:46:43 +00:00
return src ( obj , cr , uid , ids , context = ( ctx or { } ) )
2010-11-18 18:48:40 +00:00
except Exception :
2010-11-18 18:46:43 +00:00
pass
finally :
if cr : cr . close ( )
2011-06-01 09:49:08 +00:00
2010-11-18 18:46:43 +00:00
return False # so that the original SQL error will
# be returned, it is the best we have.
2010-11-18 18:46:38 +00:00
try :
2011-06-01 09:49:08 +00:00
cr = sql_db . db_connect ( dbname ) . cursor ( )
2010-11-18 18:46:43 +00:00
res = translate ( cr , name = False , source_type = ttype ,
2010-11-18 18:46:38 +00:00
lang = lang , source = src )
2010-11-18 18:46:43 +00:00
if res :
return res
else :
return src
2010-11-18 18:46:38 +00:00
finally :
if cr : cr . close ( )
def _ ( src ) :
return tr ( src , ' code ' )
2009-01-03 00:29:30 +00:00
try :
2011-05-26 21:05:32 +00:00
if pooler . get_pool ( dbname ) . _init :
2009-01-03 00:29:30 +00:00
raise except_osv ( ' Database not ready ' , ' Currently, this database is not fully loaded and can not be used. ' )
return f ( self , dbname , * args , * * kwargs )
except orm . except_orm , inst :
2011-09-30 08:50:12 +00:00
raise except_osv ( inst . name , inst . value )
2011-09-29 07:31:24 +00:00
except except_osv :
raise
2009-01-03 00:29:30 +00:00
except IntegrityError , inst :
2010-12-14 13:18:12 +00:00
osv_pool = pooler . get_pool ( dbname )
for key in osv_pool . _sql_error . keys ( ) :
2009-01-03 00:29:30 +00:00
if key in inst [ 0 ] :
2011-07-29 08:38:24 +00:00
netsvc . abort_response ( 1 , _ ( ' Constraint Error ' ) , ' warning ' ,
2010-12-14 13:18:12 +00:00
tr ( osv_pool . _sql_error [ key ] , ' sql_constraint ' ) or inst [ 0 ] )
2010-09-16 11:39:06 +00:00
if inst . pgcode in ( errorcodes . NOT_NULL_VIOLATION , errorcodes . FOREIGN_KEY_VIOLATION , errorcodes . RESTRICT_VIOLATION ) :
2010-09-09 11:02:11 +00:00
msg = _ ( ' The operation cannot be completed, probably due to the following: \n - deletion: you may be trying to delete a record while other records still reference it \n - creation/update: a mandatory field is not correctly set ' )
2010-05-21 15:59:02 +00:00
self . logger . debug ( " IntegrityError " , exc_info = True )
try :
2010-09-16 11:39:06 +00:00
errortxt = inst . pgerror . replace ( ' « ' , ' " ' ) . replace ( ' » ' , ' " ' )
if ' " public " . ' in errortxt :
context = errortxt . split ( ' " public " . ' ) [ 1 ]
model_name = table = context . split ( ' " ' ) [ 1 ]
else :
last_quote_end = errortxt . rfind ( ' " ' )
last_quote_begin = errortxt . rfind ( ' " ' , 0 , last_quote_end )
model_name = table = errortxt [ last_quote_begin + 1 : last_quote_end ] . strip ( )
2010-05-21 15:59:02 +00:00
model = table . replace ( " _ " , " . " )
2010-12-14 13:18:12 +00:00
model_obj = osv_pool . get ( model )
2010-05-21 15:59:02 +00:00
if model_obj :
model_name = model_obj . _description or model_obj . _name
2010-09-09 11:02:11 +00:00
msg + = _ ( ' \n \n [object with reference: %s - %s ] ' ) % ( model_name , model )
2010-05-21 15:59:02 +00:00
except Exception :
pass
2011-07-29 08:38:24 +00:00
netsvc . abort_response ( 1 , _ ( ' Integrity Error ' ) , ' warning ' , msg )
2010-05-19 13:15:36 +00:00
else :
2011-07-29 08:38:24 +00:00
netsvc . abort_response ( 1 , _ ( ' Integrity Error ' ) , ' warning ' , inst [ 0 ] )
2010-12-13 10:51:04 +00:00
except Exception :
2010-04-01 15:08:54 +00:00
self . logger . exception ( " Uncaught exception " )
2009-01-03 00:29:30 +00:00
raise
return wrapper
2008-08-13 08:42:42 +00:00
def execute_cr ( self , cr , uid , obj , method , * args , * * kw ) :
2009-01-03 00:29:30 +00:00
object = pooler . get_pool ( cr . dbname ) . get ( obj )
if not object :
raise except_osv ( ' Object Error ' , ' Object %s doesn \' t exist ' % str ( obj ) )
return getattr ( object , method ) ( cr , uid , * args , * * kw )
2010-03-09 14:28:07 +00:00
2009-01-03 00:29:30 +00:00
@check
2008-08-13 08:42:42 +00:00
def execute ( self , db , uid , obj , method , * args , * * kw ) :
2010-12-13 10:51:04 +00:00
cr = pooler . get_db ( db ) . cursor ( )
2008-08-13 08:42:42 +00:00
try :
2008-07-22 14:24:36 +00:00
try :
2010-03-09 14:28:07 +00:00
if method . startswith ( ' _ ' ) :
2010-04-01 15:08:54 +00:00
raise except_osv ( ' Access Denied ' , ' Private methods (such as %s ) cannot be called remotely. ' % ( method , ) )
2010-12-13 10:51:04 +00:00
res = self . execute_cr ( cr , uid , obj , method , * args , * * kw )
2010-03-09 14:28:07 +00:00
if res is None :
2010-06-12 16:49:35 +00:00
self . logger . warning ( ' The method %s of the object %s can not return `None` ! ' , method , obj )
2008-08-13 08:42:42 +00:00
cr . commit ( )
except Exception :
2008-07-22 14:24:36 +00:00
cr . rollback ( )
2008-08-13 08:42:42 +00:00
raise
finally :
cr . close ( )
return res
2008-08-12 14:44:56 +00:00
2008-08-13 08:42:42 +00:00
def exec_workflow_cr ( self , cr , uid , obj , method , * args ) :
wf_service = netsvc . LocalService ( " workflow " )
return wf_service . trg_validate ( uid , obj , args [ 0 ] , method , cr )
2008-08-12 14:44:56 +00:00
2009-01-03 00:29:30 +00:00
@check
2008-08-13 08:42:42 +00:00
def exec_workflow ( self , db , uid , obj , method , * args ) :
cr = pooler . get_db ( db ) . cursor ( )
try :
try :
res = self . exec_workflow_cr ( cr , uid , obj , method , * args )
cr . commit ( )
2009-01-03 00:29:30 +00:00
except Exception :
2008-08-13 08:42:42 +00:00
cr . rollback ( )
2009-01-03 00:29:30 +00:00
raise
2008-08-13 08:42:42 +00:00
finally :
cr . close ( )
return res
2008-08-12 14:44:56 +00:00
2011-09-23 12:45:25 +00:00
# deprecated - for backward compatibility.
2011-08-16 10:46:50 +00:00
osv = Model
osv_memory = TransientModel
2011-09-23 12:45:25 +00:00
osv_abstract = AbstractModel # ;-)
2010-01-12 12:52:16 +00:00
2008-07-22 14:24:36 +00:00
2011-05-07 11:21:29 +00:00
def start_object_proxy ( ) :
object_proxy ( )
2009-01-07 14:52:34 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: