durusmail: quixote-users: Default page encoding?
Default page encoding?
2005-01-26
Default page encoding?
Neil Schemenauer
2005-01-29
On Wed, Jan 26, 2005 at 08:48:43AM -0500, Kevin Dangoor wrote:
> If there *is* currently a way to set the default, that would be nice to
> know.

There is currently no way to change the default.  You can do
something like this:

    class RootDirectory(Directory):
        def _q_traverse(self, path):
            get_response().set_charset('utf-8')
            return super(RootDirectory, self)._q_traverse(path)


We could add a DEFAULT_CHARSET attribute to HTTPResponse (see the
attached patch).  There is still a problem with set_content_type(),
I think.  set_content_type() is often used when serving up binary
data.  In that case I don't think you want to default charset to be
UTF-8, even though you probably want text/* responses to be UTF-8.
We could change set_content_type() to this:

    def set_content_type(self, content_type, charset=None):
        self.content_type = content_type
        if charset is None:
            if content_type.startswith('text/'):
                charset = self.DEFAULT_CHARSET
            else:
                charset = 'iso-8859-1'
        self.charset = charset

That seems pretty complicated.  Unfortunately, leaving the default
for set_content_type() be 'iso-8859-1' would also be surprising.  I
guess another option would be to set self.charset set None if no
charset was supplied to set_content_type().  In that case, the
charset parameter of the Content-Type header would be left out.

Not sure what to do.

  Neil


diff -rN -u quixote-old/http_response.py quixote-new/http_response.py
--- quixote-old/http_response.py        2005-01-26 15:49:37.000000000 -0700
+++ quixote-new/http_response.py        2005-01-28 23:35:55.000000000 -0700
@@ -133,12 +133,14 @@
         else's problem.
     """

-    def __init__(self, status=200, body=None, charset='iso-8859-1'):
+    DEFAULT_CHARSET = 'iso-8859-1'
+
+    def __init__(self, status=200, body=None, charset=None):
         """
         Creates a new HTTP response.
         """
         self.content_type = 'text/html'
-        self.charset = charset
+        self.charset = charset or self.DEFAULT_CHARSET
         self.set_status(status)
         self.headers = {}



reply