Merge lp:~toolpart/openerp-client-lib/default_context into lp:openerp-client-lib

Proposed by ViktorNagy
Status: Rejected
Rejected by: Nicolas Vanhoren (OpenERP)
Proposed branch: lp:~toolpart/openerp-client-lib/default_context
Merge into: lp:openerp-client-lib
Diff against target: 159 lines (+37/-17)
1 file modified
openerplib/main.py (+37/-17)
To merge this branch: bzr merge lp:~toolpart/openerp-client-lib/default_context
Reviewer Review Type Date Requested Status
Nicolas Vanhoren (OpenERP) Disapprove
Review via email: mp+76896@code.launchpad.net

Description of the change

Backward compatible addition of context support.

To post a comment you must log in.
Revision history for this message
Nicolas Vanhoren (OpenERP) (niv-openerp) wrote :

Hello,

You can't implicitly add a context to each method call, for these reasons:

- A lot of methods have optional arguments before the context, if those arguments are not provided the context will be inserted at the wrong place.
- There are some methods in the openerp server and addons where the context is not the last argument (this should be changed in openerp 6.2).

So, I agree this functionality would be a good thing, but it's not technically possible right now.

Revision history for this message
Nicolas Vanhoren (OpenERP) (niv-openerp) :
review: Disapprove
Revision history for this message
Nicolas Vanhoren (OpenERP) (niv-openerp) wrote :

... but in the next version of the client lib I plan to implement that functionality, because we modified the openerp server to make it possible to implement.

It will only be usable using a OpenERP server v 6.1

Unmerged revisions

31. By ViktorNagy

Added default_context argument to Connection.
Added set_context method to connection
Added set_user_lang method to connection. This sets the default_context to include the user's language settings.

Added add_context argument to Model.__init__ (by default equals True). If True, then the Connection's default context is added to every request of the proxy.
Modified the proxy method to check for self.add_context, and add the connection's context if necessary.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'openerplib/main.py'
2--- openerplib/main.py 2011-09-22 15:45:57 +0000
3+++ openerplib/main.py 2011-09-25 10:35:24 +0000
4@@ -5,16 +5,16 @@
5 # Copyright (C) 2011 Nicolas Vanhoren
6 # Copyright (C) 2011 OpenERP s.a. (<http://openerp.com>).
7 # All rights reserved.
8-#
9+#
10 # Redistribution and use in source and binary forms, with or without
11-# modification, are permitted provided that the following conditions are met:
12-#
13+# modification, are permitted provided that the following conditions are met:
14+#
15 # 1. Redistributions of source code must retain the above copyright notice, this
16-# list of conditions and the following disclaimer.
17+# list of conditions and the following disclaimer.
18 # 2. Redistributions in binary form must reproduce the above copyright notice,
19 # this list of conditions and the following disclaimer in the documentation
20-# and/or other materials provided with the distribution.
21-#
22+# and/or other materials provided with the distribution.
23+#
24 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
25 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27@@ -25,7 +25,7 @@
28 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31-#
32+#
33 ##############################################################################
34
35 """
36@@ -36,7 +36,7 @@
37 """
38
39 import xmlrpclib
40-import logging
41+import logging
42 import socket
43
44 try:
45@@ -75,7 +75,7 @@
46 A type of connector that uses the XMLRPC protocol.
47 """
48 PROTOCOL = 'xmlrpc'
49-
50+
51 __logger = _getChildLogger(_logger, 'connector.xmlrpc')
52
53 def __init__(self, hostname, port=8069):
54@@ -171,7 +171,7 @@
55 """
56
57 PROTOCOL = 'netrpc'
58-
59+
60 __logger = _getChildLogger(_logger, 'connector.netrpc')
61
62 def __init__(self, hostname, port=8070):
63@@ -202,7 +202,7 @@
64 self.connector = connector
65 self.service_name = service_name
66 self.__logger = _getChildLogger(_getChildLogger(_logger, 'service'),service_name)
67-
68+
69 def __getattr__(self, method):
70 """
71 :param method: The name of the method to execute on the service.
72@@ -224,6 +224,7 @@
73 It also provides utility methods to interact with the server more easily.
74 """
75 __logger = _getChildLogger(_logger, 'connection')
76+ default_context = {}
77
78 def __init__(self, connector,
79 database=None,
80@@ -259,7 +260,7 @@
81 self.database, self.login, self.password = database, login, password
82
83 self.user_id = user_id
84-
85+
86 def check_login(self, force=True):
87 """
88 Checks that the login information is valid. Throws an AuthenticationError if the
89@@ -270,15 +271,15 @@
90 """
91 if self.user_id and not force:
92 return
93-
94+
95 if not self.database or not self.login or self.password is None:
96 raise AuthenticationError("Creditentials not provided")
97-
98+
99 self.user_id = self.get_service("common").login(self.database, self.login, self.password)
100 if not self.user_id:
101 raise AuthenticationError("Authentication failure")
102 self.__logger.debug("Authenticated with user id %s", self.user_id)
103-
104+
105 def get_model(self, model_name):
106 """
107 Returns a Model instance to allow easy remote manipulation of an OpenERP model.
108@@ -297,6 +298,21 @@
109 """
110 return Service(self.connector, service_name)
111
112+ def set_user_lang(self):
113+ if not self.user_id:
114+ self.check_login()
115+
116+ self.default_context.update({
117+ 'lang': self.get_model('res.users').read(self.user_id,
118+ ['context_lang'])['context_lang'],
119+ })
120+
121+ def set_context(self, context, reset=False):
122+ if reset:
123+ self.default_context = context
124+ else:
125+ self.default_context.update(context)
126+
127 class AuthenticationError(Exception):
128 """
129 An error thrown when an authentication to an OpenERP server failed.
130@@ -309,11 +325,13 @@
131 An instance of this class depends on a Connection instance with valid authentication information.
132 """
133
134- def __init__(self, connection, model_name):
135+ def __init__(self, connection, model_name, add_context=True):
136 """
137 :param connection: A valid Connection instance with correct authentication information.
138 :param model_name: The name of the model.
139+ :param add_context: Automatically adds the connection's default context to requests
140 """
141+ self.add_default_context = add_context
142 self.connection = connection
143 self.model_name = model_name
144 self.__logger = _getChildLogger(_getChildLogger(_logger, 'object'), model_name)
145@@ -328,6 +346,8 @@
146 """
147 :param args: A list of values for the method
148 """
149+ if self.add_default_context:
150+ args = args + (self.connection.default_context,)
151 self.connection.check_login(False)
152 self.__logger.debug(args)
153 result = self.connection.get_service('object').execute(
154@@ -396,4 +416,4 @@
155 already know it, in most cases you don't need to specify it.
156 """
157 return Connection(get_connector(hostname, protocol, port), database, login, password, user_id)
158-
159+

Subscribers

People subscribed via source and target branches