Commits

Gael Pasgrimaud committed 7da0629

add support for Model.__html__()

Comments (0)

Files changed (3)

formalchemy/fields.py

     else:
         return unicode(str(k), config.encoding)
 
+def _htmlify(k, null_value=u''):
+    if hasattr(k, '__html__'):
+        try:
+            return h.literal(k.__html__())
+        except TypeError:
+            # not callable. skipping
+            pass
+    return _stringify(k, null_value)
+
 NoDefault = object()
 
 def deserialize_once(func):
         if value is None:
             return ''
         if isinstance(value, list):
-            return u', '.join([_stringify(item) for item in value])
+            return h.literal(', ').join([self.stringify_value(item, as_html=True) for item in value])
         if isinstance(value, unicode):
             return value
-        return _stringify(value)
+        return self.stringify_value(value, as_html=True)
 
     @property
     def params(self):
             return datetime.datetime(dt.year, dt.month, dt.day, tm.hour, tm.minute, tm.second)
 
         return data
-    def stringify_value(self, v):
+
+    def stringify_value(self, v, as_html=False):
+        if as_html:
+            return _htmlify(v, null_value=self.field._null_option[1])
         return _stringify(v, null_value=self.field._null_option[1])
 
     def __repr__(self):

formalchemy/tests/__init__.py

         return self.name
     def __repr__(self):
         return '<User %s>' % self.name
+    def __html__(self):
+        return '<a href="mailto:%s">%s</a>' % (self.email, self.name)
 
 class NaturalOrder(Base):
     __tablename__ = 'natural_orders'

formalchemy/tests/test_fieldset.py

 
 >>> fs_or = FieldSet(order1)
 >>> print fs_or.user.render_readonly()
-Bill
+<a href="mailto:bill@example.com">Bill</a>
 
 >>> t = FieldSet(Manual)
 >>> t.configure(include=[t.a, t.b], readonly=True)