Permalink
Browse files

Merge branch 'feature/json-unescaping' into develop

  • Loading branch information...
2 parents 5e5beff + 74ed32f commit 82e20d14fe6112121a861525a3addd96d9159718 Matt Wright committed Apr 4, 2012
Showing with 47 additions and 9 deletions.
  1. +3 −2 controllers/search.py
  2. +2 −2 framework/controller.py
  3. +36 −1 framework/util.py
  4. +6 −4 giveaminute/project.py
View
5 controllers/search.py
@@ -9,6 +9,7 @@
import giveaminute.idea as mIdea
import giveaminute.projectResource as mProjectResource
from framework.controller import *
+from framework.util import EscapingJSONEncoder
import json
class Search(Controller):
@@ -98,9 +99,9 @@ def searchProjectsJSON(self):
limit = int(self.request('n')) if self.request('n') else 6
offset = int(self.request('offset')) if self.request('offset') else 0
locationId = self.request('location_id')
-
+
return self.json({'results':mProject.searchProjects(self.db, terms, locationId, limit, offset),
- 'total_count':100})
+ 'total_count':100}, encoder=EscapingJSONEncoder)
def searchProjectResourcesJSON(self):
terms = self.request('terms').split(',') if self.request('terms') else []
View
4 framework/controller.py
@@ -349,8 +349,8 @@ def get_gettext_translation(self, locale_id):
# default text if not found.
return gettext.translation('messages', locale_dir, [locale_id], fallback=True)
- def json(self, data):
- output = json.dumps(data)
+ def json(self, data, encoder=None):
+ output = json.dumps(data, cls=encoder)
web.header("Content-Type", "text/plain")
log.info("200: text/plain (JSON)")
View
37 framework/util.py
@@ -4,9 +4,44 @@
"""
# generally try not to import things up here
-import re, base64, string, urlparse, datetime
+import re, base64, string, urlparse, datetime, json, jinja2
from framework.log import log
+_TO_UNICODE_TYPES = (unicode, type(None))
+def to_unicode(value):
+ """Converts a string argument to a unicode string.
+
+ If the argument is already a unicode string or None, it is returned
+ unchanged. Otherwise it must be a byte string and is decoded as utf8.
+ """
+ if isinstance(value, _TO_UNICODE_TYPES):
+ return value
+ assert isinstance(value, bytes)
+ return value.decode("utf-8")
+
+def recursive_unicode(obj):
+ """Walks a simple data structure, converting byte strings to unicode.
+
+ Supports lists, tuples, and dictionaries.
+ """
+ if isinstance(obj, dict):
+ return dict((recursive_unicode(k), recursive_unicode(v)) for (k,v) in obj.iteritems())
+ elif isinstance(obj, list):
+ return list(recursive_unicode(i) for i in obj)
+ elif isinstance(obj, tuple):
+ return tuple(recursive_unicode(i) for i in obj)
+ elif isinstance(obj, bytes):
+ return to_unicode(obj)
+ else:
+ if isinstance(obj, unicode) or isinstance(obj, basestring):
+ obj = jinja2.Markup(obj).unescape()
+ return obj
+
+class EscapingJSONEncoder(json.JSONEncoder):
+ def encode(self, obj):
+ return json.JSONEncoder.encode(self, recursive_unicode(obj))
+
+
def try_f(f, data, default=None):
"""
Try to convert data to a new type using the f function. If the conversion
View
10 giveaminute/project.py
@@ -340,8 +340,8 @@ def userNameDisplay(first, last, affiliation = None, isFullLast = False):
name = "%s, %s" % (name, affiliation)
else:
name = affiliation
-
- return jinja2.Markup(name).unescape()
+ return name
+ #return jinja2.Markup(name).unescape()
def smallIdea(ideaId, description, firstName, lastName, submissionType):
return dict(idea_id = ideaId,
@@ -1009,8 +1009,10 @@ def searchProjects(db, terms, locationId, limit=1000, offset=0):
for item in data:
betterData.append(dict(project_id = item.project_id,
- title = jinja2.Markup(item.title).unescape(),
- description = jinja2.Markup(item.description).unescape(),
+ #title = jinja2.Markup(item.title).unescape(),
+ title = item.title,
+ #description = jinja2.Markup(item.description).unescape(),
+ description = item.description,
image_id = item.image_id,
location_id = item.location_id,
owner = smallUserDisplay(item.owner_user_id,

0 comments on commit 82e20d1

Please sign in to comment.