1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
23 # OSV: Objects Services
31 from psycopg2 import IntegrityError, OperationalError, errorcodes
32 from tools.func import wraps
33 from tools.translate import translate
38 module_class_list = {}
41 PG_CONCURRENCY_ERRORS_TO_RETRY = (errorcodes.LOCK_NOT_AVAILABLE, errorcodes.SERIALIZATION_FAILURE, errorcodes.DEADLOCK_DETECTED)
42 MAX_TRIES_ON_CONCURRENCY_FAILURE = 5
44 class except_osv(Exception):
45 def __init__(self, name, value, exc_type='warning'):
47 self.exc_type = exc_type
49 self.args = (exc_type, name)
52 class object_proxy(netsvc.Service):
54 self.logger = logging.getLogger('web-services')
55 netsvc.Service.__init__(self, 'object_proxy', audience='')
56 self.exportMethod(self.exec_workflow)
57 self.exportMethod(self.execute)
61 def wrapper(self, dbname, *args, **kwargs):
62 """ Wraps around OSV functions and normalises a few exceptions
66 # We try to do the same as the _(), but without the frame
67 # inspection, since we aready are wrapping an osv function
68 # trans_obj = self.get('ir.translation') cannot work yet :(
71 if args and isinstance(args[-1], dict):
73 elif isinstance(kwargs, dict):
74 ctx = kwargs.get('context', {})
77 if args and isinstance(args[0], (long, int)):
80 lang = ctx and ctx.get('lang')
81 if not (lang or hasattr(src, '__call__')):
84 # We open a *new* cursor here, one reason is that failed SQL
85 # queries (as in IntegrityError) will invalidate the current one.
88 if hasattr(src, '__call__'):
89 # callable. We need to find the right parameters to call
90 # the orm._sql_message(self, cr, uid, ids, context) function,
92 # our signature is f(osv_pool, dbname [,uid, obj, method, args])
94 if args and len(args) > 1:
95 obj = self.get(args[1])
96 if len(args) > 3 and isinstance(args[3], (long, int, list)):
100 cr = pooler.get_db_only(dbname).cursor()
101 return src(obj, cr, uid, ids, context=(ctx or {}))
107 return False # so that the original SQL error will
108 # be returned, it is the best we have.
111 cr = pooler.get_db_only(dbname).cursor()
112 res = translate(cr, name=False, source_type=ttype,
113 lang=lang, source=src)
122 return tr(src, 'code')
127 if not pooler.get_pool(dbname)._ready:
128 raise except_osv('Database not ready', 'Currently, this database is not fully loaded and can not be used.')
129 return f(self, dbname, *args, **kwargs)
130 except OperationalError, e:
131 # Automatically retry the typical transaction serialization errors
132 if not e.pgcode in PG_CONCURRENCY_ERRORS_TO_RETRY or tries >= MAX_TRIES_ON_CONCURRENCY_FAILURE:
133 self.logger.warning("%s, maximum number of tries reached" % errorcodes.lookup(e.pgcode))
135 wait_time = random.uniform(0.0, 2 ** tries)
137 self.logger.info("%s, retrying %d/%d in %.04f sec..." % (errorcodes.lookup(e.pgcode), tries, MAX_TRIES_ON_CONCURRENCY_FAILURE, wait_time))
138 time.sleep(wait_time)
139 except orm.except_orm, inst:
140 if inst.name == 'AccessError':
141 self.logger.debug("AccessError", exc_info=True)
142 self.abortResponse(1, inst.name, 'warning', inst.value)
143 except except_osv, inst:
144 self.abortResponse(1, inst.name, inst.exc_type, inst.value)
145 except IntegrityError, inst:
146 osv_pool = pooler.get_pool(dbname)
147 for key in osv_pool._sql_error.keys():
149 self.abortResponse(1, _('Constraint Error'), 'warning',
150 tr(osv_pool._sql_error[key], 'sql_constraint') or inst[0])
151 if inst.pgcode in (errorcodes.NOT_NULL_VIOLATION, errorcodes.FOREIGN_KEY_VIOLATION, errorcodes.RESTRICT_VIOLATION):
152 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')
153 self.logger.debug("IntegrityError", exc_info=True)
155 errortxt = inst.pgerror.replace('«','"').replace('»','"')
156 if '"public".' in errortxt:
157 context = errortxt.split('"public".')[1]
158 model_name = table = context.split('"')[1]
160 last_quote_end = errortxt.rfind('"')
161 last_quote_begin = errortxt.rfind('"', 0, last_quote_end)
162 model_name = table = errortxt[last_quote_begin+1:last_quote_end].strip()
163 model = table.replace("_",".")
164 model_obj = osv_pool.get(model)
166 model_name = model_obj._description or model_obj._name
167 msg += _('\n\n[object with reference: %s - %s]') % (model_name, model)
170 self.abortResponse(1, _('Integrity Error'), 'warning', msg)
172 self.abortResponse(1, _('Integrity Error'), 'warning', inst[0])
174 self.logger.exception("Uncaught exception")
179 def execute_cr(self, cr, uid, obj, method, *args, **kw):
180 object = pooler.get_pool(cr.dbname).get(obj)
182 raise except_osv('Object Error', 'Object %s doesn\'t exist' % str(obj))
183 return getattr(object, method)(cr, uid, *args, **kw)
186 def execute(self, db, uid, obj, method, *args, **kw):
187 cr = pooler.get_db(db).cursor()
190 if method.startswith('_'):
191 raise except_osv('Access Denied', 'Private methods (such as %s) cannot be called remotely.' % (method,))
192 res = self.execute_cr(cr, uid, obj, method, *args, **kw)
194 self.logger.warning('The method %s of the object %s can not return `None` !', method, obj)
203 def exec_workflow_cr(self, cr, uid, obj, method, *args):
204 wf_service = netsvc.LocalService("workflow")
205 return wf_service.trg_validate(uid, obj, args[0], method, cr)
208 def exec_workflow(self, db, uid, obj, method, *args):
209 cr = pooler.get_db(db).cursor()
212 res = self.exec_workflow_cr(cr, uid, obj, method, *args)
223 class osv_pool(object):
227 self.module_object_list = {}
230 self._store_function = {}
232 self._init_parent = {}
233 self.logger = logging.getLogger("pool")
235 def init_set(self, cr, mode):
236 different = mode != self._init
239 self._init_parent = {}
241 for o in self._init_parent:
242 self.get(o)._parent_store_compute(cr)
250 return self.obj_pool.keys()
252 # adds a new object instance to the object pool.
253 # if it already existed, the instance is replaced
254 def add(self, name, obj_inst):
255 if name in self.obj_pool:
256 del self.obj_pool[name]
257 self.obj_pool[name] = obj_inst
259 module = str(obj_inst.__class__)[6:]
260 module = module[:len(module)-1]
261 module = module.split('.')[0][2:]
262 self.module_object_list.setdefault(module, []).append(obj_inst)
264 # Return None if object does not exist
266 obj = self.obj_pool.get(name, None)
269 #TODO: pass a list of modules to load
270 def instanciate(self, module, cr):
272 class_list = module_class_list.get(module, [])
273 for klass in class_list:
274 res.append(klass.createInstance(self, module, cr))
277 class osv_base(object):
278 def __init__(self, pool, cr):
279 pool.add(self._name, self)
281 super(osv_base, self).__init__(cr)
284 module = str(cls)[6:]
285 module = module[:len(module)-1]
286 module = module.split('.')[0][2:]
287 if not hasattr(cls, '_module'):
289 module_class_list.setdefault(cls._module, []).append(cls)
290 class_pool[cls._name] = cls
291 if module not in module_list:
292 module_list.append(cls._module)
295 class osv_memory(osv_base, orm.orm_memory):
297 # Goal: try to apply inheritancy at the instanciation level and
298 # put objects in the pool var
300 def createInstance(cls, pool, module, cr):
301 parent_names = getattr(cls, '_inherit', None)
303 if isinstance(parent_names, (str, unicode)):
304 name = cls._name or parent_names
305 parent_names = [parent_names]
309 raise TypeError('_name is mandatory in case of multiple inheritance')
311 for parent_name in ((type(parent_names)==list) and parent_names or [parent_names]):
312 parent_class = pool.get(parent_name).__class__
313 assert pool.get(parent_name), "parent class %s does not exist in module %s !" % (parent_name, module)
315 for s in ('_columns', '_defaults'):
316 new = copy.copy(getattr(pool.get(parent_name), s))
318 # Don't _inherit custom fields.
322 # Duplicate float fields because they have a .digits
323 # cache (which must be per-pool, not server-wide).
325 if new[c]._type == 'float':
326 new[c] = copy.copy(new[c])
327 if hasattr(new, 'update'):
328 new.update(cls.__dict__.get(s, {}))
330 new.extend(cls.__dict__.get(s, []))
332 cls = type(name, (cls, parent_class), nattr)
334 # Duplicate float fields because they have a .digits
335 # cache (which must be per-pool, not server-wide).
336 for field_name, field in cls._columns.items():
337 if field._type == 'float':
338 cls._columns[field_name] = copy.copy(field)
340 obj = object.__new__(cls)
341 obj.__init__(pool, cr)
343 createInstance = classmethod(createInstance)
345 class osv(osv_base, orm.orm):
347 # Goal: try to apply inheritancy at the instanciation level and
348 # put objects in the pool var
350 def createInstance(cls, pool, module, cr):
351 parent_names = getattr(cls, '_inherit', None)
353 if isinstance(parent_names, (str, unicode)):
354 name = cls._name or parent_names
355 parent_names = [parent_names]
359 raise TypeError('_name is mandatory in case of multiple inheritance')
361 for parent_name in ((type(parent_names)==list) and parent_names or [parent_names]):
362 parent_class = pool.get(parent_name).__class__
363 assert pool.get(parent_name), "parent class %s does not exist in module %s !" % (parent_name, module)
365 for s in ('_columns', '_defaults', '_inherits', '_constraints', '_sql_constraints'):
366 new = copy.copy(getattr(pool.get(parent_name), s))
368 # Don't _inherit custom fields.
372 # Duplicate float fields because they have a .digits
373 # cache (which must be per-pool, not server-wide).
375 if new[c]._type == 'float':
376 new[c] = copy.copy(new[c])
378 if hasattr(new, 'update'):
379 new.update(cls.__dict__.get(s, {}))
381 if s=='_constraints':
382 for c in cls.__dict__.get(s, []):
384 for c2 in range(len(new)):
385 #For _constraints, we should check field and methods as well
386 if new[c2][2]==c[2] and (new[c2][0] == c[0] \
387 or getattr(new[c2][0],'__name__', True) == \
388 getattr(c[0],'__name__', False)):
389 # If new class defines a constraint with
390 # same function name, we let it override
398 new.extend(cls.__dict__.get(s, []))
400 cls = type(name, (cls, parent_class), nattr)
402 # Duplicate float fields because they have a .digits
403 # cache (which must be per-pool, not server-wide).
404 for field_name, field in cls._columns.items():
405 if field._type == 'float':
406 cls._columns[field_name] = copy.copy(field)
407 obj = object.__new__(cls)
408 obj.__init__(pool, cr)
410 createInstance = classmethod(createInstance)
412 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: