Commits

Martin Skala  committed 94af664

Add utf-8 as a fallback encoding if DEFAULT_CHARSET is not set

  • Participants
  • Parent commits 8997cce

Comments (0)

Files changed (5)

File django_roa/db/models.py

 
 ROA_MODEL_NAME_MAPPING = getattr(settings, 'ROA_MODEL_NAME_MAPPING', [])
 ROA_HEADERS = getattr(settings, 'ROA_HEADERS', {})
+DEFAULT_CHARSET = getattr(settings, 'DEFAULT_CHARSET', 'utf-8')
 
 class ROAModelBase(ModelBase):
     def __new__(cls, name, bases, attrs):
                 except RequestFailed, e:
                     raise ROAException(e)
             
-            response = force_unicode(response.body).encode(settings.DEFAULT_CHARSET)
+            response = force_unicode(response.body).encode(DEFAULT_CHARSET)
             
             for local_name, remote_name in ROA_MODEL_NAME_MAPPING:
                 response = response.replace(remote_name, local_name)

File django_roa/db/query.py

 ROA_MODEL_NAME_MAPPING = getattr(settings, 'ROA_MODEL_NAME_MAPPING', [])
 ROA_ARGS_NAMES_MAPPING = getattr(settings, 'ROA_ARGS_NAMES_MAPPING', {})
 ROA_HEADERS = getattr(settings, 'ROA_HEADERS', {})
-
+DEFAULT_CHARSET = getattr(settings, 'DEFAULT_CHARSET', 'utf-8')
 
 class Query(object):
     def __init__(self):
         except Exception, e:
             raise ROAException(e)
 
-        response = force_unicode(response.body).encode(settings.DEFAULT_CHARSET)
+        response = force_unicode(response.body).encode(DEFAULT_CHARSET)
         for local_name, remote_name in ROA_MODEL_NAME_MAPPING:
             response = response.replace(remote_name, local_name)
         
         except Exception, e:
             raise ROAException(e)
         
-        response = force_unicode(response.body).encode(settings.DEFAULT_CHARSET)
+        response = force_unicode(response.body).encode(DEFAULT_CHARSET)
         for local_name, remote_name in ROA_MODEL_NAME_MAPPING:
             response = response.replace(remote_name, local_name)
         

File examples/django_roa_client/serializers.py

 from django.core.serializers.xml_serializer import Serializer as XMLSerializer, \
                                                    Deserializer as XMLDeserializer
 
+DEFAULT_CHARSET = getattr(settings, 'DEFAULT_CHARSET', 'utf-8')
+
 class Serializer(XMLSerializer):
     """
     A test serializer which removes ``django-objects`` xml tag from default
         """
         Start serialization -- open the XML document and the root element.
         """
-        self.xml = SimplerXMLGenerator(self.stream, self.options.get("encoding", settings.DEFAULT_CHARSET))
+        self.xml = SimplerXMLGenerator(self.stream, self.options.get("encoding", DEFAULT_CHARSET))
         self.xml.startDocument()
         self.xml.startElement("django-test", {"version" : "1.0"})
 

File examples/django_roa_server/emitters.py

 
 logger = logging.getLogger("django_roa_server")
 
+DEFAULT_CHARSET = getattr(settings, 'DEFAULT_CHARSET', 'utf-8')
 
 class ROADjangoEmitter(DjangoEmitter):
     """
     """
     def render(self, request):
         response = super(ROADjangoEmitter, self).render(request, 'xml')
-        logger.debug(u"Response:\n%s" % str(response).decode(settings.DEFAULT_CHARSET))
+        logger.debug(u"Response:\n%s" % str(response).decode(DEFAULT_CHARSET))
         return response
     
 Emitter.register('django', ROADjangoEmitter, 'application/xml; charset=utf-8')
     """
     def render(self, request):
         response = super(CustomDjangoEmitter, self).render(request, 'custom')
-        logger.debug(u"Response:\n%s" % response.decode(settings.DEFAULT_CHARSET))
+        logger.debug(u"Response:\n%s" % response.decode(DEFAULT_CHARSET))
         return response
     
 Emitter.register('custom', CustomDjangoEmitter, 'application/xml; charset=utf-8')

File examples/twitter_roa/serializers.py

 from django.core.serializers.json import Serializer as JSONSerializer
 from django.core.serializers.python import _get_model
 
+DEFAULT_CHARSET = getattr(settings, 'DEFAULT_CHARSET', 'utf-8')
+
 class Serializer(JSONSerializer):
     pass
 
         # Handle each field
         for (field_name, field_value) in obj.iteritems():
             if isinstance(field_value, str):
-                field_value = smart_unicode(field_value, options.get("encoding", settings.DEFAULT_CHARSET), strings_only=True)
+                field_value = smart_unicode(field_value, options.get("encoding", DEFAULT_CHARSET), strings_only=True)
         
             try:
                 field = Model._meta.get_field(field_name)