[request, ui] printable_value is now a method of request, and may be given dict of formatters to use (closes #1984743)
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 06 Oct 2011 16:14:08 +0200
changeset 7914 fb757a7d887e
parent 7913 d0c6a7993cec
child 7918 d59db6068731
[request, ui] printable_value is now a method of request, and may be given dict of formatters to use (closes #1984743)
entity.py
req.py
uilib.py
--- a/entity.py	Thu Oct 06 16:14:08 2011 +0200
+++ b/entity.py	Thu Oct 06 16:14:08 2011 +0200
@@ -40,7 +40,7 @@
 from cubicweb.schema import RQLVocabularyConstraint, RQLConstraint
 from cubicweb.rqlrewrite import RQLRewriter
 
-from cubicweb.uilib import printable_value, soup2xhtml
+from cubicweb.uilib import soup2xhtml
 from cubicweb.mixins import MI_REL_TRIGGERS
 from cubicweb.mttransforms import ENGINE
 
@@ -619,8 +619,8 @@
                 return self._cw_mtc_transform(value.getvalue(), attrformat, format,
                                               encoding)
             return u''
-        value = printable_value(self._cw, attrtype, value, props,
-                                displaytime=displaytime)
+        value = self._cw.printable_value(attrtype, value, props,
+                                         displaytime=displaytime)
         if format == 'text/html':
             value = xml_escape(value)
         return value
--- a/req.py	Thu Oct 06 16:14:08 2011 +0200
+++ b/req.py	Thu Oct 06 16:14:08 2011 +0200
@@ -29,7 +29,7 @@
 from logilab.common.deprecation import deprecated
 from logilab.common.date import ustrftime, strptime, todate, todatetime
 
-from cubicweb import Unauthorized, NoSelectableObject, typed_eid
+from cubicweb import Unauthorized, NoSelectableObject, typed_eid, uilib
 from cubicweb.rset import ResultSet
 
 ONESECOND = timedelta(0, 1, 0)
@@ -343,6 +343,18 @@
                                                  rset=rset, **initargs)
         return view.render(w=w, **kwargs)
 
+    def printable_value(self, attrtype, value, props=None, displaytime=True,
+                        formatters=uilib.PRINTERS):
+        """return a displayablye value (i.e. unicode string)"""
+        if value is None:
+            return u''
+        try:
+            as_string = formatters[attrtype]
+        except KeyError:
+            self.error('given bad attrtype %s', attrtype)
+            return unicode(value)
+        return as_string(value, self, props, displaytime)
+
     def format_date(self, date, date_format=None, time=False):
         """return a string for a date time according to instance's
         configuration
--- a/uilib.py	Thu Oct 06 16:14:08 2011 +0200
+++ b/uilib.py	Thu Oct 06 16:14:08 2011 +0200
@@ -30,6 +30,7 @@
 
 from logilab.mtconverter import xml_escape, html_unescape
 from logilab.common.date import ustrftime
+from logilab.common.deprecation import deprecated
 
 from cubicweb.utils import JSString, json_dumps
 
@@ -134,15 +135,9 @@
     'Interval': print_timedelta,
     }
 
+@deprecated('[3.14] use req.printable_value(attrtype, value, ...)')
 def printable_value(req, attrtype, value, props=None, displaytime=True):
-    """return a displayable value (i.e. unicode string)"""
-    if value is None:
-        return u''
-    try:
-        printer = PRINTERS[attrtype]
-    except KeyError:
-        return unicode(value)
-    return printer(value, req, props, displaytime)
+    return req.printable_value(attrtype, value, props, displaytime)
 
 
 # text publishing #############################################################