web/__init__.py
branchstable
changeset 3741 3864c7dfddf0
parent 2934 cf4d39416fca
child 3758 d43c9709434d
equal deleted inserted replaced
3736:07196bda2456 3741:3864c7dfddf0
     8 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
     8 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
     9 """
     9 """
    10 __docformat__ = "restructuredtext en"
    10 __docformat__ = "restructuredtext en"
    11 _ = unicode
    11 _ = unicode
    12 
    12 
    13 from decimal import Decimal
       
    14 from datetime import datetime, date, timedelta
       
    15 from simplejson import dumps
    13 from simplejson import dumps
    16 
    14 
    17 from logilab.common.deprecation import deprecated
    15 from logilab.common.deprecation import deprecated
    18 
    16 
    19 from urllib import quote as urlquote
    17 from urllib import quote as urlquote
    20 from cubicweb.web._exceptions import *
    18 from cubicweb.web._exceptions import *
    21 
    19 from cubicweb.utils import CubicWebJsonEncoder
    22 
    20 
    23 INTERNAL_FIELD_VALUE = '__cubicweb_internal_field__'
    21 INTERNAL_FIELD_VALUE = '__cubicweb_internal_field__'
    24 
    22 
    25 
    23 
    26 class stdmsgs(object):
    24 class stdmsgs(object):
    45 
    43 
    46 # XXX deprecated
    44 # XXX deprecated
    47 FACETTES = set()
    45 FACETTES = set()
    48 
    46 
    49 
    47 
    50 
       
    51 def json_dumps(value):
    48 def json_dumps(value):
    52     if isinstance(value, Decimal):
    49     return dumps(value, cls=CubicWebJsonEncoder)
    53         value = float(value)
       
    54     elif isinstance(value, (date, datetime)):
       
    55         value = value.strftime('%Y-%m-%d %H:%M')
       
    56     elif isinstance(value, timedelta):
       
    57         value = (value.days * 24*60*60) + value.seconds
       
    58     try:
       
    59         return dumps(value)
       
    60     except TypeError:
       
    61         return dumps(repr(value))
       
    62 
    50 
    63 def jsonize(function):
    51 def jsonize(function):
    64     def newfunc(*args, **kwargs):
    52     def newfunc(*args, **kwargs):
    65         return json_dumps(function(*args, **kwargs))
    53         try:
       
    54             value = function(*args, **kwargs)
       
    55             return json_dumps(value)
       
    56         except TypeError:
       
    57             return json_dumps(repr(value))
    66     return newfunc
    58     return newfunc
    67 
    59 
    68 @deprecated('use req.build_ajax_replace_url() instead')
    60 @deprecated('use req.build_ajax_replace_url() instead')
    69 def ajax_replace_url(nodeid, rql, vid=None, swap=False, **extraparams):
    61 def ajax_replace_url(nodeid, rql, vid=None, swap=False, **extraparams):
    70     """builds a replacePageChunk-like url
    62     """builds a replacePageChunk-like url