From 6a0dbfee1183264bf44a426966455b1275746e79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Mon, 29 Feb 2016 22:49:17 +0100 Subject: [PATCH] misc: make sure data sources with unicode are correctly serialized (#10048) --- tests/test_datasource.py | 11 +++++++++++ wcs/data_sources.py | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/test_datasource.py b/tests/test_datasource.py index 4a39ff6..19afdfe 100644 --- a/tests/test_datasource.py +++ b/tests/test_datasource.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + import json import sys import shutil @@ -212,3 +214,12 @@ def test_optional_item_field_with_data_source(): widget = form.get_widget('f1') assert widget is not None assert widget.options == [('1', 'un', '1'), ('2', 'deux', '2')] + +def test_data_source_unicode(): + NamedDataSource.wipe() + data_source = NamedDataSource(name='foobar') + data_source.data_source = {'type': 'formula', 'value': "['uné', 'deux']"} + data_source.store() + + data_source2 = NamedDataSource.select()[0] + assert data_source2.data_source == data_source.data_source diff --git a/wcs/data_sources.py b/wcs/data_sources.py index a41878f..6f0eea1 100644 --- a/wcs/data_sources.py +++ b/wcs/data_sources.py @@ -215,12 +215,12 @@ class NamedDataSource(XmlStorableObject): def export_data_source_to_xml(self, element, attribute_name, charset): data_source = getattr(self, attribute_name) ET.SubElement(element, 'type').text = data_source.get('type') - ET.SubElement(element, 'value').text = data_source.get('value') or '' + ET.SubElement(element, 'value').text = unicode(data_source.get('value') or '', charset) def import_data_source_from_xml(self, element, charset): return { 'type': str(element.find('type').text), - 'value': str(element.find('value').text), + 'value': (element.find('value').text or '').encode(charset) } def get_by_slug(cls, slug): -- 2.7.0