Merge lp:~ovnicraft/openobject-client/client-decimal-separator-fixed into lp:openobject-client/6.0

Proposed by Cristian Salamea
Status: Needs review
Proposed branch: lp:~ovnicraft/openobject-client/client-decimal-separator-fixed
Merge into: lp:openobject-client/6.0
Diff against target: 365 lines (+124/-44)
7 files modified
bin/tools/__init__.py (+0/-27)
bin/tools/user_locale_format.py (+29/-0)
bin/widget/view/form_gtk/spinbutton.py (+23/-4)
bin/widget/view/form_gtk/spinint.py (+23/-4)
bin/widget/view/tree_gtk/parser.py (+2/-2)
bin/widget_search/spinbutton.py (+24/-4)
bin/widget_search/spinint.py (+23/-3)
To merge this branch: bzr merge lp:~ovnicraft/openobject-client/client-decimal-separator-fixed
Reviewer Review Type Date Requested Status
OpenERP Core Team Pending
Review via email: mp+65998@code.launchpad.net

Description of the change

Fixed error with decimal separator reported in bug https://bugs.launchpad.net/openobject-client/+bug/697625

To post a comment you must log in.

Unmerged revisions

1846. By Cristian Salamea

[FIX] apply patch from trunk to fix decimal separator; bug related #697625

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bin/tools/__init__.py'
2--- bin/tools/__init__.py 2010-12-30 06:00:45 +0000
3+++ bin/tools/__init__.py 2011-06-27 14:17:54 +0000
4@@ -205,12 +205,6 @@
5
6 return unicode(value, from_encoding)
7
8-def locale_format(format, value):
9- label_str = locale.format(format, value, True, True)
10- if not locale.getpreferredencoding().lower().startswith('utf'):
11- label_str = label_str.replace('\xa0', '\xc2\xa0')
12- return label_str
13-
14 def format_connection_string(login, _passwd, server, port, protocol, dbname):
15 #def format_connection_string(*args):
16 # login, _passwd, server, port, protocol, dbname = args
17@@ -225,27 +219,6 @@
18 result += '/%s' % (dbname,)
19 return result
20
21-def str2int(string, default=None):
22- assert isinstance(string, basestring)
23- try:
24- integer = locale.atoi(string)
25- return integer
26- except:
27- if default is not None:
28- return default
29- raise ValueError("%r does not represent a valid integer value" % (string,))
30-
31-
32-def str2float(string, default=None):
33- assert isinstance(string, basestring)
34- try:
35- float = locale.atof(string)
36- return float
37- except:
38- if default is not None:
39- return default
40- raise ValueError("%r does not represent a valid float value" % (string,))
41-
42 def str2bool(string, default=None):
43 """Convert a string representing a boolean into the corresponding boolean
44
45
46=== modified file 'bin/tools/user_locale_format.py'
47--- bin/tools/user_locale_format.py 2011-01-17 07:21:19 +0000
48+++ bin/tools/user_locale_format.py 2011-06-27 14:17:54 +0000
49@@ -195,3 +195,32 @@
50 if grouping:
51 formatted = group(formatted, monetary=monetary, grouping=lang_grouping, thousands_sep=thousands_sep)[0]
52 return formatted
53+
54+def str2int(string):
55+ ''' Converts a string to an integer according to the locale settings
56+ that the user has in Administration/Translations/Languages.
57+ '''
58+ assert isinstance(string, basestring)
59+ return str2float(string, int)
60+
61+def str2float(string, func=float):
62+ ''' Parses a string as a float according to the locale settings
63+ that the user has in Administration/Translations/Languages.
64+ '''
65+ assert isinstance(string, basestring)
66+ try:
67+ #First, get rid of the thousand separator
68+ ts = LOCALE_CACHE.get('thousands_sep')
69+ if ts:
70+ string = string.replace(ts, '')
71+ #next, replace the decimal point with a dot
72+ dd = LOCALE_CACHE.get('decimal_point')
73+ if dd:
74+ string = string.replace(dd, '.')
75+ #finally, parse the string
76+ return func(string)
77+ except:
78+ type = 'float'
79+ if func == int:
80+ type = 'integer'
81+ raise ValueError("%r does not represent a valid %s value" % (string,type))
82
83=== modified file 'bin/widget/view/form_gtk/spinbutton.py'
84--- bin/widget/view/form_gtk/spinbutton.py 2010-07-16 05:41:32 +0000
85+++ bin/widget/view/form_gtk/spinbutton.py 2011-06-27 14:17:54 +0000
86@@ -1,6 +1,6 @@
87 # -*- coding: utf-8 -*-
88 ##############################################################################
89-#
90+#
91 # OpenERP, Open Source Management Solution
92 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
93 #
94@@ -15,14 +15,16 @@
95 # GNU Affero General Public License for more details.
96 #
97 # You should have received a copy of the GNU Affero General Public License
98-# along with this program. If not, see <http://www.gnu.org/licenses/>.
99+# along with this program. If not, see <http://www.gnu.org/licenses/>.
100 #
101 ##############################################################################
102
103 import gtk
104 import sys
105+import ctypes
106 import interface
107-
108+import tools
109+from tools import user_locale_format
110
111 class spinbutton(interface.widget_interface):
112 def __init__(self, window, parent, model, attrs={}):
113@@ -30,7 +32,6 @@
114
115 adj = gtk.Adjustment(0.0, -sys.maxint, sys.maxint, 1.0, 5.0)
116 self.widget = gtk.SpinButton(adj, 1.0, digits=int( attrs.get('digits',(14,2))[1] ) )
117- self.widget.set_numeric(True)
118 self.widget.set_activates_default(True)
119 self.widget.connect('populate-popup', self._menu_open)
120 if self.attrs['readonly']:
121@@ -38,6 +39,24 @@
122 self.widget.connect('focus-in-event', lambda x,y: self._focus_in())
123 self.widget.connect('focus-out-event', lambda x,y: self._focus_out())
124 self.widget.connect('activate', self.sig_activate)
125+ self.widget.connect('input', self.format_input)
126+ self.widget.connect('output', self.format_output)
127+
128+ def format_output(self, spin):
129+ digits = spin.get_digits()
130+ value = spin.get_value()
131+ text = user_locale_format.format('%.' + str(digits) + 'f', value)
132+ spin.set_text(text)
133+ return True
134+
135+ def format_input(self, spin, new_value_pointer):
136+ text = spin.get_text()
137+ if text:
138+ value = user_locale_format.str2float(text)
139+ value_location = ctypes.c_double.from_address(hash(new_value_pointer))
140+ value_location.value = float(value)
141+ return True
142+ return False
143
144 def set_value(self, model, model_field):
145 self.widget.update()
146
147=== modified file 'bin/widget/view/form_gtk/spinint.py'
148--- bin/widget/view/form_gtk/spinint.py 2010-07-16 05:41:32 +0000
149+++ bin/widget/view/form_gtk/spinint.py 2011-06-27 14:17:54 +0000
150@@ -1,6 +1,6 @@
151 # -*- coding: utf-8 -*-
152 ##############################################################################
153-#
154+#
155 # OpenERP, Open Source Management Solution
156 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
157 #
158@@ -15,14 +15,16 @@
159 # GNU Affero General Public License for more details.
160 #
161 # You should have received a copy of the GNU Affero General Public License
162-# along with this program. If not, see <http://www.gnu.org/licenses/>.
163+# along with this program. If not, see <http://www.gnu.org/licenses/>.
164 #
165 ##############################################################################
166
167 import gtk
168 import sys
169+import ctypes
170 import interface
171-
172+import tools
173+from tools import user_locale_format
174
175 class spinint(interface.widget_interface):
176
177@@ -31,7 +33,6 @@
178
179 adj = gtk.Adjustment(0.0, -sys.maxint, sys.maxint, 1.0, 5.0)
180 self.widget = gtk.SpinButton(adj, 1, digits=0)
181- self.widget.set_numeric(True)
182 self.widget.set_width_chars(5)
183 self.widget.set_activates_default(True)
184 self.widget.connect('populate-popup', self._menu_open)
185@@ -40,6 +41,24 @@
186 self.widget.connect('focus-in-event', lambda x,y: self._focus_in())
187 self.widget.connect('focus-out-event', lambda x,y: self._focus_out())
188 self.widget.connect('activate', self.sig_activate)
189+ self.widget.connect('input', self.format_input)
190+ self.widget.connect('output', self.format_output)
191+
192+ def format_output(self, spin):
193+ digits = spin.get_digits()
194+ value = spin.get_value()
195+ text = user_locale_format.format('%.' + str(digits) + 'f', value)
196+ spin.set_text(text)
197+ return True
198+
199+ def format_input(self, spin, new_value_pointer):
200+ text = spin.get_text()
201+ if text:
202+ value = user_locale_format.str2int(text)
203+ value_location = ctypes.c_double.from_address(hash(new_value_pointer))
204+ value_location.value = float(value)
205+ return True
206+ return False
207
208 def set_value(self, model, model_field):
209 self.widget.update()
210
211=== modified file 'bin/widget/view/tree_gtk/parser.py'
212--- bin/widget/view/tree_gtk/parser.py 2011-01-18 06:45:01 +0000
213+++ bin/widget/view/tree_gtk/parser.py 2011-06-27 14:17:54 +0000
214@@ -343,7 +343,7 @@
215 class Int(Char):
216
217 def value_from_text(self, model, text):
218- return tools.str2int(text)
219+ return user_locale_format.str2int(text)
220
221 def get_textual_value(self, model):
222 count = False
223@@ -472,7 +472,7 @@
224 return converted_val
225
226 def value_from_text(self, model, text):
227- return tools.str2float(text)
228+ return user_locale_format.str2float(text)
229
230 class FloatTime(Char):
231
232
233=== modified file 'bin/widget_search/spinbutton.py'
234--- bin/widget_search/spinbutton.py 2010-09-21 09:39:49 +0000
235+++ bin/widget_search/spinbutton.py 2011-06-27 14:17:54 +0000
236@@ -23,7 +23,9 @@
237 import common
238 import wid_int
239 import sys
240+import ctypes
241 import tools
242+from tools import user_locale_format
243
244 class spinbutton(wid_int.wid_int):
245 def __init__(self, name, parent, attrs={},screen=None):
246@@ -33,21 +35,39 @@
247
248 adj1 = gtk.Adjustment(0.0, -sys.maxint, sys.maxint, 1.0, 5.0)
249 self.spin1 = gtk.SpinButton(adj1, 1.0, digits=int(attrs.get('digits', (14, 2))[1]))
250- self.spin1.set_numeric(True)
251 self.spin1.set_activates_default(True)
252+ self.spin1.connect('input', self.format_input)
253+ self.spin1.connect('output', self.format_output)
254 self.widget.pack_start(self.spin1, expand=False, fill=True)
255
256 self.widget.pack_start(gtk.Label('-'), expand=False, fill=False)
257
258 adj2 = gtk.Adjustment(0.0, -sys.maxint, sys.maxint, 1.0, 5.0)
259 self.spin2 = gtk.SpinButton(adj2, 1.0, digits=int(attrs.get('digits', (14, 2))[1]))
260- self.spin2.set_numeric(True)
261 self.spin2.set_activates_default(True)
262+ self.spin2.connect('input', self.format_input)
263+ self.spin2.connect('output', self.format_output)
264 self.widget.pack_start(self.spin2, expand=False, fill=True)
265
266 if self.default_search:
267 self.spin1.set_value(self.default_search)
268
269+ def format_output(self, spin):
270+ digits = spin.get_digits()
271+ value = spin.get_value()
272+ text = user_locale_format.format('%.' + str(digits) + 'f', value)
273+ spin.set_text(text)
274+ return True
275+
276+ def format_input(self, spin, new_value_pointer):
277+ text = spin.get_text()
278+ if text:
279+ value = user_locale_format.str2float(text)
280+ value_location = ctypes.c_double.from_address(hash(new_value_pointer))
281+ value_location.value = float(value)
282+ return True
283+ return False
284+
285 def _value_get(self):
286 res = []
287 self.spin1.update()
288@@ -73,8 +93,8 @@
289
290 def clear(self):
291 self.value = 0.00
292-
293-
294+
295+
296 def grab_focus(self):
297 self.spin1.grab_focus()
298
299
300=== modified file 'bin/widget_search/spinint.py'
301--- bin/widget_search/spinint.py 2010-09-21 09:39:49 +0000
302+++ bin/widget_search/spinint.py 2011-06-27 14:17:54 +0000
303@@ -21,11 +21,13 @@
304
305 import gtk
306 from gtk import glade
307+import ctypes
308 import gettext
309 import sys
310 import common
311 import wid_int
312 import tools
313+from tools import user_locale_format
314
315
316 class spinint(wid_int.wid_int):
317@@ -37,20 +39,38 @@
318
319 adj1 = gtk.Adjustment(0.0, 0.0, sys.maxint, 1.0, 5.0)
320 self.spin1 = gtk.SpinButton(adj1, 1, digits=0)
321- self.spin1.set_numeric(True)
322 self.spin1.set_activates_default(True)
323+ self.spin1.connect('input', self.format_input)
324+ self.spin1.connect('output', self.format_output)
325 self.widget.pack_start(self.spin1, expand=False, fill=True)
326
327 self.widget.pack_start(gtk.Label('-'), expand=False, fill=False)
328
329 adj2 = gtk.Adjustment(0.0, 0.0, sys.maxint, 1.0, 5.0)
330 self.spin2 = gtk.SpinButton(adj2, 1, digits=0)
331- self.spin2.set_numeric(True)
332 self.spin2.set_activates_default(True)
333+ self.spin2.connect('input', self.format_input)
334+ self.spin2.connect('output', self.format_output)
335 self.widget.pack_start(self.spin2, expand=False, fill=True)
336 if self.default_search:
337 self.spin1.set_value(self.default_search)
338
339+ def format_output(self, spin):
340+ digits = spin.get_digits()
341+ value = spin.get_value()
342+ text = user_locale_format.format('%.' + str(digits) + 'f', value)
343+ spin.set_text(text)
344+ return True
345+
346+ def format_input(self, spin, new_value_pointer):
347+ text = spin.get_text()
348+ if text:
349+ value = user_locale_format.str2int(text)
350+ value_location = ctypes.c_double.from_address(hash(new_value_pointer))
351+ value_location.value = float(value)
352+ return True
353+ return False
354+
355 def _value_get(self):
356 res = []
357 self.spin1.update()
358@@ -76,7 +96,7 @@
359
360 def clear(self):
361 self.value = 0.0
362-
363+
364 def grab_focus(self):
365 self.spin1.grab_focus()
366