author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 11 Sep 2009 08:39:17 +0200 | |
branch | stable |
changeset 3174 | 97273eeaaead |
parent 3094 | 978ed8c2c0e4 |
child 3101 | e75ab3635f07 |
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 |
0 | 13 |
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
|
14 |
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
|
15 |
from time import time, mktime |
0 | 16 |
from random import randint, seed |
1625
744f0cc8758f
restore monthrange import
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
1616
diff
changeset
|
17 |
from calendar import monthrange |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
18 |
|
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
|
19 |
# 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
|
20 |
seed() |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
21 |
try: |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
22 |
strptime = datetime.strptime |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
23 |
except AttributeError: # py < 2.5 |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
24 |
from time import strptime as time_strptime |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
25 |
def strptime(value, format): |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
709
diff
changeset
|
26 |
return datetime(*time_strptime(value, format)[:6]) |
0 | 27 |
|
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
|
28 |
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
|
29 |
"""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
|
30 |
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
|
31 |
return date(somedate.year, somedate.month, somedate.day) |
1603 | 32 |
assert isinstance(somedate, date), repr(somedate) |
1621
6260508f7d71
todatetime impl., fix todate impl.
sylvain.thenault@logilab.fr
parents:
1616
diff
changeset
|
33 |
return somedate |
6260508f7d71
todatetime impl., fix todate impl.
sylvain.thenault@logilab.fr
parents:
1616
diff
changeset
|
34 |
|
6260508f7d71
todatetime impl., fix todate impl.
sylvain.thenault@logilab.fr
parents:
1616
diff
changeset
|
35 |
def todatetime(somedate): |
6260508f7d71
todatetime impl., fix todate impl.
sylvain.thenault@logilab.fr
parents:
1616
diff
changeset
|
36 |
"""return a date from a date (leaving unchanged) or a datetime""" |
1751 | 37 |
# take care, datetime is a subclass of date |
38 |
if isinstance(somedate, datetime): |
|
39 |
return somedate |
|
40 |
assert isinstance(somedate, date), repr(somedate) |
|
41 |
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
|
42 |
|
2009
b1e2b9e4c809
[utils] move datetime2ticks from cw.web.views.plots to cw.utils
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
43 |
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
|
44 |
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
|
45 |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
46 |
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
|
47 |
ONEWEEK = timedelta(days=7) |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
48 |
|
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
49 |
def days_in_month(date_): |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
50 |
return monthrange(date_.year, date_.month)[1] |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
51 |
|
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 |
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
|
53 |
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
|
54 |
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
|
55 |
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
|
56 |
return date_ |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
57 |
|
1697
5dae28906769
next_month / previous_month now support a new arg to give number of month
sylvain.thenault@logilab.fr
parents:
1626
diff
changeset
|
58 |
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
|
59 |
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
|
60 |
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
|
61 |
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
|
62 |
return date_ |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
63 |
|
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
64 |
def first_day(date_): |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
65 |
return date(date_.year, date_.month, 1) |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
66 |
|
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
67 |
def last_day(date_): |
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
68 |
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
|
69 |
|
1705
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
70 |
def date_range(begin, end, incday=None, incmonth=None): |
0 | 71 |
"""yields each date between begin and end |
72 |
:param begin: the start date |
|
73 |
:param end: the end date |
|
74 |
: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
|
75 |
one day. |
0 | 76 |
:param include: None (means no exclusion) or a function taking a |
77 |
date as parameter, and returning True if the date |
|
78 |
should be included. |
|
79 |
""" |
|
1705
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
80 |
assert not (incday and incmonth) |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
81 |
begin = todate(begin) |
1711
182536159750
Make sure end is datetime.date.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1705
diff
changeset
|
82 |
end = todate(end) |
1705
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
83 |
if incmonth: |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
84 |
while begin < end: |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
85 |
begin = next_month(begin, incmonth) |
1132 | 86 |
yield begin |
1705
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
87 |
else: |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
88 |
if not incday: |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
89 |
incr = ONEDAY |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
90 |
else: |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
91 |
incr = timedelta(incday) |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
92 |
while begin <= end: |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
93 |
yield begin |
d5e02874ae77
Fix date_range increments argument.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
1697
diff
changeset
|
94 |
begin += incr |
0 | 95 |
|
709 | 96 |
def ustrftime(date, fmt='%Y-%m-%d'): |
97 |
"""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
|
98 |
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
|
99 |
|
709 | 100 |
encoding is guessed by locale.getpreferredencoding() |
101 |
""" |
|
102 |
# date format may depend on the locale |
|
103 |
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
|
104 |
return unicode(date.strftime(str(fmt)), encoding) |
709 | 105 |
|
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
|
106 |
def make_uid(key): |
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
|
107 |
"""forge a unique identifier""" |
2009
b1e2b9e4c809
[utils] move datetime2ticks from cw.web.views.plots to cw.utils
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
108 |
msg = str(key) + "%.10f" % time() + str(randint(0, 1000000)) |
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
|
109 |
return md5(msg).hexdigest() |
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
|
110 |
|
0 | 111 |
|
112 |
def dump_class(cls, clsname): |
|
113 |
"""create copy of a class by creating an empty class inheriting |
|
114 |
from the given cls. |
|
115 |
||
116 |
Those class will be used as place holder for attribute and relation |
|
117 |
description |
|
118 |
""" |
|
119 |
# type doesn't accept unicode name |
|
120 |
# return type.__new__(type, str(clsname), (cls,), {}) |
|
121 |
# __autogenerated__ attribute is just a marker |
|
122 |
return type(str(clsname), (cls,), {'__autogenerated__': True}) |
|
123 |
||
124 |
||
125 |
def merge_dicts(dict1, dict2): |
|
126 |
"""update a copy of `dict1` with `dict2`""" |
|
127 |
dict1 = dict(dict1) |
|
128 |
dict1.update(dict2) |
|
129 |
return dict1 |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
130 |
|
0 | 131 |
|
132 |
class SizeConstrainedList(list): |
|
133 |
"""simple list that makes sure the list does not get bigger |
|
134 |
than a given size. |
|
135 |
||
136 |
when the list is full and a new element is added, the first |
|
137 |
element of the list is removed before appending the new one |
|
138 |
||
139 |
>>> l = SizeConstrainedList(2) |
|
140 |
>>> l.append(1) |
|
141 |
>>> l.append(2) |
|
142 |
>>> l |
|
143 |
[1, 2] |
|
144 |
>>> l.append(3) |
|
145 |
[2, 3] |
|
146 |
""" |
|
147 |
def __init__(self, maxsize): |
|
148 |
self.maxsize = maxsize |
|
149 |
||
150 |
def append(self, element): |
|
151 |
if len(self) == self.maxsize: |
|
152 |
del self[0] |
|
153 |
super(SizeConstrainedList, self).append(element) |
|
154 |
||
155 |
def extend(self, sequence): |
|
156 |
super(SizeConstrainedList, self).extend(sequence) |
|
157 |
keepafter = len(self) - self.maxsize |
|
158 |
if keepafter > 0: |
|
159 |
del self[:keepafter] |
|
160 |
||
161 |
__iadd__ = extend |
|
162 |
||
163 |
||
164 |
class UStringIO(list): |
|
165 |
"""a file wrapper which automatically encode unicode string to an encoding |
|
166 |
specifed in the constructor |
|
167 |
""" |
|
168 |
||
169 |
def __nonzero__(self): |
|
170 |
return True |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
171 |
|
0 | 172 |
def write(self, value): |
173 |
assert isinstance(value, unicode), u"unicode required not %s : %s"\ |
|
174 |
% (type(value).__name__, repr(value)) |
|
175 |
self.append(value) |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
176 |
|
0 | 177 |
def getvalue(self): |
178 |
return u''.join(self) |
|
179 |
||
180 |
def __repr__(self): |
|
181 |
return '<%s at %#x>' % (self.__class__.__name__, id(self)) |
|
182 |
||
183 |
||
184 |
class HTMLHead(UStringIO): |
|
185 |
"""wraps HTML header's stream |
|
186 |
||
187 |
Request objects use a HTMLHead instance to ease adding of |
|
188 |
javascripts and stylesheets |
|
189 |
""" |
|
190 |
js_unload_code = u'jQuery(window).unload(unloadPageData);' |
|
191 |
||
192 |
def __init__(self): |
|
193 |
super(HTMLHead, self).__init__() |
|
194 |
self.jsvars = [] |
|
195 |
self.jsfiles = [] |
|
196 |
self.cssfiles = [] |
|
197 |
self.ie_cssfiles = [] |
|
198 |
self.post_inlined_scripts = [] |
|
199 |
self.pagedata_unload = False |
|
200 |
||
201 |
||
202 |
def add_raw(self, rawheader): |
|
203 |
self.write(rawheader) |
|
204 |
||
205 |
def define_var(self, var, value): |
|
206 |
self.jsvars.append( (var, value) ) |
|
207 |
||
208 |
def add_post_inline_script(self, content): |
|
209 |
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
|
210 |
|
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
|
211 |
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
|
212 |
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
|
213 |
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
|
214 |
%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
|
215 |
});""" % 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
|
216 |
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
|
217 |
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
|
218 |
%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
|
219 |
});""" % jscode) |
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
220 |
|
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
221 |
|
0 | 222 |
def add_js(self, jsfile): |
223 |
"""adds `jsfile` to the list of javascripts used in the webpage |
|
224 |
||
225 |
This function checks if the file has already been added |
|
226 |
:param jsfile: the script's URL |
|
227 |
""" |
|
228 |
if jsfile not in self.jsfiles: |
|
229 |
self.jsfiles.append(jsfile) |
|
230 |
||
231 |
def add_css(self, cssfile, media): |
|
232 |
"""adds `cssfile` to the list of javascripts used in the webpage |
|
233 |
||
234 |
This function checks if the file has already been added |
|
235 |
:param cssfile: the stylesheet's URL |
|
236 |
""" |
|
237 |
if (cssfile, media) not in self.cssfiles: |
|
238 |
self.cssfiles.append( (cssfile, media) ) |
|
239 |
||
240 |
def add_ie_css(self, cssfile, media='all'): |
|
241 |
"""registers some IE specific CSS""" |
|
242 |
if (cssfile, media) not in self.ie_cssfiles: |
|
243 |
self.ie_cssfiles.append( (cssfile, media) ) |
|
244 |
||
245 |
def add_unload_pagedata(self): |
|
246 |
"""registers onunload callback to clean page data on server""" |
|
247 |
if not self.pagedata_unload: |
|
248 |
self.post_inlined_scripts.append(self.js_unload_code) |
|
249 |
self.pagedata_unload = True |
|
250 |
||
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
|
251 |
def getvalue(self, skiphead=False): |
0 | 252 |
"""reimplement getvalue to provide a consistent (and somewhat browser |
253 |
optimzed cf. http://stevesouders.com/cuzillion) order in external |
|
254 |
resources declaration |
|
255 |
""" |
|
256 |
w = self.write |
|
257 |
# 1/ variable declaration if any |
|
258 |
if self.jsvars: |
|
259 |
from simplejson import dumps |
|
2554
3b6a6d2f9d7e
make jsvars declaration xhtml and html compatible
Fabrice <fabrice@secondweb.fr>
parents:
2361
diff
changeset
|
260 |
w(u'<script type="text/javascript"><!--//--><![CDATA[//><!--\n') |
0 | 261 |
for var, value in self.jsvars: |
262 |
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
|
263 |
w(u'//--><!]]></script>\n') |
0 | 264 |
# 2/ css files |
265 |
for cssfile, media in self.cssfiles: |
|
266 |
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
|
267 |
(media, xml_escape(cssfile))) |
0 | 268 |
# 3/ ie css if necessary |
269 |
if self.ie_cssfiles: |
|
270 |
w(u'<!--[if lt IE 8]>\n') |
|
271 |
for cssfile, media in self.ie_cssfiles: |
|
272 |
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
|
273 |
(media, xml_escape(cssfile))) |
0 | 274 |
w(u'<![endif]--> \n') |
275 |
# 4/ js files |
|
276 |
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
|
277 |
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
|
278 |
xml_escape(jsfile)) |
0 | 279 |
# 5/ post inlined scripts (i.e. scripts depending on other JS files) |
280 |
if self.post_inlined_scripts: |
|
281 |
w(u'<script type="text/javascript">\n') |
|
282 |
w(u'\n\n'.join(self.post_inlined_scripts)) |
|
283 |
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
|
284 |
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
|
285 |
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
|
286 |
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
|
287 |
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
|
288 |
|
0 | 289 |
|
290 |
class HTMLStream(object): |
|
291 |
"""represents a HTML page. |
|
292 |
||
293 |
This is used my main templates so that HTML headers can be added |
|
294 |
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
|
295 |
|
0 | 296 |
HTMLStream uses the (U)StringIO interface to be compliant with |
297 |
existing code. |
|
298 |
""" |
|
1549
f87561822e27
some basic calendar manipulation functions, delete-trailing-whitespace
sylvain.thenault@logilab.fr
parents:
1397
diff
changeset
|
299 |
|
0 | 300 |
def __init__(self, req): |
301 |
# stream for <head> |
|
302 |
self.head = req.html_headers |
|
303 |
# main stream |
|
304 |
self.body = UStringIO() |
|
305 |
self.doctype = u'' |
|
306 |
# xmldecl and html opening tag |
|
307 |
self.xmldecl = u'<?xml version="1.0" encoding="%s"?>\n' % req.encoding |
|
308 |
self.htmltag = u'<html xmlns="http://www.w3.org/1999/xhtml" ' \ |
|
309 |
'xmlns:cubicweb="http://www.logilab.org/2008/cubicweb" ' \ |
|
310 |
'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
|
311 |
# 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
|
312 |
req.main_stream = self |
0 | 313 |
|
314 |
def write(self, data): |
|
315 |
"""StringIO interface: this method will be assigned to self.w |
|
316 |
""" |
|
317 |
self.body.write(data) |
|
318 |
||
319 |
def getvalue(self): |
|
320 |
"""writes HTML headers, closes </head> tag and writes HTML body""" |
|
321 |
return u'%s\n%s\n%s\n%s\n%s\n</html>' % (self.xmldecl, self.doctype, |
|
322 |
self.htmltag, |
|
323 |
self.head.getvalue(), |
|
324 |
self.body.getvalue()) |
|
325 |
||
326 |
||
327 |
class AcceptMixIn(object): |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2554
diff
changeset
|
328 |
"""Mixin class for appobjects defining the 'accepts' attribute describing |
0 | 329 |
a set of supported entity type (Any by default). |
330 |
""" |
|
331 |
# XXX deprecated, no more necessary |
|
332 |