changeset 4168b4eeecdc in tryton:default
details: https://hg.tryton.org/tryton?cmd=changeset;node=4168b4eeecdc
description:
Use locale format for data export and import
issue8196
review255151002
diffstat:
CHANGELOG | 1 +
tryton/common/common.py | 2 +-
tryton/gui/window/win_csv.py | 4 ++++
tryton/gui/window/win_export.py | 19 ++++++++++++++++++-
tryton/gui/window/win_import.py | 19 ++++++++++++++++++-
5 files changed, 42 insertions(+), 3 deletions(-)
diffs (128 lines):
diff -r b553daa13fce -r 4168b4eeecdc CHANGELOG
--- a/CHANGELOG Thu Mar 28 12:25:23 2019 +0100
+++ b/CHANGELOG Thu Mar 28 19:22:49 2019 +0100
@@ -1,3 +1,4 @@
+* Use locale format for data import and export
* Add day view on calendar
* Add CSV exports into print toolbar
* Display unread and total of notes
diff -r b553daa13fce -r 4168b4eeecdc tryton/common/common.py
--- a/tryton/common/common.py Thu Mar 28 12:25:23 2019 +0100
+++ b/tryton/common/common.py Thu Mar 28 19:22:49 2019 +0100
@@ -1230,7 +1230,7 @@
return Gtk.Align.END
-def date_format(format_):
+def date_format(format_=None):
return format_ or rpc.CONTEXT.get('locale', {}).get('date', '%x')
diff -r b553daa13fce -r 4168b4eeecdc tryton/gui/window/win_csv.py
--- a/tryton/gui/window/win_csv.py Thu Mar 28 12:25:23 2019 +0100
+++ b/tryton/gui/window/win_csv.py Thu Mar 28 19:22:49 2019 +0100
@@ -171,6 +171,10 @@
label_csv_enc.set_mnemonic_widget(self.csv_enc)
box.pack_start(self.csv_enc, expand=False, fill=True, padding=0)
+ self.csv_locale = Gtk.CheckButton(label=_("Use locale format"))
+ self.csv_locale.set_active(True)
+ box.pack_start(self.csv_locale, expand=False, fill=True, padding=0)
+
self.add_csv_header_param(box)
button_cancel = self.dialog.add_button(
diff -r b553daa13fce -r 4168b4eeecdc tryton/gui/window/win_export.py
--- a/tryton/gui/window/win_export.py Thu Mar 28 12:25:23 2019 +0100
+++ b/tryton/gui/window/win_export.py Thu Mar 28 19:22:49 2019 +0100
@@ -1,9 +1,12 @@
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import csv
+import datetime
import os
import tempfile
import gettext
+import locale
+from numbers import Number
from gi.repository import Gdk, GObject, Gtk
@@ -300,6 +303,7 @@
def export_csv(self, fname, fields, data, popup=True):
encoding = self.csv_enc.get_active_text() or 'UTF-8'
+ locale_format = self.csv_locale.get_active()
try:
writer = csv.writer(
@@ -308,7 +312,20 @@
delimiter=self.get_delimiter())
if self.add_field_names.get_active():
writer.writerow(fields)
- writer.writerows(data)
+ for line in data:
+ row = []
+ for val in line:
+ if locale_format:
+ if isinstance(val, Number):
+ val = locale.str(val)
+ elif isinstance(val, datetime.datetime):
+ val = val.strftime(common.date_format() + ' %X')
+ elif isinstance(val, datetime.date):
+ val = val.strftime(common.date_format())
+ elif isinstance(val, bool):
+ val = int(val)
+ row.append(val)
+ writer.writerow(row)
if popup:
if len(data) == 1:
common.message(_('%d record saved.') % len(data))
diff -r b553daa13fce -r 4168b4eeecdc tryton/gui/window/win_import.py
--- a/tryton/gui/window/win_import.py Thu Mar 28 12:25:23 2019 +0100
+++ b/tryton/gui/window/win_import.py Thu Mar 28 19:22:49 2019 +0100
@@ -2,11 +2,14 @@
# this repository contains the full copyright notices and license terms.
import csv
import gettext
+import locale
+from decimal import Decimal
from gi.repository import Gtk
import tryton.common as common
from tryton.common import RPCExecute, RPCException
+from tryton.common.datetime_ import date_parse
from tryton.gui.window.win_csv import WinCSV
_ = gettext.gettext
@@ -183,6 +186,7 @@
# TODO: make it works with references
skip = self.csv_skip.get_value_as_int()
encoding = self.get_encoding()
+ locale_format = self.csv_locale.get_active()
reader = csv.reader(
open(fname, 'r', encoding=encoding),
quotechar=self.get_quotechar(),
@@ -191,7 +195,20 @@
for i, line in enumerate(reader):
if i < skip or not line:
continue
- data.append([x for x in line])
+ row = []
+ for field, val in zip(fields, line):
+ if locale_format:
+ type_ = self.fields_data[field]['type']
+ if type_ in ['integer', 'biginteger']:
+ val = locale.atoi(val)
+ elif type_ == 'float':
+ val = locale.atof(val)
+ elif type_ == 'numeric':
+ val = locale.atof(val, Decimal)
+ elif type_ in ['date', 'datetime']:
+ val = date_parse(val, common.date_format())
+ row.append(val)
+ data.append(row)
try:
count = RPCExecute(
'model', self.model, 'import_data', fields, data,