common/uilib.py
branchtls-sprint
changeset 1635 866563e2d0fc
parent 1623 0b974e21d762
child 1646 88182252fd11
equal deleted inserted replaced
1634:dbb70cdca535 1635:866563e2d0fc
    12 import csv
    12 import csv
    13 import decimal
    13 import decimal
    14 import re
    14 import re
    15 from datetime import datetime, date, timedelta
    15 from datetime import datetime, date, timedelta
    16 from urllib import quote as urlquote
    16 from urllib import quote as urlquote
    17 from cStringIO import StringIO
    17 from StringIO import StringIO
    18 
    18 
    19 from logilab.mtconverter import html_escape, html_unescape
    19 from logilab.mtconverter import html_escape, html_unescape
    20 
    20 
    21 from cubicweb.utils import ustrftime
    21 from cubicweb.utils import ustrftime
    22 
    22 
   237 
   237 
   238 def toggle_link(nodeid, label):
   238 def toggle_link(nodeid, label):
   239     """builds a HTML link that uses the js toggleVisibility function"""
   239     """builds a HTML link that uses the js toggleVisibility function"""
   240     return u'<a href="%s">%s</a>' % (toggle_action(nodeid), label)
   240     return u'<a href="%s">%s</a>' % (toggle_action(nodeid), label)
   241 
   241 
   242 def ajax_replace_url(nodeid, rql, vid=None, swap=False, **extraparams):
       
   243     """builds a replacePageChunk-like url
       
   244     >>> ajax_replace_url('foo', 'Person P')
       
   245     "javascript: replacePageChunk('foo', 'Person%20P');"
       
   246     >>> ajax_replace_url('foo', 'Person P', 'oneline')
       
   247     "javascript: replacePageChunk('foo', 'Person%20P', 'oneline');"
       
   248     >>> ajax_replace_url('foo', 'Person P', 'oneline', name='bar', age=12)
       
   249     "javascript: replacePageChunk('foo', 'Person%20P', 'oneline', {'age':12, 'name':'bar'});"
       
   250     >>> ajax_replace_url('foo', 'Person P', name='bar', age=12)
       
   251     "javascript: replacePageChunk('foo', 'Person%20P', 'null', {'age':12, 'name':'bar'});"
       
   252     """
       
   253     params = [repr(nodeid), repr(urlquote(rql))]
       
   254     if extraparams and not vid:
       
   255         params.append("'null'")
       
   256     elif vid:
       
   257         params.append(repr(vid))
       
   258     if extraparams:
       
   259         import simplejson
       
   260         params.append(simplejson.dumps(extraparams))
       
   261     if swap:
       
   262         params.append('true')
       
   263     return "javascript: replacePageChunk(%s);" % ', '.join(params)
       
   264 
       
   265 
       
   266 from StringIO import StringIO
       
   267 
   242 
   268 def ureport_as_html(layout):
   243 def ureport_as_html(layout):
   269     from logilab.common.ureports import HTMLWriter
   244     from logilab.common.ureports import HTMLWriter
   270     formater = HTMLWriter(True)
   245     formater = HTMLWriter(True)
   271     stream = StringIO() #UStringIO() don't want unicode assertion
   246     stream = StringIO() #UStringIO() don't want unicode assertion
   505                 return ret[:self.maxsize]
   480                 return ret[:self.maxsize]
   506             return ret
   481             return ret
   507         return newfunc
   482         return newfunc
   508 
   483 
   509 
   484 
   510 def jsonize(function):
       
   511     import simplejson
       
   512     def newfunc(*args, **kwargs):
       
   513         ret = function(*args, **kwargs)
       
   514         if isinstance(ret, decimal.Decimal):
       
   515             ret = float(ret)
       
   516         elif isinstance(ret, (date, datetime)):
       
   517             ret = ret.strftime('%Y-%m-%d %H:%M')
       
   518         elif isinstance(ret, timedelta):
       
   519             ret = (ret.days * 24*60*60) + ret.seconds
       
   520         try:
       
   521             return simplejson.dumps(ret)
       
   522         except TypeError:
       
   523             return simplejson.dumps(repr(ret))
       
   524     return newfunc
       
   525 
       
   526 
       
   527 def htmlescape(function):
   485 def htmlescape(function):
   528     def newfunc(*args, **kwargs):
   486     def newfunc(*args, **kwargs):
   529         ret = function(*args, **kwargs)
   487         ret = function(*args, **kwargs)
   530         assert isinstance(ret, basestring)
   488         assert isinstance(ret, basestring)
   531         return html_escape(ret)
   489         return html_escape(ret)