web/__init__.py
branchtls-sprint
changeset 1645 374586978230
parent 1635 866563e2d0fc
child 1801 672acc730ce5
equal deleted inserted replaced
1644:45bec81a9e1c 1645:374586978230
     7 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     7 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     8 """
     8 """
     9 __docformat__ = "restructuredtext en"
     9 __docformat__ = "restructuredtext en"
    10 _ = unicode
    10 _ = unicode
    11 
    11 
       
    12 from decimal import Decimal
       
    13 from datetime import datetime, date, timedelta
    12 from simplejson import dumps
    14 from simplejson import dumps
    13 
    15 
       
    16 from cubicweb.common.uilib import urlquote
    14 from cubicweb.web._exceptions import *
    17 from cubicweb.web._exceptions import *
    15 
    18 
    16 
    19 
    17 INTERNAL_FIELD_VALUE = '__cubicweb_internal_field__'
    20 INTERNAL_FIELD_VALUE = '__cubicweb_internal_field__'
    18 
    21 
    41 FACETTES = set()
    44 FACETTES = set()
    42 
    45 
    43 
    46 
    44 
    47 
    45 def json_dumps(value):
    48 def json_dumps(value):
    46     if isinstance(value, decimal.Decimal):
    49     if isinstance(value, Decimal):
    47         value = float(value)
    50         value = float(value)
    48     elif isinstance(value, (date, datetime)):
    51     elif isinstance(value, (date, datetime)):
    49         value = value.strftime('%Y-%m-%d %H:%M')
    52         value = value.strftime('%Y-%m-%d %H:%M')
    50     elif isinstance(value, timedelta):
    53     elif isinstance(value, timedelta):
    51         value = (value.days * 24*60*60) + value.seconds
    54         value = (value.days * 24*60*60) + value.seconds
    52     try:
    55     try:
    53         return simplejson.dumps(value)
    56         return dumps(value)
    54     except TypeError:
    57     except TypeError:
    55         return simplejson.dumps(repr(value))
    58         return dumps(repr(value))
    56 
    59 
    57 def jsonize(function):
    60 def jsonize(function):
    58     def newfunc(*args, **kwargs):
    61     def newfunc(*args, **kwargs):
    59         return json_dumps(function(*args, **kwargs))
    62         return json_dumps(function(*args, **kwargs))
    60     return newfunc
    63     return newfunc