28 import re |
28 import re |
29 from StringIO import StringIO |
29 from StringIO import StringIO |
30 |
30 |
31 from logilab.mtconverter import xml_escape, html_unescape |
31 from logilab.mtconverter import xml_escape, html_unescape |
32 from logilab.common.date import ustrftime |
32 from logilab.common.date import ustrftime |
|
33 from logilab.common.deprecation import deprecated |
33 |
34 |
34 from cubicweb.utils import JSString, json_dumps |
35 from cubicweb.utils import JSString, json_dumps |
35 |
36 |
36 |
37 |
37 def rql_for_eid(eid): |
38 def rql_for_eid(eid): |
58 # don't translate empty value if you don't want strange results |
59 # 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 if props is not None and value and props.get('internationalizable'): |
60 return req._(value) |
61 return req._(value) |
61 return value |
62 return value |
62 |
63 |
|
64 def print_int(value, req, props, displaytime=True): |
|
65 return unicode(value) |
|
66 |
63 def print_date(value, req, props, displaytime=True): |
67 def print_date(value, req, props, displaytime=True): |
64 return ustrftime(value, req.property_value('ui.date-format')) |
68 return ustrftime(value, req.property_value('ui.date-format')) |
65 |
69 |
66 def print_time(value, req, props, displaytime=True): |
70 def print_time(value, req, props, displaytime=True): |
67 return ustrftime(value, req.property_value('ui.time-format')) |
71 return ustrftime(value, req.property_value('ui.time-format')) |
77 def print_tzdatetime(value, req, props, displaytime=True): |
81 def print_tzdatetime(value, req, props, displaytime=True): |
78 if displaytime: |
82 if displaytime: |
79 return ustrftime(value, req.property_value('ui.datetime-format')) + u' UTC' |
83 return ustrftime(value, req.property_value('ui.datetime-format')) + u' UTC' |
80 return ustrftime(value, req.property_value('ui.date-format')) |
84 return ustrftime(value, req.property_value('ui.date-format')) |
81 |
85 |
|
86 _('%d years') |
|
87 _('%d months') |
|
88 _('%d weeks') |
|
89 _('%d days') |
|
90 _('%d hours') |
|
91 _('%d minutes') |
|
92 _('%d seconds') |
|
93 |
|
94 def print_timedelta(value, req, props, displaytime=True): |
|
95 if isinstance(value, (int, long)): |
|
96 # `date - date`, unlike `datetime - datetime` gives an int |
|
97 # (number of days), not a timedelta |
|
98 # XXX should rql be fixed to return Int instead of Interval in |
|
99 # that case? that would be probably the proper fix but we |
|
100 # loose information on the way... |
|
101 value = timedelta(days=value) |
|
102 if value.days > 730 or value.days < -730: # 2 years |
|
103 return req._('%d years') % (value.days // 365) |
|
104 elif value.days > 60 or value.days < -60: # 2 months |
|
105 return req._('%d months') % (value.days // 30) |
|
106 elif value.days > 14 or value.days < -14: # 2 weeks |
|
107 return req._('%d weeks') % (value.days // 7) |
|
108 elif value.days > 2 or value.days < -2: |
|
109 return req._('%d days') % int(value.days) |
|
110 else: |
|
111 minus = 1 if value.days >= 0 else -1 |
|
112 if value.seconds > 3600: |
|
113 return req._('%d hours') % (int(value.seconds // 3600) * minus) |
|
114 elif value.seconds >= 120: |
|
115 return req._('%d minutes') % (int(value.seconds // 60) * minus) |
|
116 else: |
|
117 return req._('%d seconds') % (int(value.seconds) * minus) |
|
118 |
82 def print_boolean(value, req, props, displaytime=True): |
119 def print_boolean(value, req, props, displaytime=True): |
83 if value: |
120 if value: |
84 return req._('yes') |
121 return req._('yes') |
85 return req._('no') |
122 return req._('no') |
86 |
123 |
88 return unicode(req.property_value('ui.float-format') % value) |
125 return unicode(req.property_value('ui.float-format') % value) |
89 |
126 |
90 PRINTERS = { |
127 PRINTERS = { |
91 'Bytes': print_bytes, |
128 'Bytes': print_bytes, |
92 'String': print_string, |
129 'String': print_string, |
|
130 'Int': print_int, |
|
131 'BigInt': print_int, |
93 'Date': print_date, |
132 'Date': print_date, |
94 'Time': print_time, |
133 'Time': print_time, |
95 'TZTime': print_tztime, |
134 'TZTime': print_tztime, |
96 'Datetime': print_datetime, |
135 'Datetime': print_datetime, |
97 'TZDatetime': print_tzdatetime, |
136 'TZDatetime': print_tzdatetime, |
98 'Boolean': print_boolean, |
137 'Boolean': print_boolean, |
99 'Float': print_float, |
138 'Float': print_float, |
100 'Decimal': print_float, |
139 'Decimal': print_float, |
101 # XXX Interval |
140 'Interval': print_timedelta, |
102 } |
141 } |
103 |
142 |
|
143 @deprecated('[3.14] use req.printable_value(attrtype, value, ...)') |
104 def printable_value(req, attrtype, value, props=None, displaytime=True): |
144 def printable_value(req, attrtype, value, props=None, displaytime=True): |
105 """return a displayable value (i.e. unicode string)""" |
145 return req.printable_value(attrtype, value, props, displaytime) |
106 if value is None: |
146 |
107 return u'' |
147 def css_em_num_value(vreg, propname, default): |
108 try: |
148 """ we try to read an 'em' css property |
109 printer = PRINTERS[attrtype] |
149 if we get another unit we're out of luck and resort to the given default |
110 except KeyError: |
150 (hence, it is strongly advised not to specify but ems for this css prop) |
111 return unicode(value) |
151 """ |
112 return printer(value, req, props, displaytime) |
152 propvalue = vreg.config.uiprops[propname].lower().strip() |
113 |
153 if propvalue.endswith('em'): |
|
154 try: |
|
155 return float(propvalue[:-2]) |
|
156 except Exception: |
|
157 vreg.warning('css property %s looks malformed (%r)', |
|
158 propname, propvalue) |
|
159 else: |
|
160 vreg.warning('css property %s should use em (currently is %r)', |
|
161 propname, propvalue) |
|
162 return default |
114 |
163 |
115 # text publishing ############################################################# |
164 # text publishing ############################################################# |
116 |
165 |
117 try: |
166 try: |
118 from cubicweb.ext.rest import rest_publish # pylint: disable=W0611 |
167 from cubicweb.ext.rest import rest_publish # pylint: disable=W0611 |