utils.py
branchstable
changeset 3371 762a7bc3b73d
parent 3364 039d1694f36d
child 3372 26b89dfe4170
equal deleted inserted replaced
3370:62ed9981a770 3371:762a7bc3b73d
    16 from md5 import md5
    16 from md5 import md5
    17 from datetime import datetime, timedelta, date
    17 from datetime import datetime, timedelta, date
    18 from time import time, mktime
    18 from time import time, mktime
    19 from random import randint, seed
    19 from random import randint, seed
    20 from calendar import monthrange
    20 from calendar import monthrange
    21 
       
    22 import simplejson
       
    23 
    21 
    24 # initialize random seed from current time
    22 # initialize random seed from current time
    25 seed()
    23 seed()
    26 try:
    24 try:
    27     strptime = datetime.strptime
    25     strptime = datetime.strptime
   268         resources declaration
   266         resources declaration
   269         """
   267         """
   270         w = self.write
   268         w = self.write
   271         # 1/ variable declaration if any
   269         # 1/ variable declaration if any
   272         if self.jsvars:
   270         if self.jsvars:
   273             from simplejson import dumps
       
   274             w(u'<script type="text/javascript"><!--//--><![CDATA[//><!--\n')
   271             w(u'<script type="text/javascript"><!--//--><![CDATA[//><!--\n')
   275             for var, value in self.jsvars:
   272             for var, value in self.jsvars:
   276                 w(u'%s = %s;\n' % (var, dumps(value)))
   273                 w(u'%s = %s;\n' % (var, dumps(value)))
   277             w(u'//--><!]]></script>\n')
   274             w(u'//--><!]]></script>\n')
   278         # 2/ css files
   275         # 2/ css files
   361         __answer[0] = False
   358         __answer[0] = False
   362         return False
   359         return False
   363     __answer[0] = True
   360     __answer[0] = True
   364     return True
   361     return True
   365 
   362 
   366 
   363 try:
   367 class CubicWebJsonEncoder(simplejson.JSONEncoder):
   364     # may not be there if cubicwbeb-web not installed
   368     """define a simplejson encoder to be able to encode yams std types"""
   365     from simplejson import dumps, JSONEncoder
   369     def default(self, obj):
   366 except ImportError:
   370         if isinstance(obj, pydatetime.datetime):
   367     pass
   371             return obj.strftime('%Y/%m/%d %H:%M:%S')
   368 else:
   372         elif isinstance(obj, pydatetime.date):
   369 
   373             return obj.strftime('%Y/%m/%d')
   370     class CubicWebJsonEncoder(JSONEncoder):
   374         elif isinstance(obj, pydatetime.time):
   371         """define a simplejson encoder to be able to encode yams std types"""
   375             return obj.strftime('%H:%M:%S')
   372         def default(self, obj):
   376         elif isinstance(obj, decimal.Decimal):
   373             if isinstance(obj, pydatetime.datetime):
   377             return float(obj)
   374                 return obj.strftime('%Y/%m/%d %H:%M:%S')
   378         try:
   375             elif isinstance(obj, pydatetime.date):
   379             return simplejson.JSONEncoder.default(self, obj)
   376                 return obj.strftime('%Y/%m/%d')
   380         except TypeError:
   377             elif isinstance(obj, pydatetime.time):
   381             # we never ever want to fail because of an unknown type,
   378                 return obj.strftime('%H:%M:%S')
   382             # just return None in those cases.
   379             elif isinstance(obj, decimal.Decimal):
   383             return None
   380                 return float(obj)
       
   381             try:
       
   382                 return JSONEncoder.default(self, obj)
       
   383             except TypeError:
       
   384                 # we never ever want to fail because of an unknown type,
       
   385                 # just return None in those cases.
       
   386                 return None