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