uilib.py
changeset 7661 e157174c595c
parent 7575 335f14e8e5a7
child 7662 3217298823c1
equal deleted inserted replaced
7660:a1506b5306cc 7661:e157174c595c
    49 def eid_param(name, eid):
    49 def eid_param(name, eid):
    50     assert name is not None
    50     assert name is not None
    51     assert eid is not None
    51     assert eid is not None
    52     return '%s:%s' % (name, eid)
    52     return '%s:%s' % (name, eid)
    53 
    53 
       
    54 def print_bytes(value, req, props, displaytime=True):
       
    55     return u''
       
    56 
       
    57 def print_string(value, req, props, displaytime=True):
       
    58     # don't translate empty value if you don't want strange results
       
    59     if props is not None and value and props.get('internationalizable'):
       
    60         return req._(value)
       
    61     return value
       
    62 
       
    63 def print_date(value, req, props, displaytime=True):
       
    64     return ustrftime(value, req.property_value('ui.date-format'))
       
    65 
       
    66 def print_time(value, req, props, displaytime=True):
       
    67     return ustrftime(value, req.property_value('ui.time-format'))
       
    68 
       
    69 def print_tztime(value, req, props, displaytime=True):
       
    70     return ustrftime(value, req.property_value('ui.time-format')) + u' UTC'
       
    71 
       
    72 def print_datetime(value, req, props, displaytime=True):
       
    73     if displaytime:
       
    74         return ustrftime(value, req.property_value('ui.datetime-format'))
       
    75     return ustrftime(value, req.property_value('ui.date-format'))
       
    76 
       
    77 def print_tzdatetime(value, req, props, displaytime=True):
       
    78     if displaytime:
       
    79         return ustrftime(value, req.property_value('ui.datetime-format')) + u' UTC'
       
    80     return ustrftime(value, req.property_value('ui.date-format'))
       
    81 
       
    82 def print_boolean(value, req, props, displaytime=True):
       
    83     if value:
       
    84         return req._('yes')
       
    85     return req._('no')
       
    86 
       
    87 def print_float(value, req, props, displaytime=True):
       
    88     return unicode(req.property_value('ui.float-format') % value)
       
    89 
       
    90 PRINTERS = {
       
    91     'Bytes': print_bytes,
       
    92     'String': print_string,
       
    93     'Date': print_date,
       
    94     'Time': print_time,
       
    95     'TZTime': print_tztime,
       
    96     'Datetime': print_datetime,
       
    97     'TZDatetime': print_tzdatetime,
       
    98     'Boolean': print_boolean,
       
    99     'Float': print_float,
       
   100     'Decimal': print_float,
       
   101     # XXX Interval
       
   102     }
       
   103 
    54 def printable_value(req, attrtype, value, props=None, displaytime=True):
   104 def printable_value(req, attrtype, value, props=None, displaytime=True):
    55     """return a displayable value (i.e. unicode string)"""
   105     """return a displayable value (i.e. unicode string)"""
    56     if value is None or attrtype == 'Bytes':
   106     if value is None:
    57         return u''
   107         return u''
    58     if attrtype == 'String':
   108     try:
    59         # don't translate empty value if you don't want strange results
   109         printer = PRINTERS[attrtype]
    60         if props is not None and value and props.get('internationalizable'):
   110     except KeyError:
    61             return req._(value)
   111         return unicode(value)
    62         return value
   112     return printer(req, value, props, displaytime)
    63     if attrtype == 'Date':
       
    64         return ustrftime(value, req.property_value('ui.date-format'))
       
    65     if attrtype == 'Time':
       
    66         return ustrftime(value, req.property_value('ui.time-format'))
       
    67     if attrtype == 'TZTime':
       
    68         return ustrftime(value, req.property_value('ui.time-format')) + u' UTC'
       
    69     if attrtype == 'Datetime':
       
    70         if displaytime:
       
    71             return ustrftime(value, req.property_value('ui.datetime-format'))
       
    72         return ustrftime(value, req.property_value('ui.date-format'))
       
    73     if attrtype == 'TZDatetime':
       
    74         if displaytime:
       
    75             return ustrftime(value, req.property_value('ui.datetime-format')) + u' UTC'
       
    76         return ustrftime(value, req.property_value('ui.date-format'))
       
    77     if attrtype == 'Boolean':
       
    78         if value:
       
    79             return req._('yes')
       
    80         return req._('no')
       
    81     if attrtype in ('Float', 'Decimal'):
       
    82         value = req.property_value('ui.float-format') % value
       
    83     # XXX Interval
       
    84     return unicode(value)
       
    85 
   113 
    86 
   114 
    87 # text publishing #############################################################
   115 # text publishing #############################################################
    88 
   116 
    89 try:
   117 try: