[EMAIL PROTECTED] ha scritto:
Author: rhymes
Date: Tue Jan  9 23:04:27 2007
New Revision: 253

Modified:
   code/pythonisti/trunk/pythonisti/geo/google.py
   code/pythonisti/trunk/pythonisti/geo/urls.py
   code/pythonisti/trunk/pythonisti/geo/views.py
Log:
parsing the charset directly from the HTTP header

Modified: code/pythonisti/trunk/pythonisti/geo/google.py
==============================================================================
--- code/pythonisti/trunk/pythonisti/geo/google.py      (original)
+++ code/pythonisti/trunk/pythonisti/geo/google.py      Tue Jan  9 23:04:27 2007
@@ -12,14 +12,20 @@
from django.conf import settings -
+import re
class AddressNotFound(Exception):
     pass
class MultipleAddress(Exception):
     pass
-
+ +def _get_charset(header):
+    charset_pattern = re.compile(r"charset=(.*)")
+    matches = charset_pattern.search(header)
+    if matches:
+        return matches.groups(0)[0].split(";")[0] # take the first charset only
+    return None
def get_page(url):
     """Retrieve the content of the HTTP url, as an Unicode string.
@@ -29,10 +35,9 @@
fp = urlopen(url) - content_type = fp.info()['content-type']
-    # TODO parse the charset
-    charset = 'utf-8'
-    print content_type
+    info = fp.info()
+    content_type = info['content-type']
+    charset = _get_charset(content_type) or "utf-8"
return fp.read().decode(charset)

Ok.


Modified: code/pythonisti/trunk/pythonisti/geo/urls.py
==============================================================================
--- code/pythonisti/trunk/pythonisti/geo/urls.py        (original)
+++ code/pythonisti/trunk/pythonisti/geo/urls.py        Tue Jan  9 23:04:27 2007
@@ -4,6 +4,6 @@
urlpatterns = patterns('',
-    (r'^db/', views.db_view),
-    (r'^map/', views.map_view)
+    (r'^json/', views.json),
+    (r'^map/', views.map)
 )

Modified: code/pythonisti/trunk/pythonisti/geo/views.py
==============================================================================
--- code/pythonisti/trunk/pythonisti/geo/views.py       (original)
+++ code/pythonisti/trunk/pythonisti/geo/views.py       Tue Jan  9 23:04:27 2007
@@ -1,37 +1,30 @@
-# Create your views here.
 from django.template import Context, loader
 from django.http import HttpResponse
 from django.shortcuts import render_to_response
-from django.utils.simplejson import dumps, loads
+from django.utils import simplejson
 from django.conf import settings
from pythonisti.geo import models -
-
-def db_view(request):
+def json(request):
     locations = models.GeoLocation.objects.all()
-    content = []
+ content = []
     for item in locations:
         content.append({
                 'info': item.username.username,
-                'location': loads(item.geolocation)
-                })
- - t = loader.get_template('geo/db.js')
-    c = Context({
-        'locations': dumps(content)
+                'location': simplejson.loads(item.geolocation)
         })
- return HttpResponse(t.render(c), mimetype='text/javascript')
+    json_locations = simplejson.dumps(content)
+    return HttpResponse(json_locations, mimetype='application/json')
-def map_view(request):
+def map(request):
     context = {
         'key': settings.GOOGLE_API_KEY,
         'center': settings.MAP_CENTER,
         'zoom': settings.MAP_ZOOM
-        }
+    }
return render_to_response('geo/map.xhtml', context)


Questo non andava fatto.
Queste modifiche andavano fatte in un altro commit.

Inoltre non รจ stato modificato il template della pagine XHTML (che usa il vecchio db.js), quindi temo che questo codice non funzioni.



Saluti  Manlio Perillo
_______________________________________________
Python mailing list
Python@lists.python.it
http://lists.python.it/mailman/listinfo/python

Rispondere a