def test_safeuni(self): class HasUnicode(object): def __unicode__(self): return u"unicode here" uni = u"unicode" base = "basestring" hasUni = HasUnicode() noUni = 5 self.assertEqual(type(util.safeuni(uni)), unicode, "Did not return a unicode string from uni.") self.assertEqual(type(util.safeuni(base)), unicode, "Did not return a unicode string from base.") self.assertEqual(type(util.safeuni(hasUni)), unicode, "Did not return a unicode string from hasUni.") self.assertEqual(util.safeuni(hasUni), "unicode here", "Did not return the expected string from hasUni.") self.assertEqual(type(util.safeuni(noUni)), unicode, "Did not return a unicode string from noUni.")
def dict_to_instance(self, data, instance=None): if instance is None: Model = self.get_model() instance = Model() for (key, val) in data.iteritems(): if key.startswith('_'): continue # All non-ascii (ond special) characters should be escaped with char-ref try: if key in Model.__table__.c.keys() and str(Model.__table__.c.get(key).type).startswith('VARCHAR'): val = jinja2.escape(val).encode('ascii','xmlcharrefreplace') if len(val) > 0: val = safeuni(val) except Exception, e: log.debug("Exception encoding field %s: %s" % (key, e)) pass setattr(instance, key, val)
def dict_to_instance(self, data, instance=None): if instance is None: Model = self.get_model() instance = Model() for (key, val) in data.iteritems(): if key.startswith('_'): continue # All non-ascii (ond special) characters should be escaped with char-ref try: if key in Model.__table__.c.keys() and str( Model.__table__.c.get(key).type).startswith('VARCHAR'): val = jinja2.escape(val).encode('ascii', 'xmlcharrefreplace') if len(val) > 0: val = safeuni(val) except Exception, e: log.debug("Exception encoding field %s: %s" % (key, e)) pass setattr(instance, key, val)
def serialize_fallback(self, obj): """ Convert any unhandled object into a serializable representation. """ return safeuni(obj)
params = dict([part.split('=') for part in querystring.split('&')]) try: var = params[var] except KeyError: return None else: var = web.input()[var] if hasattr(web.input(), var) else None if isinstance(var, basestring): #var = util.strip_html(var) var = escape(var) var = var.strip() if len(var) == 0: return None var = util.safeuni(var) return var def render(self, template_name, template_values=None, suffix="html", content_type = "text/html", status="200 OK"): """ Custom renderer for Change by Us templates. @type template_name: string @param template_name: Name of template (without extension) @type template_values: dict @param template_values: Values to include in the template. @type suffix: string @param suffix: Extension of template file. @type content_type: string @param content_type: HTTP header content type to output.
params = dict([part.split('=') for part in querystring.split('&')]) try: var = params[var] except KeyError: return None else: var = web.input()[var] if hasattr(web.input(), var) else None if isinstance(var, basestring): #var = util.strip_html(var) var = escape(var) var = var.strip() if len(var) == 0: return None var = util.safeuni(var) return var def render(self, template_name, template_values=None, suffix="html", content_type="text/html", status="200 OK"): """ Custom renderer for Change by Us templates. @type template_name: string @param template_name: Name of template (without extension) @type template_values: dict