lims_interface: be able to define the charset of source files

This commit is contained in:
Adrián Bernardi 2020-09-15 10:16:37 -03:00
parent 789b2734d9
commit dea6cacf9a
4 changed files with 27 additions and 9 deletions

View File

@ -232,7 +232,8 @@ class Interface(Workflow, ModelSQL, ModelView):
charset = fields.Selection([
(None, ''),
('utf-8', 'UTF-8'),
('iso-8859', 'ISO-8859')], 'Charset')
('iso-8859-1', 'ISO-8859-1'),
], 'Charset')
del _controller_states, _template_states, _depends
@ -1053,6 +1054,7 @@ class Compilation(Workflow, ModelSQL, ModelView):
}
delimiter = separator[self.interface.field_separator]
first_row = self.interface.first_row - 1
encoding = self.interface.charset
with Transaction().set_context(
lims_interface_table=self.table):
imported_files = []
@ -1060,8 +1062,12 @@ class Compilation(Workflow, ModelSQL, ModelView):
if origin.imported:
continue
filedata = io.BytesIO(origin.origin_file)
wrapper = io.TextIOWrapper(filedata, encoding='utf-8')
str_data = io.StringIO(wrapper.read())
wrapper = io.TextIOWrapper(filedata, encoding=encoding)
try:
str_data = io.StringIO(wrapper.read())
except UnicodeDecodeError:
raise UserError(gettext(
'lims_interface.invalid_interface_charset'))
reader = csv.reader(str_data, delimiter=delimiter)
count = 0
for row in reader:
@ -1102,7 +1108,7 @@ class Compilation(Workflow, ModelSQL, ModelView):
schema[k]['field_name'])
line[k] = resource[0].id
else:
line[k] = str(row[col - 1])
line[k] = str(value)
f_fields = sorted(formula_fields.items(),
key=lambda x: x[1]['evaluation_order'])

View File

@ -555,6 +555,13 @@ msgctxt "model:ir.message,text:invalid_default_value_type"
msgid "The field type in \"%(name)s\" is not valid for default values."
msgstr "El Tipo de campo en \"%(name)s\" no permite valores por defecto."
msgctxt "model:ir.message,text:invalid_interface_charset"
msgid ""
"The charset of the file does not match the one defined in the interface."
msgstr ""
"El conjunto de caracteres del archivo no coincide con el definido en la "
"interfaz."
msgctxt "model:ir.message,text:msg_interface_column_alias_unique"
msgid "There cannot be two columns with the same alias in an interface."
msgstr "No puede haber dos columnas con el mismo alias en una interfaz."
@ -673,8 +680,8 @@ msgid "Interface Variable Value"
msgstr "Valor de Variable de Interfaz"
msgctxt "selection:lims.interface,charset:"
msgid "ISO-8859"
msgstr "ISO-8859"
msgid "ISO-8859-1"
msgstr "ISO-8859-1"
msgctxt "selection:lims.interface,charset:"
msgid "UTF-8"

View File

@ -31,5 +31,8 @@
<record model="ir.message" id="delete_done_compilation">
<field name="text">You cannot delete a Compilation in "Done" state</field>
</record>
<record model="ir.message" id="invalid_interface_charset">
<field name="text">The charset of the file does not match the one defined in the interface.</field>
</record>
</data>
</tryton>

View File

@ -30,11 +30,13 @@
<page id="template" string="Template">
<label name="template_type"/>
<field name="template_type"/>
<label name="first_row"/>
<field name="first_row"/>
<label name="charset"/>
<field name="charset"/>
<label name="language"/>
<field name="language" widget="selection"/>
<group id="separators" colspan="4" col="6">
<label name="first_row"/>
<field name="first_row"/>
<group id="separators" colspan="4" col="4">
<label name="field_separator"/>
<field name="field_separator"/>
<label name="field_separator_other"/>