uilib.py
changeset 7913 d0c6a7993cec
parent 7879 9aae456abab5
child 7914 fb757a7d887e
equal deleted inserted replaced
7910:e5d5609e3bf1 7913:d0c6a7993cec
    77 def print_tzdatetime(value, req, props, displaytime=True):
    77 def print_tzdatetime(value, req, props, displaytime=True):
    78     if displaytime:
    78     if displaytime:
    79         return ustrftime(value, req.property_value('ui.datetime-format')) + u' UTC'
    79         return ustrftime(value, req.property_value('ui.datetime-format')) + u' UTC'
    80     return ustrftime(value, req.property_value('ui.date-format'))
    80     return ustrftime(value, req.property_value('ui.date-format'))
    81 
    81 
       
    82 _('%d years')
       
    83 _('%d months')
       
    84 _('%d weeks')
       
    85 _('%d days')
       
    86 _('%d hours')
       
    87 _('%d minutes')
       
    88 _('%d seconds')
       
    89 
       
    90 def print_timedelta(value, req, props, displaytime=True):
       
    91     if isinstance(value, (int, long)):
       
    92         # `date - date`, unlike `datetime - datetime` gives an int
       
    93         # (number of days), not a timedelta
       
    94         # XXX should rql be fixed to return Int instead of Interval in
       
    95         #     that case? that would be probably the proper fix but we
       
    96         #     loose information on the way...
       
    97         value = timedelta(days=value)
       
    98     if value.days > 730 or value.days < -730: # 2 years
       
    99         return req._('%d years') % (value.days // 365)
       
   100     elif value.days > 60 or value.days < -60: # 2 months
       
   101         return req._('%d months') % (value.days // 30)
       
   102     elif value.days > 14 or value.days < -14: # 2 weeks
       
   103         return req._('%d weeks') % (value.days // 7)
       
   104     elif value.days > 2 or value.days < -2:
       
   105         return req._('%d days') % int(value.days)
       
   106     else:
       
   107         minus = 1 if value.days > 0 else -1
       
   108         if value.seconds > 3600:
       
   109             return req._('%d hours') % (int(value.seconds // 3600) * minus)
       
   110         elif value.seconds >= 120:
       
   111             return req._('%d minutes') % (int(value.seconds // 60) * minus)
       
   112         else:
       
   113             return req._('%d seconds') % (int(value.seconds) * minus)
       
   114 
    82 def print_boolean(value, req, props, displaytime=True):
   115 def print_boolean(value, req, props, displaytime=True):
    83     if value:
   116     if value:
    84         return req._('yes')
   117         return req._('yes')
    85     return req._('no')
   118     return req._('no')
    86 
   119 
    96     'Datetime': print_datetime,
   129     'Datetime': print_datetime,
    97     'TZDatetime': print_tzdatetime,
   130     'TZDatetime': print_tzdatetime,
    98     'Boolean': print_boolean,
   131     'Boolean': print_boolean,
    99     'Float': print_float,
   132     'Float': print_float,
   100     'Decimal': print_float,
   133     'Decimal': print_float,
   101     # XXX Interval
   134     'Interval': print_timedelta,
   102     }
   135     }
   103 
   136 
   104 def printable_value(req, attrtype, value, props=None, displaytime=True):
   137 def printable_value(req, attrtype, value, props=None, displaytime=True):
   105     """return a displayable value (i.e. unicode string)"""
   138     """return a displayable value (i.e. unicode string)"""
   106     if value is None:
   139     if value is None: