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
33 from psycopg2 import IntegrityError, errorcodes
34 from tools.func import wraps
38 module_class_list = {}
41 class except_osv(Exception):
42 def __init__(self, name, value, exc_type='warning'):
44 self.exc_type = exc_type
46 self.args = (exc_type, name)
49 class osv_pool(netsvc.Service):
53 def wrapper(self, dbname, *args, **kwargs):
55 if not pooler.get_pool(dbname)._ready:
56 raise except_osv('Database not ready', 'Currently, this database is not fully loaded and can not be used.')
57 return f(self, dbname, *args, **kwargs)
58 except orm.except_orm, inst:
59 if inst.name == 'AccessError':
60 self.logger.debug("AccessError", exc_info=True)
61 self.abortResponse(1, inst.name, 'warning', inst.value)
62 except except_osv, inst:
63 self.abortResponse(1, inst.name, inst.exc_type, inst.value)
64 except IntegrityError, inst:
65 for key in self._sql_error.keys():
67 self.abortResponse(1, 'Constraint Error', 'warning', self._sql_error[key])
68 if inst.pgcode == errorcodes.NOT_NULL_VIOLATION:
69 msg = 'Sorry, this record cannot be deleted at the moment because other records still reference it.'
70 self.logger.debug("IntegrityError", exc_info=True)
72 context = inst.pgerror.split('"public".')[1]
73 model_name = table = context.split('"')[1]
74 model = table.replace("_",".")
75 model_obj = self.get(model)
77 model_name = model_obj._description or model_obj._name
78 msg += '\n\n[object with reference: %s - %s]' % (model_name, model)
81 self.abortResponse(1, 'Integrity Error', 'warning', msg)
83 self.abortResponse(1, 'Integrity Error', 'warning', inst[0])
85 self.logger.exception("Uncaught exception")
94 self.module_object_list = {}
97 self._store_function = {}
99 self._init_parent = {}
100 self.logger = logging.getLogger("web-services")
101 netsvc.Service.__init__(self, 'object_proxy', audience='')
102 self.exportMethod(self.obj_list)
103 self.exportMethod(self.exec_workflow)
104 self.exportMethod(self.execute)
106 def init_set(self, cr, mode):
107 different = mode != self._init
110 self._init_parent = {}
112 for o in self._init_parent:
113 self.get(o)._parent_store_compute(cr)
119 def execute_cr(self, cr, uid, obj, method, *args, **kw):
120 object = pooler.get_pool(cr.dbname).get(obj)
122 raise except_osv('Object Error', 'Object %s doesn\'t exist' % str(obj))
123 return getattr(object, method)(cr, uid, *args, **kw)
126 def execute(self, db, uid, obj, method, *args, **kw):
127 db, pool = pooler.get_db_and_pool(db)
131 if method.startswith('_'):
132 raise except_osv('Access Denied', 'Private methods (such as %s) cannot be called remotely.' % (method,))
133 res = pool.execute_cr(cr, uid, obj, method, *args, **kw)
135 self.logger.warning('The method %s of the object %s can not return `None` !', method, obj)
144 def exec_workflow_cr(self, cr, uid, obj, method, *args):
145 wf_service = netsvc.LocalService("workflow")
146 return wf_service.trg_validate(uid, obj, args[0], method, cr)
149 def exec_workflow(self, db, uid, obj, method, *args):
150 cr = pooler.get_db(db).cursor()
153 res = self.exec_workflow_cr(cr, uid, obj, method, *args)
163 return self.obj_pool.keys()
165 # adds a new object instance to the object pool.
166 # if it already existed, the instance is replaced
167 def add(self, name, obj_inst):
168 if name in self.obj_pool:
169 del self.obj_pool[name]
170 self.obj_pool[name] = obj_inst
172 module = str(obj_inst.__class__)[6:]
173 module = module[:len(module)-1]
174 module = module.split('.')[0][2:]
175 self.module_object_list.setdefault(module, []).append(obj_inst)
177 # Return None if object does not exist
179 obj = self.obj_pool.get(name, None)
182 #TODO: pass a list of modules to load
183 def instanciate(self, module, cr):
185 class_list = module_class_list.get(module, [])
186 for klass in class_list:
187 res.append(klass.createInstance(self, module, cr))
190 class osv_base(object):
191 def __init__(self, pool, cr):
192 pool.add(self._name, self)
194 super(osv_base, self).__init__(cr)
197 module = str(cls)[6:]
198 module = module[:len(module)-1]
199 module = module.split('.')[0][2:]
200 if not hasattr(cls, '_module'):
202 module_class_list.setdefault(cls._module, []).append(cls)
203 class_pool[cls._name] = cls
204 if module not in module_list:
205 module_list.append(cls._module)
208 class osv_memory(osv_base, orm.orm_memory):
210 # Goal: try to apply inheritancy at the instanciation level and
211 # put objects in the pool var
213 def createInstance(cls, pool, module, cr):
214 parent_names = getattr(cls, '_inherit', None)
216 if isinstance(parent_names, (str, unicode)):
217 name = cls._name or parent_names
218 parent_names = [parent_names]
222 raise TypeError('_name is mandatory in case of multiple inheritance')
224 for parent_name in ((type(parent_names)==list) and parent_names or [parent_names]):
225 parent_class = pool.get(parent_name).__class__
226 assert pool.get(parent_name), "parent class %s does not exist in module %s !" % (parent_name, module)
228 for s in ('_columns', '_defaults'):
229 new = copy.copy(getattr(pool.get(parent_name), s))
230 if hasattr(new, 'update'):
231 new.update(cls.__dict__.get(s, {}))
233 new.extend(cls.__dict__.get(s, []))
235 cls = type(name, (cls, parent_class), nattr)
237 obj = object.__new__(cls)
238 obj.__init__(pool, cr)
240 createInstance = classmethod(createInstance)
242 class osv(osv_base, orm.orm):
244 # Goal: try to apply inheritancy at the instanciation level and
245 # put objects in the pool var
247 def createInstance(cls, pool, module, cr):
248 parent_names = getattr(cls, '_inherit', None)
250 if isinstance(parent_names, (str, unicode)):
251 name = cls._name or parent_names
252 parent_names = [parent_names]
256 raise TypeError('_name is mandatory in case of multiple inheritance')
258 for parent_name in ((type(parent_names)==list) and parent_names or [parent_names]):
259 parent_class = pool.get(parent_name).__class__
260 assert pool.get(parent_name), "parent class %s does not exist in module %s !" % (parent_name, module)
262 for s in ('_columns', '_defaults', '_inherits', '_constraints', '_sql_constraints'):
263 new = copy.copy(getattr(pool.get(parent_name), s))
264 if hasattr(new, 'update'):
265 new.update(cls.__dict__.get(s, {}))
267 if s=='_constraints':
268 for c in cls.__dict__.get(s, []):
270 for c2 in range(len(new)):
271 #For _constraints, we should check field and methods as well
272 if new[c2][2]==c[2] and new[c2][0]==c[0]:
279 new.extend(cls.__dict__.get(s, []))
281 cls = type(name, (cls, parent_class), nattr)
282 obj = object.__new__(cls)
283 obj.__init__(pool, cr)
285 createInstance = classmethod(createInstance)
287 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: