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
|
|
|
|
|
|
|
#
|
2008-08-13 08:42:42 +00:00
|
|
|
# OSV: Objects Services
|
2006-12-07 13:41:40 +00:00
|
|
|
#
|
|
|
|
|
2008-08-13 08:42:42 +00:00
|
|
|
import orm
|
2006-12-07 13:41:40 +00:00
|
|
|
import netsvc
|
2008-08-13 08:42:42 +00:00
|
|
|
import pooler
|
|
|
|
import copy
|
|
|
|
import sys
|
2010-02-04 17:26:10 +00:00
|
|
|
import traceback
|
2010-04-02 07:08:23 +00:00
|
|
|
import logging
|
2010-05-21 15:59:02 +00:00
|
|
|
from psycopg2 import IntegrityError, errorcodes
|
2010-02-04 17:26:10 +00:00
|
|
|
from tools.func import wraps
|
2006-12-07 13:41:40 +00:00
|
|
|
|
2009-11-05 06:39:15 +00:00
|
|
|
|
2008-08-13 08:42:42 +00:00
|
|
|
module_list = []
|
|
|
|
module_class_list = {}
|
|
|
|
class_pool = {}
|
2006-12-07 13:41:40 +00:00
|
|
|
|
2008-08-13 08:42:42 +00:00
|
|
|
class except_osv(Exception):
|
|
|
|
def __init__(self, name, value, exc_type='warning'):
|
2008-07-22 14:24:36 +00:00
|
|
|
self.name = name
|
2008-08-13 08:42:42 +00:00
|
|
|
self.exc_type = exc_type
|
2008-07-22 14:24:36 +00:00
|
|
|
self.value = value
|
2008-10-07 14:51:30 +00:00
|
|
|
self.args = (exc_type, name)
|
|
|
|
|
2006-12-07 13:41:40 +00:00
|
|
|
|
2008-08-13 08:42:42 +00:00
|
|
|
class osv_pool(netsvc.Service):
|
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):
|
|
|
|
try:
|
|
|
|
if not pooler.get_pool(dbname)._ready:
|
|
|
|
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:
|
2010-02-04 17:26:10 +00:00
|
|
|
if inst.name == 'AccessError':
|
2010-04-01 15:08:54 +00:00
|
|
|
self.logger.debug("AccessError", exc_info=True)
|
2009-01-03 00:29:30 +00:00
|
|
|
self.abortResponse(1, inst.name, 'warning', inst.value)
|
|
|
|
except except_osv, inst:
|
|
|
|
self.abortResponse(1, inst.name, inst.exc_type, inst.value)
|
|
|
|
except IntegrityError, inst:
|
|
|
|
for key in self._sql_error.keys():
|
|
|
|
if key in inst[0]:
|
2010-02-04 17:26:10 +00:00
|
|
|
self.abortResponse(1, 'Constraint Error', 'warning', self._sql_error[key])
|
2010-05-21 15:59:02 +00:00
|
|
|
if inst.pgcode == errorcodes.NOT_NULL_VIOLATION:
|
|
|
|
msg = 'Sorry, this record cannot be deleted at the moment because other records still reference it.'
|
|
|
|
self.logger.debug("IntegrityError", exc_info=True)
|
|
|
|
try:
|
|
|
|
context = inst.pgerror.split('"public".')[1]
|
|
|
|
model_name = table = context.split('"')[1]
|
|
|
|
model = table.replace("_",".")
|
|
|
|
model_obj = self.get(model)
|
|
|
|
if model_obj:
|
|
|
|
model_name = model_obj._description or model_obj._name
|
|
|
|
msg += '\n\n[object with reference: %s - %s]' % (model_name, model)
|
|
|
|
except Exception:
|
|
|
|
pass
|
2010-05-19 13:15:36 +00:00
|
|
|
self.abortResponse(1, 'Integrity Error', 'warning', msg)
|
|
|
|
else:
|
|
|
|
self.abortResponse(1, 'Integrity Error', 'warning', inst[0])
|
2009-01-03 00:29:30 +00:00
|
|
|
except Exception, e:
|
2010-04-01 15:08:54 +00:00
|
|
|
self.logger.exception("Uncaught exception")
|
2009-01-03 00:29:30 +00:00
|
|
|
raise
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
2006-12-07 13:41:40 +00:00
|
|
|
|
2008-07-22 14:24:36 +00:00
|
|
|
def __init__(self):
|
2009-01-03 00:29:30 +00:00
|
|
|
self._ready = False
|
2008-08-13 08:42:42 +00:00
|
|
|
self.obj_pool = {}
|
|
|
|
self.module_object_list = {}
|
|
|
|
self.created = []
|
|
|
|
self._sql_error = {}
|
|
|
|
self._store_function = {}
|
2008-08-17 18:28:29 +00:00
|
|
|
self._init = True
|
|
|
|
self._init_parent = {}
|
2010-04-01 15:08:54 +00:00
|
|
|
self.logger = logging.getLogger("web-services")
|
2008-08-13 08:42:42 +00:00
|
|
|
netsvc.Service.__init__(self, 'object_proxy', audience='')
|
|
|
|
self.exportMethod(self.obj_list)
|
|
|
|
self.exportMethod(self.exec_workflow)
|
|
|
|
self.exportMethod(self.execute)
|
|
|
|
|
2008-08-17 18:28:29 +00:00
|
|
|
def init_set(self, cr, mode):
|
2009-01-03 00:29:30 +00:00
|
|
|
different = mode != self._init
|
|
|
|
if different:
|
2008-08-17 18:28:29 +00:00
|
|
|
if mode:
|
2008-10-07 14:51:30 +00:00
|
|
|
self._init_parent = {}
|
2008-08-17 18:28:29 +00:00
|
|
|
if not mode:
|
|
|
|
for o in self._init_parent:
|
|
|
|
self.get(o)._parent_store_compute(cr)
|
|
|
|
self._init = mode
|
2010-03-09 14:28:07 +00:00
|
|
|
|
2009-01-03 00:29:30 +00:00
|
|
|
self._ready = True
|
|
|
|
return different
|
2010-03-09 14:28:07 +00:00
|
|
|
|
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):
|
|
|
|
db, pool = pooler.get_db_and_pool(db)
|
|
|
|
cr = db.cursor()
|
|
|
|
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,))
|
2008-08-13 08:42:42 +00:00
|
|
|
res = pool.execute_cr(cr, uid, obj, method, *args, **kw)
|
2010-03-09 14:28:07 +00:00
|
|
|
if res is None:
|
2010-04-01 15:08:54 +00:00
|
|
|
self.logger.warning('RPC methods cannot return `None` at the moment!')
|
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
|
|
|
|
2008-08-13 08:42:42 +00:00
|
|
|
def obj_list(self):
|
|
|
|
return self.obj_pool.keys()
|
|
|
|
|
|
|
|
# adds a new object instance to the object pool.
|
|
|
|
# if it already existed, the instance is replaced
|
|
|
|
def add(self, name, obj_inst):
|
2008-10-07 14:51:30 +00:00
|
|
|
if name in self.obj_pool:
|
2008-08-13 08:42:42 +00:00
|
|
|
del self.obj_pool[name]
|
|
|
|
self.obj_pool[name] = obj_inst
|
|
|
|
|
|
|
|
module = str(obj_inst.__class__)[6:]
|
|
|
|
module = module[:len(module)-1]
|
|
|
|
module = module.split('.')[0][2:]
|
|
|
|
self.module_object_list.setdefault(module, []).append(obj_inst)
|
|
|
|
|
2009-01-03 00:29:30 +00:00
|
|
|
# Return None if object does not exist
|
2008-08-13 08:42:42 +00:00
|
|
|
def get(self, name):
|
|
|
|
obj = self.obj_pool.get(name, None)
|
|
|
|
return obj
|
|
|
|
|
|
|
|
#TODO: pass a list of modules to load
|
|
|
|
def instanciate(self, module, cr):
|
|
|
|
res = []
|
|
|
|
class_list = module_class_list.get(module, [])
|
|
|
|
for klass in class_list:
|
|
|
|
res.append(klass.createInstance(self, module, cr))
|
2008-07-22 14:24:36 +00:00
|
|
|
return res
|
2008-08-13 08:42:42 +00:00
|
|
|
|
2009-12-17 19:21:39 +00:00
|
|
|
class osv_base(object):
|
|
|
|
def __init__(self, pool, cr):
|
|
|
|
pool.add(self._name, self)
|
|
|
|
self.pool = pool
|
|
|
|
super(osv_base, self).__init__(cr)
|
2008-10-07 14:51:30 +00:00
|
|
|
|
2008-08-13 08:42:42 +00:00
|
|
|
def __new__(cls):
|
|
|
|
module = str(cls)[6:]
|
|
|
|
module = module[:len(module)-1]
|
|
|
|
module = module.split('.')[0][2:]
|
|
|
|
if not hasattr(cls, '_module'):
|
|
|
|
cls._module = module
|
|
|
|
module_class_list.setdefault(cls._module, []).append(cls)
|
|
|
|
class_pool[cls._name] = cls
|
|
|
|
if module not in module_list:
|
|
|
|
module_list.append(cls._module)
|
|
|
|
return None
|
2008-07-22 14:24:36 +00:00
|
|
|
|
2009-12-17 19:21:39 +00:00
|
|
|
class osv_memory(osv_base, orm.orm_memory):
|
2008-07-22 14:24:36 +00:00
|
|
|
#
|
2008-08-13 08:42:42 +00:00
|
|
|
# Goal: try to apply inheritancy at the instanciation level and
|
|
|
|
# put objects in the pool var
|
2008-07-22 14:24:36 +00:00
|
|
|
#
|
2010-01-12 15:39:21 +00:00
|
|
|
def createInstance(cls, pool, module, cr):
|
2010-01-03 14:10:21 +00:00
|
|
|
parent_names = getattr(cls, '_inherit', None)
|
2009-12-17 18:05:28 +00:00
|
|
|
if parent_names:
|
2010-01-12 12:52:16 +00:00
|
|
|
if isinstance(parent_names, (str, unicode)):
|
2010-01-12 15:38:44 +00:00
|
|
|
name = cls._name or parent_names
|
2010-01-12 12:52:16 +00:00
|
|
|
parent_names = [parent_names]
|
|
|
|
else:
|
|
|
|
name = cls._name
|
|
|
|
if not name:
|
|
|
|
raise TypeError('_name is mandatory in case of multiple inheritance')
|
|
|
|
|
2009-12-17 18:05:28 +00:00
|
|
|
for parent_name in ((type(parent_names)==list) and parent_names or [parent_names]):
|
|
|
|
parent_class = pool.get(parent_name).__class__
|
|
|
|
assert pool.get(parent_name), "parent class %s does not exist in module %s !" % (parent_name, module)
|
|
|
|
nattr = {}
|
|
|
|
for s in ('_columns', '_defaults'):
|
|
|
|
new = copy.copy(getattr(pool.get(parent_name), s))
|
|
|
|
if hasattr(new, 'update'):
|
|
|
|
new.update(cls.__dict__.get(s, {}))
|
|
|
|
else:
|
|
|
|
new.extend(cls.__dict__.get(s, []))
|
|
|
|
nattr[s] = new
|
|
|
|
cls = type(name, (cls, parent_class), nattr)
|
2009-09-17 07:27:12 +00:00
|
|
|
|
2008-08-13 08:42:42 +00:00
|
|
|
obj = object.__new__(cls)
|
|
|
|
obj.__init__(pool, cr)
|
|
|
|
return obj
|
|
|
|
createInstance = classmethod(createInstance)
|
|
|
|
|
2009-12-17 19:21:39 +00:00
|
|
|
class osv(osv_base, orm.orm):
|
2008-07-22 14:24:36 +00:00
|
|
|
#
|
2008-08-13 08:42:42 +00:00
|
|
|
# Goal: try to apply inheritancy at the instanciation level and
|
|
|
|
# put objects in the pool var
|
2008-07-22 14:24:36 +00:00
|
|
|
#
|
2008-08-13 08:42:42 +00:00
|
|
|
def createInstance(cls, pool, module, cr):
|
2010-01-12 15:39:21 +00:00
|
|
|
parent_names = getattr(cls, '_inherit', None)
|
2009-12-17 18:07:56 +00:00
|
|
|
if parent_names:
|
2010-01-12 12:52:16 +00:00
|
|
|
if isinstance(parent_names, (str, unicode)):
|
2010-01-12 15:38:44 +00:00
|
|
|
name = cls._name or parent_names
|
2010-01-12 12:52:16 +00:00
|
|
|
parent_names = [parent_names]
|
|
|
|
else:
|
|
|
|
name = cls._name
|
|
|
|
if not name:
|
|
|
|
raise TypeError('_name is mandatory in case of multiple inheritance')
|
|
|
|
|
2009-12-17 18:07:56 +00:00
|
|
|
for parent_name in ((type(parent_names)==list) and parent_names or [parent_names]):
|
|
|
|
parent_class = pool.get(parent_name).__class__
|
|
|
|
assert pool.get(parent_name), "parent class %s does not exist in module %s !" % (parent_name, module)
|
|
|
|
nattr = {}
|
|
|
|
for s in ('_columns', '_defaults', '_inherits', '_constraints', '_sql_constraints'):
|
|
|
|
new = copy.copy(getattr(pool.get(parent_name), s))
|
|
|
|
if hasattr(new, 'update'):
|
|
|
|
new.update(cls.__dict__.get(s, {}))
|
2008-11-23 11:27:27 +00:00
|
|
|
else:
|
2009-12-17 18:07:56 +00:00
|
|
|
if s=='_constraints':
|
|
|
|
for c in cls.__dict__.get(s, []):
|
|
|
|
exist = False
|
|
|
|
for c2 in range(len(new)):
|
2010-05-11 08:36:01 +00:00
|
|
|
#For _constraints, we should check field and methods as well
|
|
|
|
if new[c2][2]==c[2] and new[c2][0]==c[0]:
|
2009-12-17 18:07:56 +00:00
|
|
|
new[c2] = c
|
|
|
|
exist = True
|
|
|
|
break
|
|
|
|
if not exist:
|
|
|
|
new.append(c)
|
|
|
|
else:
|
|
|
|
new.extend(cls.__dict__.get(s, []))
|
2010-01-12 12:34:31 +00:00
|
|
|
nattr[s] = new
|
2009-12-17 18:07:56 +00:00
|
|
|
cls = type(name, (cls, parent_class), nattr)
|
2008-08-13 08:42:42 +00:00
|
|
|
obj = object.__new__(cls)
|
|
|
|
obj.__init__(pool, cr)
|
|
|
|
return obj
|
|
|
|
createInstance = classmethod(createInstance)
|
2008-07-22 14:24:36 +00:00
|
|
|
|
2009-01-07 14:52:34 +00:00
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
|
|
|
|