Commits

Yohan Boniface committed 1381dcf

Be a little bit more smart at in decoding strings at import (fix #74)

Comments (0)

Files changed (4)

leaflet_storage/tests/__init__.py

 from .views import *
 from .base_models import *
 from .fields import *
+from .utils import *

leaflet_storage/tests/utils.py

+# -*- coding:utf-8 -*-
+
+from django.test import TestCase
+
+from leaflet_storage.utils import smart_decode
+
+
+class SmartDecodeTests(TestCase):
+
+    def test_should_return_unicode(self):
+        self.assertTrue(isinstance(smart_decode('test'), unicode))
+        self.assertTrue(isinstance(smart_decode(u'test'), unicode))
+
+    def test_should_convert_utf8(self):
+        self.assertEqual(smart_decode('é'), u"é")

leaflet_storage/utils.py

             decorate(result, func)
 
     return result
+
+
+def smart_decode(s):
+    """Convert a str to unicode when you cannot be sure of its encoding."""
+    if isinstance(s, unicode):
+        return s
+    attempts = [
+        ('utf-8', 'strict', ),
+        ('latin-1', 'strict', ),
+        ('utf-8', 'replace', ),
+    ]
+    for args in attempts:
+        try:
+            s = s.decode(*args)
+        except:
+            continue
+        else:
+            break
+    return s

leaflet_storage/views.py

 from django.contrib import messages
 from django.utils import simplejson
 from django.core.signing import Signer, BadSignature
-from django.db.utils import DatabaseError
 from django.template import RequestContext
 from django.contrib.auth.models import User
 from django.views.generic import DetailView
 
 from .models import (Map, Marker, DataLayer, Polyline, TileLayer,
                      Polygon, Pictogram)
-from .utils import get_uri_template
+from .utils import get_uri_template, smart_decode
 from .forms import (QuickMapCreateForm, UpdateMapExtentForm, DataLayerForm,
                     UploadDataForm, UpdateMapPermissionsForm, MapSettingsForm,
                     MarkerForm, PolygonForm, PolylineForm, AnonymousMapPermissionsForm,
                         value = feature.properties[candidate]
                         if not value:
                             continue
+                        value = smart_decode(value)
                         if name in klass._meta.get_all_field_names():
                             kwargs[name] = value
                         else: