appobject.py
branchtls-sprint
changeset 1016 26387b836099
parent 875 dd83bd56deae
child 1129 c0603e04727d
equal deleted inserted replaced
1014:4792a1bb72a9 1016:26387b836099
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
     6 """
     6 """
     7 __docformat__ = "restructuredtext en"
     7 __docformat__ = "restructuredtext en"
     8 
     8 
     9 from warnings import warn
     9 from warnings import warn
    10 
    10 from datetime import datetime, timedelta
    11 from mx.DateTime import now, oneSecond
    11 
    12 from simplejson import dumps
    12 from simplejson import dumps
    13 
    13 
    14 from logilab.common.decorators import classproperty
    14 from logilab.common.decorators import classproperty
    15 from logilab.common.deprecation import obsolete
    15 from logilab.common.deprecation import obsolete
    16 
    16 
    20 from cubicweb import Unauthorized
    20 from cubicweb import Unauthorized
    21 from cubicweb.vregistry import VObject, AndSelector
    21 from cubicweb.vregistry import VObject, AndSelector
    22 from cubicweb.selectors import yes
    22 from cubicweb.selectors import yes
    23 from cubicweb.utils import UStringIO, ustrftime
    23 from cubicweb.utils import UStringIO, ustrftime
    24 
    24 
       
    25 ONESECOND = timedelta(0, 1, 0)
    25 
    26 
    26 class Cache(dict):    
    27 class Cache(dict):    
    27     def __init__(self):
    28     def __init__(self):
    28         super(Cache, self).__init__()
    29         super(Cache, self).__init__()
    29         self.cache_creation_date = None
    30         self.cache_creation_date = None
    30         self.latest_cache_lookup = now()
    31         self.latest_cache_lookup = datetime.now()
    31     
    32     
    32 CACHE_REGISTRY = {}
    33 CACHE_REGISTRY = {}
    33 
    34 
    34 class AppRsetObject(VObject):
    35 class AppRsetObject(VObject):
    35     """This is the base class for CubicWeb application objects
    36     """This is the base class for CubicWeb application objects
   134         if cachename in CACHE_REGISTRY:
   135         if cachename in CACHE_REGISTRY:
   135             cache = CACHE_REGISTRY[cachename]
   136             cache = CACHE_REGISTRY[cachename]
   136         else:
   137         else:
   137             cache = Cache()
   138             cache = Cache()
   138             CACHE_REGISTRY[cachename] = cache
   139             CACHE_REGISTRY[cachename] = cache
   139         _now = now()
   140         _now = datetime.now()
   140         if _now > cache.latest_cache_lookup + oneSecond:
   141         if _now > cache.latest_cache_lookup + ONESECOND:
   141             ecache = self.req.execute('Any C,T WHERE C is ECache, C name %(name)s, C timestamp T', 
   142             ecache = self.req.execute('Any C,T WHERE C is ECache, C name %(name)s, C timestamp T', 
   142                                       {'name':cachename}).get_entity(0,0)
   143                                       {'name':cachename}).get_entity(0,0)
   143             cache.latest_cache_lookup = _now
   144             cache.latest_cache_lookup = _now
   144             if not ecache.valid(cache.cache_creation_date):
   145             if not ecache.valid(cache.cache_creation_date):
   145                 cache.empty()
   146                 cache.empty()
   279         output = UStringIO()
   280         output = UStringIO()
   280         template.expand(context, output)
   281         template.expand(context, output)
   281         return output.getvalue()
   282         return output.getvalue()
   282 
   283 
   283     def format_date(self, date, date_format=None, time=False):
   284     def format_date(self, date, date_format=None, time=False):
   284         """return a string for a mx date time according to application's
   285         """return a string for a date time according to application's
   285         configuration
   286         configuration
   286         """
   287         """
   287         if date:
   288         if date:
   288             if date_format is None:
   289             if date_format is None:
   289                 if time:
   290                 if time:
   292                     date_format = self.req.property_value('ui.date-format')
   293                     date_format = self.req.property_value('ui.date-format')
   293             return ustrftime(date, date_format)
   294             return ustrftime(date, date_format)
   294         return u''
   295         return u''
   295 
   296 
   296     def format_time(self, time):
   297     def format_time(self, time):
   297         """return a string for a mx date time according to application's
   298         """return a string for a time according to application's
   298         configuration
   299         configuration
   299         """
   300         """
   300         if time:
   301         if time:
   301             return ustrftime(time, self.req.property_value('ui.time-format'))
   302             return ustrftime(time, self.req.property_value('ui.time-format'))
   302         return u''
   303         return u''