Merge lp:~openerp-dev/openobject-server/trunk-check-ids-thu into lp:openobject-server

Proposed by Vo Minh Thu
Status: Needs review
Proposed branch: lp:~openerp-dev/openobject-server/trunk-check-ids-thu
Merge into: lp:openobject-server
Diff against target: 87 lines (+31/-9)
3 files modified
openerp/addons/base/res/res_users.py (+3/-0)
openerp/conf/deprecation.py (+21/-0)
openerp/osv/orm.py (+7/-9)
To merge this branch: bzr merge lp:~openerp-dev/openobject-server/trunk-check-ids-thu
Reviewer Review Type Date Requested Status
OpenERP Core Team Pending
Review via email: mp+162594@code.launchpad.net
To post a comment you must log in.
4827. By Vo Minh Thu

[MERGE] merged trunk.

Unmerged revisions

4827. By Vo Minh Thu

[MERGE] merged trunk.

4826. By Vo Minh Thu

[REF] orm: added a deprecation warning helper for IDs check.

4825. By Vo Minh Thu

[REF] orm: deprecation warning when passing a single ID to read().

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'openerp/addons/base/res/res_users.py'
2--- openerp/addons/base/res/res_users.py 2013-04-04 13:07:04 +0000
3+++ openerp/addons/base/res/res_users.py 2013-05-06 13:13:33 +0000
4@@ -256,6 +256,8 @@
5 SELF_READABLE_FIELDS = ['signature', 'company_id', 'login', 'email', 'name', 'image', 'image_medium', 'image_small', 'lang', 'tz', 'tz_offset', 'groups_id', 'partner_id', '__last_update']
6
7 def read(self, cr, uid, ids, fields=None, context=None, load='_classic_read'):
8+ openerp.conf.deprecation.check_single_id(self, 'read', ids, _logger)
9+
10 def override_password(o):
11 if 'password' in o and ('id' not in o or o['id'] != uid):
12 o['password'] = '********'
13@@ -280,6 +282,7 @@
14 return result
15
16 def write(self, cr, uid, ids, values, context=None):
17+ openerp.conf.deprecation.check_single_id(self, 'write', ids, _logger)
18 if not hasattr(ids, '__iter__'):
19 ids = [ids]
20 if ids == [uid]:
21
22=== modified file 'openerp/conf/deprecation.py'
23--- openerp/conf/deprecation.py 2013-03-27 16:40:45 +0000
24+++ openerp/conf/deprecation.py 2013-05-06 13:13:33 +0000
25@@ -37,6 +37,27 @@
26 # Change to False around 2013.02.
27 open_openerp_namespace = False
28
29+# If True the old behavior of allowing a single ID to the orm methods is kept
30+# but a warning is dieplayed in the logs. Otherwise raise an exception.
31+# Introduced around 2013.03.
32+allow_single_id = True
33+
34+def check_single_id(model, method, ids, logger):
35+ if isinstance(ids, (int, long)):
36+ if allow_single_id:
37+ logger.warning('Passing a single ID to %s.%s() is deprecated.\n%s', model._name, method, get_caller(up=3))
38+ else:
39+ raise TypeError, 'Cannot pass a single ID to %s.%s().' % (model._name, method)
40+
41+def get_caller(up=2):
42+ import inspect
43+ try:
44+ frame,filename,line_number,function_name,lines,index=\
45+ inspect.getouterframes(inspect.currentframe())[up]
46+ return "Called in %s() at %s:%s." % (function_name, filename, line_number)
47+ except Exception, e:
48+ return "Called in an unknown location, probably inside a safe_eval()."
49+
50 # If True, openerp.netsvc.LocalService() can be used to lookup reports or to
51 # access openerp.workflow.
52 # Introduced around 2013.03.
53
54=== modified file 'openerp/osv/orm.py'
55--- openerp/osv/orm.py 2013-04-22 09:36:55 +0000
56+++ openerp/osv/orm.py 2013-05-06 13:13:33 +0000
57@@ -3599,16 +3599,13 @@
58
59 self.check_access_rights(cr, user, 'read')
60 fields = self.check_field_access_rights(cr, user, 'read', fields)
61- if isinstance(ids, (int, long)):
62- select = [ids]
63- else:
64- select = ids
65- select = map(lambda x: isinstance(x, dict) and x['id'] or x, select)
66- result = self._read_flat(cr, user, select, fields, context, load)
67-
68- if isinstance(ids, (int, long)):
69+ openerp.conf.deprecation.check_single_id(self, 'read', ids, _logger)
70+ if isinstance(ids, (int, long)):
71+ result = self._read_flat(cr, user, [ids], fields, context, load)
72 return result and result[0] or False
73- return result
74+ else:
75+ result = self._read_flat(cr, user, ids, fields, context, load)
76+ return result
77
78 def _read_flat(self, cr, user, ids, fields_to_read, context=None, load='_classic_read'):
79 if not context:
80@@ -4105,6 +4102,7 @@
81
82 if not context:
83 context = {}
84+ openerp.conf.deprecation.check_single_id(self, 'write', ids, _logger)
85 if not ids:
86 return True
87 if isinstance(ids, (int, long)):