author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Tue, 22 Sep 2009 09:50:45 +0200 | |
branch | stable |
changeset 3362 | 2a2dcfb379a0 |
parent 3316 | c4c07aab1c39 |
child 3364 | 039d1694f36d |
permissions | -rw-r--r-- |
0 | 1 |
"""Some utilities for CubicWeb server/clients. |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1751
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1751
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
3091
c3d73cecb29e
[html-head] escape js and css urls
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2678
diff
changeset
|
10 |
from logilab.mtconverter import xml_escape |
c3d73cecb29e
[html-head] escape js and css urls
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2678
diff
changeset
|
11 |
|
709 | 12 |
import locale |
3316 | 13 |
import sys |
14 |
import decimal |
|
15 |
import datetime as pydatetime |
|
0 | 16 |
from md5 import md5 |
1024
bb96289257bf
remove unused working_hours function, new todate function since we can't compare date and datetime objects
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
17 |
from datetime import datetime, timedelta, date |
2009
b1e2b9e4c809
[utils] move datetime2ticks from cw.web.views.plots to cw.utils
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
18 |
from time import time, mktime |
0 | 19 |
from random import randint, seed |
1625
744f0cc8758f
restore monthrange import
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
1616
diff
changeset
|
20 |
from calendar import monthrange |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
21 |
|
3231
3ee43e2f8560
[utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3101
diff
changeset
|
22 |
import simplejson |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
23 |
|
1024
bb96289257bf
remove unused working_hours function, new todate function since we can't compare date and datetime objects
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
24 |
# initialize random seed from current time |
bb96289257bf
remove unused working_hours function, new todate function since we can't compare date and datetime objects
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
25 |
seed() |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
26 |
try: |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
27 |
strptime = datetime.strptime |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
28 |
except AttributeError: # py < 2.5 |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
29 |
from time import strptime as time_strptime |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
30 |
def strptime(value, format): |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
31 |
return datetime(*time_strptime(value, format)[:6]) |
0 | 32 |
|
1024
bb96289257bf
remove unused working_hours function, new todate function since we can't compare date and datetime objects
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
33 |
def todate(somedate): |
bb96289257bf
remove unused working_hours function, new todate function since we can't compare date and datetime objects
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
34 |
"""return a date from a date (leaving unchanged) or a datetime""" |
bb96289257bf
remove unused working_hours function, new todate function since we can't compare date and datetime objects
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
35 |
if isinstance(somedate, datetime): |
bb96289257bf
remove unused working_hours function, new todate function since we can't compare date and datetime objects
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
36 |
return date(somedate.year, somedate.month, somedate.day) |
1603 | 37 |
assert isinstance(somedate, date), repr(somedate) |
1621
6260508f7d71
todatetime impl., fix todate impl.
sylvain.thenault@logilab.fr
parents:
1616
diff
changeset
|
38 |
return somedate |
6260508f7d71
todatetime impl., fix todate impl.
sylvain.thenault@logilab.fr
parents:
1616
diff
changeset
|
39 |
|
6260508f7d71
todatetime impl., fix todate impl.
sylvain.thenault@logilab.fr
parents:
1616
diff
changeset
|
40 |
def todatetime(somedate): |
6260508f7d71
todatetime impl., fix todate impl.
sylvain.thenault@logilab.fr
parents:
1616
diff
changeset
|
41 |
"""return a date from a date (leaving unchanged) or a datetime""" |
1751 | 42 |
# take care, datetime is a subclass of date |
43 |
if isinstance(somedate, datetime): |
|
44 |
return somedate |
|
45 |
assert isinstance(somedate, date), repr(somedate) |
|
46 |
return datetime(somedate.year, somedate.month, somedate.day) |
|
1024
bb96289257bf
remove unused working_hours function, new todate function since we can't compare date and datetime objects
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
47 |
|
2009
b1e2b9e4c809
[utils] move datetime2ticks from cw.web.views.plots to cw.utils
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
48 |
def datetime2ticks(date): |
b1e2b9e4c809
[utils] move datetime2ticks from cw.web.views.plots to cw.utils
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
49 |
return mktime(date.timetuple()) * 1000 |
b1e2b9e4c809
[utils] move datetime2ticks from cw.web.views.plots to cw.utils
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
50 |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
51 |
ONEDAY = timedelta(days=1) |
1697
5dae28906769
next_month / previous_month now support a new arg to give number of month
sylvain.thenault@logilab.fr
parents:
1626
diff
changeset
|
52 |
ONEWEEK = timedelta(days=7) |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
53 |
|
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
54 |
def days_in_month(date_): |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
55 |
return monthrange(date_.year, date_.month)[1] |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
56 |
|
1697
5dae28906769
next_month / previous_month now support a new arg to give number of month
sylvain.thenault@logilab.fr
parents:
1626
diff
changeset
|
57 |
def previous_month(date_, nbmonth=1): |
5dae28906769
next_month / previous_month now support a new arg to give number of month
sylvain.thenault@logilab.fr
parents:
1626
diff
changeset
|
58 |
while nbmonth: |
5dae28906769
next_month / previous_month now support a new arg to give number of month
sylvain.thenault@logilab.fr
parents:
1626
diff
changeset
|
59 |
date_ = first_day(date_) - ONEDAY |
5dae28906769
next_month / previous_month now support a new arg to give number of month
sylvain.thenault@logilab.fr
parents:
1626
diff
changeset
|
60 |
nbmonth -= 1 |
5dae28906769
next_month / previous_month now support a new arg to give number of month
sylvain.thenault@logilab.fr
parents:
1626
diff
changeset
|
61 |
return date_ |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
62 |
|
1697
5dae28906769
next_month / previous_month now support a new arg to give number of month
sylvain.thenault@logilab.fr
parents:
1626
diff
changeset
|
63 |
def next_month(date_, nbmonth=1): |
5dae28906769
next_month / previous_month now support a new arg to give number of month
sylvain.thenault@logilab.fr
parents:
1626
diff
changeset
|
64 |
while nbmonth: |
5dae28906769
next_month / previous_month now support a new arg to give number of month
sylvain.thenault@logilab.fr
parents:
1626
diff
changeset
|
65 |
date_ = last_day(date_) + ONEDAY |
5dae28906769
next_month / previous_month now support a new arg to give number of month
sylvain.thenault@logilab.fr
parents:
1626
diff
changeset
|
66 |
nbmonth -= 1 |
5dae28906769
next_month / previous_month now support a new arg to give number of month
sylvain.thenault@logilab.fr
parents:
1626
diff
changeset
|
67 |
return date_ |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
68 |
|
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
69 |
def first_day(date_): |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
70 |
return date(date_.year, date_.month, 1) |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
71 |
|
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
72 |
def last_day(date_): |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
73 |
return date(date_.year, date_.month, days_in_month(date_)) |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
74 |
|
1705
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
75 |
def date_range(begin, end, incday=None, incmonth=None): |
0 | 76 |
"""yields each date between begin and end |
77 |
:param begin: the start date |
|
78 |
:param end: the end date |
|
79 |
:param incr: the step to use to iterate over dates. Default is |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
80 |
one day. |
0 | 81 |
:param include: None (means no exclusion) or a function taking a |
82 |
date as parameter, and returning True if the date |
|
83 |
should be included. |
|
84 |
""" |
|
1705
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
85 |
assert not (incday and incmonth) |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
86 |
begin = todate(begin) |
1711
182536159750
Make sure end is datetime.date.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1705
diff
changeset
|
87 |
end = todate(end) |
1705
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
88 |
if incmonth: |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
89 |
while begin < end: |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
90 |
begin = next_month(begin, incmonth) |
1132 | 91 |
yield begin |
1705
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
92 |
else: |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
93 |
if not incday: |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
94 |
incr = ONEDAY |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
95 |
else: |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
96 |
incr = timedelta(incday) |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
97 |
while begin <= end: |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
98 |
yield begin |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
99 |
begin += incr |
0 | 100 |
|
709 | 101 |
def ustrftime(date, fmt='%Y-%m-%d'): |
102 |
"""like strftime, but returns a unicode string instead of an encoded |
|
1705
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
103 |
string which' may be problematic with localized date. |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
104 |
|
709 | 105 |
encoding is guessed by locale.getpreferredencoding() |
106 |
""" |
|
107 |
# date format may depend on the locale |
|
108 |
encoding = locale.getpreferredencoding(do_setlocale=False) or 'UTF-8' |
|
1397
6cbc7bc8ea6d
rollback florent's not-proven-to-be-useful fix
sylvain.thenault@logilab.fr
parents:
1387
diff
changeset
|
109 |
return unicode(date.strftime(str(fmt)), encoding) |
709 | 110 |
|
1024
bb96289257bf
remove unused working_hours function, new todate function since we can't compare date and datetime objects
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
111 |
|
3146
cfc4344023f2
have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents:
3101
diff
changeset
|
112 |
if sys.version_info[:2] < (2, 5): |
cfc4344023f2
have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents:
3101
diff
changeset
|
113 |
def make_uid(key): |
cfc4344023f2
have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents:
3101
diff
changeset
|
114 |
"""forge a unique identifier |
cfc4344023f2
have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents:
3101
diff
changeset
|
115 |
not that unique on win32""" |
cfc4344023f2
have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents:
3101
diff
changeset
|
116 |
msg = str(key) + "%.10f" % time() + str(randint(0, 1000000)) |
cfc4344023f2
have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents:
3101
diff
changeset
|
117 |
return md5(msg).hexdigest() |
cfc4344023f2
have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents:
3101
diff
changeset
|
118 |
else: |
cfc4344023f2
have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents:
3101
diff
changeset
|
119 |
from uuid import uuid4 |
cfc4344023f2
have a better make_uid function, esp. useful for win32 as collisions are frequents with the old one
Aurelien Campeas
parents:
3101
diff
changeset
|
120 |
def make_uid(key): |
3155
14e30e2520d7
oops, key can be non str
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3146
diff
changeset
|
121 |
return str(key) + str(uuid4()) |
0 | 122 |
|
123 |
def dump_class(cls, clsname): |
|
124 |
"""create copy of a class by creating an empty class inheriting |
|
125 |
from the given cls. |
|
126 |
||
127 |
Those class will be used as place holder for attribute and relation |
|
128 |
description |
|
129 |
""" |
|
130 |
# type doesn't accept unicode name |
|
131 |
# return type.__new__(type, str(clsname), (cls,), {}) |
|
132 |
# __autogenerated__ attribute is just a marker |
|
133 |
return type(str(clsname), (cls,), {'__autogenerated__': True}) |
|
134 |
||
135 |
||
136 |
def merge_dicts(dict1, dict2): |
|
137 |
"""update a copy of `dict1` with `dict2`""" |
|
138 |
dict1 = dict(dict1) |
|
139 |
dict1.update(dict2) |
|
140 |
return dict1 |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
141 |
|
0 | 142 |
|
143 |
class SizeConstrainedList(list): |
|
144 |
"""simple list that makes sure the list does not get bigger |
|
145 |
than a given size. |
|
146 |
||
147 |
when the list is full and a new element is added, the first |
|
148 |
element of the list is removed before appending the new one |
|
149 |
||
150 |
>>> l = SizeConstrainedList(2) |
|
151 |
>>> l.append(1) |
|
152 |
>>> l.append(2) |
|
153 |
>>> l |
|
154 |
[1, 2] |
|
155 |
>>> l.append(3) |
|
156 |
[2, 3] |
|
157 |
""" |
|
158 |
def __init__(self, maxsize): |
|
159 |
self.maxsize = maxsize |
|
160 |
||
161 |
def append(self, element): |
|
162 |
if len(self) == self.maxsize: |
|
163 |
del self[0] |
|
164 |
super(SizeConstrainedList, self).append(element) |
|
165 |
||
166 |
def extend(self, sequence): |
|
167 |
super(SizeConstrainedList, self).extend(sequence) |
|
168 |
keepafter = len(self) - self.maxsize |
|
169 |
if keepafter > 0: |
|
170 |
del self[:keepafter] |
|
171 |
||
172 |
__iadd__ = extend |
|
173 |
||
174 |
||
175 |
class UStringIO(list): |
|
176 |
"""a file wrapper which automatically encode unicode string to an encoding |
|
177 |
specifed in the constructor |
|
178 |
""" |
|
179 |
||
180 |
def __nonzero__(self): |
|
181 |
return True |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
182 |
|
0 | 183 |
def write(self, value): |
184 |
assert isinstance(value, unicode), u"unicode required not %s : %s"\ |
|
185 |
% (type(value).__name__, repr(value)) |
|
186 |
self.append(value) |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
187 |
|
0 | 188 |
def getvalue(self): |
189 |
return u''.join(self) |
|
190 |
||
191 |
def __repr__(self): |
|
192 |
return '<%s at %#x>' % (self.__class__.__name__, id(self)) |
|
193 |
||
194 |
||
195 |
class HTMLHead(UStringIO): |
|
196 |
"""wraps HTML header's stream |
|
197 |
||
198 |
Request objects use a HTMLHead instance to ease adding of |
|
199 |
javascripts and stylesheets |
|
200 |
""" |
|
201 |
js_unload_code = u'jQuery(window).unload(unloadPageData);' |
|
202 |
||
203 |
def __init__(self): |
|
204 |
super(HTMLHead, self).__init__() |
|
205 |
self.jsvars = [] |
|
206 |
self.jsfiles = [] |
|
207 |
self.cssfiles = [] |
|
208 |
self.ie_cssfiles = [] |
|
209 |
self.post_inlined_scripts = [] |
|
210 |
self.pagedata_unload = False |
|
211 |
||
212 |
||
213 |
def add_raw(self, rawheader): |
|
214 |
self.write(rawheader) |
|
215 |
||
216 |
def define_var(self, var, value): |
|
217 |
self.jsvars.append( (var, value) ) |
|
218 |
||
219 |
def add_post_inline_script(self, content): |
|
220 |
self.post_inlined_scripts.append(content) |
|
28
9b7067bfaa15
introduce html_headers.on_load() method as a shortcut for add_post_inline_script('''jQuery(document).ready(...''')
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
221 |
|
2258
79bc598c6411
when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2009
diff
changeset
|
222 |
def add_onload(self, jscode, jsoncall=False): |
79bc598c6411
when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2009
diff
changeset
|
223 |
if jsoncall: |
79bc598c6411
when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2009
diff
changeset
|
224 |
self.add_post_inline_script(u"""jQuery(CubicWeb).bind('ajax-loaded', function(event) { |
79bc598c6411
when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2009
diff
changeset
|
225 |
%s |
79bc598c6411
when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2009
diff
changeset
|
226 |
});""" % jscode) |
79bc598c6411
when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2009
diff
changeset
|
227 |
else: |
79bc598c6411
when request is a json request, bind on 'ajax-loaded' instead of document.ready()
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2009
diff
changeset
|
228 |
self.add_post_inline_script(u"""jQuery(document).ready(function () { |
28
9b7067bfaa15
introduce html_headers.on_load() method as a shortcut for add_post_inline_script('''jQuery(document).ready(...''')
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
229 |
%s |
9b7067bfaa15
introduce html_headers.on_load() method as a shortcut for add_post_inline_script('''jQuery(document).ready(...''')
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
230 |
});""" % jscode) |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
231 |
|
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
232 |
|
0 | 233 |
def add_js(self, jsfile): |
234 |
"""adds `jsfile` to the list of javascripts used in the webpage |
|
235 |
||
236 |
This function checks if the file has already been added |
|
237 |
:param jsfile: the script's URL |
|
238 |
""" |
|
239 |
if jsfile not in self.jsfiles: |
|
240 |
self.jsfiles.append(jsfile) |
|
241 |
||
242 |
def add_css(self, cssfile, media): |
|
243 |
"""adds `cssfile` to the list of javascripts used in the webpage |
|
244 |
||
245 |
This function checks if the file has already been added |
|
246 |
:param cssfile: the stylesheet's URL |
|
247 |
""" |
|
248 |
if (cssfile, media) not in self.cssfiles: |
|
249 |
self.cssfiles.append( (cssfile, media) ) |
|
250 |
||
251 |
def add_ie_css(self, cssfile, media='all'): |
|
252 |
"""registers some IE specific CSS""" |
|
253 |
if (cssfile, media) not in self.ie_cssfiles: |
|
254 |
self.ie_cssfiles.append( (cssfile, media) ) |
|
255 |
||
256 |
def add_unload_pagedata(self): |
|
257 |
"""registers onunload callback to clean page data on server""" |
|
258 |
if not self.pagedata_unload: |
|
259 |
self.post_inlined_scripts.append(self.js_unload_code) |
|
260 |
self.pagedata_unload = True |
|
261 |
||
643
616191014b8b
[jsoncontroller] reorganize _html_exec (used by replacePageChunk) to output required css and js scripts
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
28
diff
changeset
|
262 |
def getvalue(self, skiphead=False): |
0 | 263 |
"""reimplement getvalue to provide a consistent (and somewhat browser |
264 |
optimzed cf. http://stevesouders.com/cuzillion) order in external |
|
265 |
resources declaration |
|
266 |
""" |
|
267 |
w = self.write |
|
268 |
# 1/ variable declaration if any |
|
269 |
if self.jsvars: |
|
270 |
from simplejson import dumps |
|
2554
3b6a6d2f9d7e
make jsvars declaration xhtml and html compatible
Fabrice <fabrice@secondweb.fr>
parents:
2361
diff
changeset
|
271 |
w(u'<script type="text/javascript"><!--//--><![CDATA[//><!--\n') |
0 | 272 |
for var, value in self.jsvars: |
273 |
w(u'%s = %s;\n' % (var, dumps(value))) |
|
2554
3b6a6d2f9d7e
make jsvars declaration xhtml and html compatible
Fabrice <fabrice@secondweb.fr>
parents:
2361
diff
changeset
|
274 |
w(u'//--><!]]></script>\n') |
0 | 275 |
# 2/ css files |
276 |
for cssfile, media in self.cssfiles: |
|
277 |
w(u'<link rel="stylesheet" type="text/css" media="%s" href="%s"/>\n' % |
|
3091
c3d73cecb29e
[html-head] escape js and css urls
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2678
diff
changeset
|
278 |
(media, xml_escape(cssfile))) |
0 | 279 |
# 3/ ie css if necessary |
280 |
if self.ie_cssfiles: |
|
281 |
w(u'<!--[if lt IE 8]>\n') |
|
282 |
for cssfile, media in self.ie_cssfiles: |
|
283 |
w(u'<link rel="stylesheet" type="text/css" media="%s" href="%s"/>\n' % |
|
3091
c3d73cecb29e
[html-head] escape js and css urls
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2678
diff
changeset
|
284 |
(media, xml_escape(cssfile))) |
0 | 285 |
w(u'<![endif]--> \n') |
286 |
# 4/ js files |
|
287 |
for jsfile in self.jsfiles: |
|
3091
c3d73cecb29e
[html-head] escape js and css urls
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2678
diff
changeset
|
288 |
w(u'<script type="text/javascript" src="%s"></script>\n' % |
c3d73cecb29e
[html-head] escape js and css urls
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2678
diff
changeset
|
289 |
xml_escape(jsfile)) |
0 | 290 |
# 5/ post inlined scripts (i.e. scripts depending on other JS files) |
291 |
if self.post_inlined_scripts: |
|
292 |
w(u'<script type="text/javascript">\n') |
|
293 |
w(u'\n\n'.join(self.post_inlined_scripts)) |
|
294 |
w(u'\n</script>\n') |
|
643
616191014b8b
[jsoncontroller] reorganize _html_exec (used by replacePageChunk) to output required css and js scripts
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
28
diff
changeset
|
295 |
header = super(HTMLHead, self).getvalue() |
616191014b8b
[jsoncontroller] reorganize _html_exec (used by replacePageChunk) to output required css and js scripts
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
28
diff
changeset
|
296 |
if skiphead: |
616191014b8b
[jsoncontroller] reorganize _html_exec (used by replacePageChunk) to output required css and js scripts
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
28
diff
changeset
|
297 |
return header |
616191014b8b
[jsoncontroller] reorganize _html_exec (used by replacePageChunk) to output required css and js scripts
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
28
diff
changeset
|
298 |
return u'<head>\n%s</head>\n' % header |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
299 |
|
0 | 300 |
|
301 |
class HTMLStream(object): |
|
302 |
"""represents a HTML page. |
|
303 |
||
304 |
This is used my main templates so that HTML headers can be added |
|
305 |
at any time during the page generation. |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
306 |
|
0 | 307 |
HTMLStream uses the (U)StringIO interface to be compliant with |
308 |
existing code. |
|
309 |
""" |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
310 |
|
0 | 311 |
def __init__(self, req): |
312 |
# stream for <head> |
|
313 |
self.head = req.html_headers |
|
314 |
# main stream |
|
315 |
self.body = UStringIO() |
|
316 |
self.doctype = u'' |
|
317 |
# xmldecl and html opening tag |
|
318 |
self.xmldecl = u'<?xml version="1.0" encoding="%s"?>\n' % req.encoding |
|
319 |
self.htmltag = u'<html xmlns="http://www.w3.org/1999/xhtml" ' \ |
|
320 |
'xmlns:cubicweb="http://www.logilab.org/2008/cubicweb" ' \ |
|
321 |
'xml:lang="%s" lang="%s">' % (req.lang, req.lang) |
|
3094
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3091
diff
changeset
|
322 |
# keep main_stream's reference on req for easier text/html demoting |
978ed8c2c0e4
[googlemap] #344872 set request content-type to text/html
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3091
diff
changeset
|
323 |
req.main_stream = self |
0 | 324 |
|
325 |
def write(self, data): |
|
326 |
"""StringIO interface: this method will be assigned to self.w |
|
327 |
""" |
|
328 |
self.body.write(data) |
|
329 |
||
330 |
def getvalue(self): |
|
331 |
"""writes HTML headers, closes </head> tag and writes HTML body""" |
|
332 |
return u'%s\n%s\n%s\n%s\n%s\n</html>' % (self.xmldecl, self.doctype, |
|
333 |
self.htmltag, |
|
334 |
self.head.getvalue(), |
|
335 |
self.body.getvalue()) |
|
336 |
||
337 |
||
2935
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
338 |
def can_do_pdf_conversion(__answer=[None]): |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
339 |
"""pdf conversion depends on |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
340 |
* pyxmltrf (python package) |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
341 |
* fop 0.9x |
0 | 342 |
""" |
2935
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
343 |
if __answer[0] is not None: |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
344 |
return __answer[0] |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
345 |
try: |
2936
f35b64718d02
follow name change, fix pdf template registration, add debian package tracking
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2935
diff
changeset
|
346 |
import pysixt |
2935
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
347 |
except ImportError: |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
348 |
__answer[0] = False |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
349 |
return False |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
350 |
from subprocess import Popen, STDOUT |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
351 |
import os |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
352 |
try: |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
353 |
Popen(['/usr/bin/fop', '-q'], |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
354 |
stdout=open(os.devnull, 'w'), |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
355 |
stderr=STDOUT) |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
356 |
except OSError, e: |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
357 |
print e |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
358 |
__answer[0] = False |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
359 |
return False |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
360 |
__answer[0] = True |
e06b3eadef31
[views,pdf] minimal support for xhtml->pdf conversion
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2932
diff
changeset
|
361 |
return True |
3231
3ee43e2f8560
[utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3101
diff
changeset
|
362 |
|
3ee43e2f8560
[utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3101
diff
changeset
|
363 |
|
3ee43e2f8560
[utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3101
diff
changeset
|
364 |
class CubicWebJsonEncoder(simplejson.JSONEncoder): |
3ee43e2f8560
[utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3101
diff
changeset
|
365 |
"""define a simplejson encoder to be able to encode yams std types""" |
3ee43e2f8560
[utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3101
diff
changeset
|
366 |
def default(self, obj): |
3ee43e2f8560
[utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3101
diff
changeset
|
367 |
if isinstance(obj, pydatetime.datetime): |
3ee43e2f8560
[utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3101
diff
changeset
|
368 |
return obj.strftime('%Y/%m/%d %H:%M:%S') |
3ee43e2f8560
[utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3101
diff
changeset
|
369 |
elif isinstance(obj, pydatetime.date): |
3ee43e2f8560
[utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3101
diff
changeset
|
370 |
return obj.strftime('%Y/%m/%d') |
3ee43e2f8560
[utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3101
diff
changeset
|
371 |
elif isinstance(obj, pydatetime.time): |
3ee43e2f8560
[utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3101
diff
changeset
|
372 |
return obj.strftime('%H:%M:%S') |
3ee43e2f8560
[utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3101
diff
changeset
|
373 |
elif isinstance(obj, decimal.Decimal): |
3ee43e2f8560
[utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3101
diff
changeset
|
374 |
return float(obj) |
3ee43e2f8560
[utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3101
diff
changeset
|
375 |
try: |
3ee43e2f8560
[utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3101
diff
changeset
|
376 |
return simplejson.JSONEncoder.default(self, obj) |
3ee43e2f8560
[utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3101
diff
changeset
|
377 |
except TypeError: |
3ee43e2f8560
[utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3101
diff
changeset
|
378 |
# we never ever want to fail because of an unknown type, |
3ee43e2f8560
[utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3101
diff
changeset
|
379 |
# just return None in those cases. |
3ee43e2f8560
[utils] provide a class to extend the default simplejson encoder to be able to dump standard yams attribute types
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3101
diff
changeset
|
380 |
return None |