author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Sat, 01 Aug 2009 16:18:14 +0200 | |
changeset 2630 | b3f997bc8c26 |
parent 2613 | 5e19c2bb370e |
child 2644 | 100b81ba3a98 |
permissions | -rw-r--r-- |
0 | 1 |
"""abstract views and templates classes for CubicWeb web client |
2 |
||
3 |
||
4 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1756
diff
changeset
|
5 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 6 |
: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:
1756
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 8 |
""" |
9 |
__docformat__ = "restructuredtext en" |
|
1999
b9a8f5995658
obsolete methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1985
diff
changeset
|
10 |
_ = unicode |
0 | 11 |
|
12 |
from cStringIO import StringIO |
|
13 |
||
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2558
diff
changeset
|
14 |
from logilab.common.deprecation import deprecated |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2217
diff
changeset
|
15 |
from logilab.mtconverter import xml_escape |
0 | 16 |
|
1181
620ec8e6ae19
cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents:
1144
diff
changeset
|
17 |
from cubicweb import NotAnEntity |
1132 | 18 |
from cubicweb.selectors import yes, non_final_entity, nonempty_rset, none_rset |
635
305da8d6aa2d
require_group/match_user_group -> match_user_groups
sylvain.thenault@logilab.fr
parents:
633
diff
changeset
|
19 |
from cubicweb.selectors import require_group_compat, accepts_compat |
707 | 20 |
from cubicweb.appobject import AppRsetObject |
708
b4728112625f
move utils from cw.common to cw
sylvain.thenault@logilab.fr
parents:
707
diff
changeset
|
21 |
from cubicweb.utils import UStringIO, HTMLStream |
2217
3a8cee8f8778
explicit display_name import
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2207
diff
changeset
|
22 |
from cubicweb.schema import display_name |
633 | 23 |
|
0 | 24 |
# robots control |
25 |
NOINDEX = u'<meta name="ROBOTS" content="NOINDEX" />' |
|
26 |
NOFOLLOW = u'<meta name="ROBOTS" content="NOFOLLOW" />' |
|
27 |
||
28 |
CW_XHTML_EXTENSIONS = '''[ |
|
29 |
<!ATTLIST html xmlns:cubicweb CDATA #FIXED \'http://www.logilab.org/2008/cubicweb\' > |
|
30 |
||
31 |
<!ENTITY % coreattrs |
|
522
385ce5e0b30b
cubicweb__loadurl -> cubicweb:lazyloadurl
sylvain.thenault@logilab.fr
parents:
497
diff
changeset
|
32 |
"id ID #IMPLIED |
385ce5e0b30b
cubicweb__loadurl -> cubicweb:lazyloadurl
sylvain.thenault@logilab.fr
parents:
497
diff
changeset
|
33 |
class CDATA #IMPLIED |
385ce5e0b30b
cubicweb__loadurl -> cubicweb:lazyloadurl
sylvain.thenault@logilab.fr
parents:
497
diff
changeset
|
34 |
style CDATA #IMPLIED |
0 | 35 |
title CDATA #IMPLIED |
36 |
||
37 |
cubicweb:sortvalue CDATA #IMPLIED |
|
38 |
cubicweb:target CDATA #IMPLIED |
|
39 |
cubicweb:limit CDATA #IMPLIED |
|
40 |
cubicweb:type CDATA #IMPLIED |
|
41 |
cubicweb:loadtype CDATA #IMPLIED |
|
42 |
cubicweb:wdgtype CDATA #IMPLIED |
|
43 |
cubicweb:initfunc CDATA #IMPLIED |
|
44 |
cubicweb:inputid CDATA #IMPLIED |
|
45 |
cubicweb:tindex CDATA #IMPLIED |
|
46 |
cubicweb:inputname CDATA #IMPLIED |
|
47 |
cubicweb:value CDATA #IMPLIED |
|
48 |
cubicweb:required CDATA #IMPLIED |
|
49 |
cubicweb:accesskey CDATA #IMPLIED |
|
50 |
cubicweb:maxlength CDATA #IMPLIED |
|
51 |
cubicweb:variables CDATA #IMPLIED |
|
52 |
cubicweb:displayactions CDATA #IMPLIED |
|
53 |
cubicweb:fallbackvid CDATA #IMPLIED |
|
1498
2c6eec0b46b9
fix imports, cleanup, repair some ajax calls
sylvain.thenault@logilab.fr
parents:
1433
diff
changeset
|
54 |
cubicweb:fname CDATA #IMPLIED |
0 | 55 |
cubicweb:vid CDATA #IMPLIED |
56 |
cubicweb:rql CDATA #IMPLIED |
|
57 |
cubicweb:actualrql CDATA #IMPLIED |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
58 |
cubicweb:rooteid CDATA #IMPLIED |
0 | 59 |
cubicweb:dataurl CDATA #IMPLIED |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
60 |
cubicweb:size CDATA #IMPLIED |
0 | 61 |
cubicweb:tlunit CDATA #IMPLIED |
62 |
cubicweb:loadurl CDATA #IMPLIED |
|
63 |
cubicweb:uselabel CDATA #IMPLIED |
|
472 | 64 |
cubicweb:facetargs CDATA #IMPLIED |
65 |
cubicweb:facetName CDATA #IMPLIED |
|
0 | 66 |
"> ] ''' |
67 |
||
1421
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
68 |
TRANSITIONAL_DOCTYPE = u'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" %s>\n' % CW_XHTML_EXTENSIONS |
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
69 |
TRANSITIONAL_DOCTYPE_NOEXT = u'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n' |
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
70 |
STRICT_DOCTYPE = u'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" %s>\n' % CW_XHTML_EXTENSIONS |
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
71 |
STRICT_DOCTYPE_NOEXT = u'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n' |
0 | 72 |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
73 |
# base view object ############################################################ |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
74 |
|
0 | 75 |
class View(AppRsetObject): |
76 |
"""abstract view class, used as base for every renderable object such |
|
77 |
as views, templates, some components...web |
|
78 |
||
79 |
A view is instantiated to render a [part of a] result set. View |
|
80 |
subclasses may be parametred using the following class attributes: |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
81 |
|
0 | 82 |
* `templatable` indicates if the view may be embeded in a main |
83 |
template or if it has to be rendered standalone (i.e. XML for |
|
84 |
instance) |
|
85 |
* if the view is not templatable, it should set the `content_type` class |
|
86 |
attribute to the correct MIME type (text/xhtml by default) |
|
87 |
* the `category` attribute may be used in the interface to regroup related |
|
88 |
objects together |
|
89 |
||
90 |
At instantiation time, the standard `req`, `rset`, and `cursor` |
|
91 |
attributes are added and the `w` attribute will be set at rendering |
|
92 |
time to a write function to use. |
|
93 |
""" |
|
816
9cd49a910fce
kill Template class and 'templates' registry
sylvain.thenault@logilab.fr
parents:
781
diff
changeset
|
94 |
__registry__ = 'views' |
9cd49a910fce
kill Template class and 'templates' registry
sylvain.thenault@logilab.fr
parents:
781
diff
changeset
|
95 |
registered = require_group_compat(AppRsetObject.registered) |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
96 |
|
0 | 97 |
templatable = True |
98 |
need_navigation = True |
|
99 |
# content_type = 'application/xhtml+xml' # text/xhtml' |
|
100 |
binary = False |
|
101 |
add_to_breadcrumbs = True |
|
102 |
category = 'view' |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
103 |
|
1143
8d097defbf2c
change the way selected/__init__ are used for AppRsetObject
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
104 |
def __init__(self, req=None, rset=None, **kwargs): |
8d097defbf2c
change the way selected/__init__ are used for AppRsetObject
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
105 |
super(View, self).__init__(req, rset, **kwargs) |
0 | 106 |
self.w = None |
107 |
||
108 |
@property |
|
109 |
def content_type(self): |
|
1421
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
110 |
return self.req.html_content_type() |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
111 |
|
0 | 112 |
def set_stream(self, w=None): |
113 |
if self.w is not None: |
|
114 |
return |
|
115 |
if w is None: |
|
116 |
if self.binary: |
|
117 |
self._stream = stream = StringIO() |
|
118 |
else: |
|
119 |
self._stream = stream = UStringIO() |
|
120 |
w = stream.write |
|
121 |
else: |
|
122 |
stream = None |
|
123 |
self.w = w |
|
124 |
return stream |
|
125 |
||
126 |
# main view interface ##################################################### |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
127 |
|
1723 | 128 |
def render(self, w=None, **context): |
0 | 129 |
"""called to render a view object for a result set. |
130 |
||
131 |
This method is a dispatched to an actual method selected |
|
132 |
according to optional row and col parameters, which are locating |
|
133 |
a particular row or cell in the result set: |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
134 |
|
1132 | 135 |
* if row is specified, `cell_call` is called |
0 | 136 |
* if none of them is supplied, the view is considered to apply on |
137 |
the whole result set (which may be None in this case), `call` is |
|
138 |
called |
|
139 |
""" |
|
1132 | 140 |
row = context.get('row') |
0 | 141 |
if row is not None: |
142 |
context.setdefault('col', 0) |
|
143 |
view_func = self.cell_call |
|
144 |
else: |
|
145 |
view_func = self.call |
|
146 |
stream = self.set_stream(w) |
|
147 |
# stream = self.set_stream(context) |
|
148 |
view_func(**context) |
|
149 |
# return stream content if we have created it |
|
150 |
if stream is not None: |
|
151 |
return self._stream.getvalue() |
|
152 |
||
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2558
diff
changeset
|
153 |
dispatch = deprecated('.dispatch is deprecated, use .render')(render) |
1723 | 154 |
|
0 | 155 |
# should default .call() method add a <div classs="section"> around each |
156 |
# rset item |
|
157 |
add_div_section = True |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
158 |
|
0 | 159 |
def call(self, **kwargs): |
160 |
"""the view is called for an entire result set, by default loop |
|
161 |
other rows of the result set and call the same view on the |
|
162 |
particular row |
|
163 |
||
164 |
Views applicable on None result sets have to override this method |
|
165 |
""" |
|
166 |
rset = self.rset |
|
167 |
if rset is None: |
|
168 |
raise NotImplementedError, self |
|
169 |
wrap = self.templatable and len(rset) > 1 and self.add_div_section |
|
170 |
for i in xrange(len(rset)): |
|
171 |
if wrap: |
|
172 |
self.w(u'<div class="section">') |
|
173 |
self.wview(self.id, rset, row=i, **kwargs) |
|
174 |
if wrap: |
|
175 |
self.w(u"</div>") |
|
176 |
||
177 |
def cell_call(self, row, col, **kwargs): |
|
178 |
"""the view is called for a particular result set cell""" |
|
179 |
raise NotImplementedError, self |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
180 |
|
0 | 181 |
def linkable(self): |
182 |
"""return True if the view may be linked in a menu |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
183 |
|
0 | 184 |
by default views without title are not meant to be displayed |
185 |
""" |
|
186 |
if not getattr(self, 'title', None): |
|
187 |
return False |
|
188 |
return True |
|
189 |
||
190 |
def is_primary(self): |
|
191 |
return self.id == 'primary' |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
192 |
|
0 | 193 |
def url(self): |
194 |
"""return the url associated with this view. Should not be |
|
195 |
necessary for non linkable views, but a default implementation |
|
196 |
is provided anyway. |
|
197 |
""" |
|
2468
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
198 |
rset = self.rset |
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
199 |
if rset is None: |
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
200 |
return self.build_url('view', vid=self.id) |
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
201 |
coltypes = rset.column_types(0) |
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
202 |
if len(coltypes) == 1: |
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
203 |
etype = iter(coltypes).next() |
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
204 |
if not self.schema.eschema(etype).is_final(): |
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
205 |
if len(rset) == 1: |
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
206 |
entity = rset.get_entity(0, 0) |
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
207 |
return entity.absolute_url(vid=self.id) |
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
208 |
# don't want to generate /<etype> url if there is some restriction |
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
209 |
# on something else than the entity type |
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
210 |
restr = rset.syntax_tree().children[0].where |
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
211 |
# XXX norestriction is not correct here. For instance, in cases like |
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
212 |
# "Any P,N WHERE P is Project, P name N" norestriction should equal |
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
213 |
# True |
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
214 |
norestriction = (isinstance(restr, nodes.Relation) and |
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
215 |
restr.is_types_restriction()) |
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
216 |
if norestriction: |
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
217 |
return self.build_url(etype.lower(), vid=self.id) |
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
218 |
return self.build_url('view', rql=rset.printable_rql(), vid=self.id) |
0 | 219 |
|
220 |
def set_request_content_type(self): |
|
221 |
"""set the content type returned by this view""" |
|
222 |
self.req.set_content_type(self.content_type) |
|
223 |
||
224 |
# view utilities ########################################################## |
|
1433 | 225 |
|
1985
9c1db4e06095
move view method's logic on the registry, so it's easier to call it from outside an appobject. Also make rset argument optional
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
226 |
def wview(self, __vid, rset=None, __fallback_vid=None, **kwargs): |
0 | 227 |
"""shortcut to self.view method automatically passing self.w as argument |
228 |
""" |
|
229 |
self.view(__vid, rset, __fallback_vid, w=self.w, **kwargs) |
|
1433 | 230 |
|
819 | 231 |
# XXX Template bw compat |
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2558
diff
changeset
|
232 |
template = deprecated('.template is deprecated, use .view')(wview) |
0 | 233 |
|
234 |
def whead(self, data): |
|
235 |
self.req.html_headers.write(data) |
|
236 |
||
237 |
def wdata(self, data): |
|
238 |
"""simple helper that escapes `data` and writes into `self.w`""" |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2217
diff
changeset
|
239 |
self.w(xml_escape(data)) |
0 | 240 |
|
241 |
def html_headers(self): |
|
242 |
"""return a list of html headers (eg something to be inserted between |
|
243 |
<head> and </head> of the returned page |
|
244 |
||
245 |
by default return a meta tag to disable robot indexation of the page |
|
246 |
""" |
|
247 |
return [NOINDEX] |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
248 |
|
0 | 249 |
def page_title(self): |
250 |
"""returns a title according to the result set - used for the |
|
251 |
title in the HTML header |
|
252 |
""" |
|
253 |
vtitle = self.req.form.get('vtitle') |
|
254 |
if vtitle: |
|
255 |
return self.req._(vtitle) |
|
256 |
# class defined title will only be used if the resulting title doesn't |
|
257 |
# seem clear enough |
|
258 |
vtitle = getattr(self, 'title', None) or u'' |
|
259 |
if vtitle: |
|
260 |
vtitle = self.req._(vtitle) |
|
261 |
rset = self.rset |
|
262 |
if rset and rset.rowcount: |
|
263 |
if rset.rowcount == 1: |
|
264 |
try: |
|
265 |
entity = self.complete_entity(0) |
|
266 |
# use long_title to get context information if any |
|
267 |
clabel = entity.dc_long_title() |
|
268 |
except NotAnEntity: |
|
269 |
clabel = display_name(self.req, rset.description[0][0]) |
|
270 |
clabel = u'%s (%s)' % (clabel, vtitle) |
|
271 |
else : |
|
272 |
etypes = rset.column_types(0) |
|
273 |
if len(etypes) == 1: |
|
274 |
etype = iter(etypes).next() |
|
275 |
clabel = display_name(self.req, etype, 'plural') |
|
276 |
else : |
|
277 |
clabel = u'#[*] (%s)' % vtitle |
|
278 |
else: |
|
279 |
clabel = vtitle |
|
280 |
return u'%s (%s)' % (clabel, self.req.property_value('ui.site-title')) |
|
281 |
||
282 |
def output_url_builder( self, name, url, args ): |
|
283 |
self.w(u'<script language="JavaScript"><!--\n' \ |
|
284 |
u'function %s( %s ) {\n' % (name, ','.join(args) ) ) |
|
285 |
url_parts = url.split("%s") |
|
286 |
self.w(u' url="%s"' % url_parts[0] ) |
|
287 |
for arg, part in zip(args, url_parts[1:]): |
|
288 |
self.w(u'+str(%s)' % arg ) |
|
289 |
if part: |
|
290 |
self.w(u'+"%s"' % part) |
|
291 |
self.w('\n document.window.href=url;\n') |
|
292 |
self.w('}\n-->\n</script>\n') |
|
293 |
||
294 |
def create_url(self, etype, **kwargs): |
|
295 |
""" return the url of the entity creation form for a given entity type""" |
|
296 |
return self.req.build_url('add/%s'%etype, **kwargs) |
|
297 |
||
298 |
def field(self, label, value, row=True, show_label=True, w=None, tr=True): |
|
299 |
""" read-only field """ |
|
300 |
if w is None: |
|
301 |
w = self.w |
|
302 |
if row: |
|
303 |
w(u'<div class="row">') |
|
304 |
if show_label: |
|
305 |
if tr: |
|
306 |
label = display_name(self.req, label) |
|
307 |
w(u'<span class="label">%s</span>' % label) |
|
308 |
w(u'<div class="field">%s</div>' % value) |
|
309 |
if row: |
|
310 |
w(u'</div>') |
|
1433 | 311 |
|
0 | 312 |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
313 |
|
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
314 |
# concrete views base classes ################################################# |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
315 |
|
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
316 |
class EntityView(View): |
843 | 317 |
"""base class for views applying on an entity (i.e. uniform result set)""" |
781
323656dd85a9
fix import, use non_final_entity instead of implements('Any')
sylvain.thenault@logilab.fr
parents:
757
diff
changeset
|
318 |
__select__ = non_final_entity() |
757
01740274e774
remove explicit access to .im_func when overriding `registered`
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
719
diff
changeset
|
319 |
registered = accepts_compat(View.registered) |
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
320 |
|
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
321 |
category = 'entityview' |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
322 |
|
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
323 |
|
0 | 324 |
class StartupView(View): |
843 | 325 |
"""base class for views which doesn't need a particular result set to be |
326 |
displayed (so they can always be displayed !) |
|
0 | 327 |
""" |
707 | 328 |
__select__ = none_rset() |
757
01740274e774
remove explicit access to .im_func when overriding `registered`
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
719
diff
changeset
|
329 |
registered = require_group_compat(View.registered) |
1433 | 330 |
|
0 | 331 |
category = 'startupview' |
1433 | 332 |
|
0 | 333 |
def html_headers(self): |
334 |
"""return a list of html headers (eg something to be inserted between |
|
335 |
<head> and </head> of the returned page |
|
336 |
||
337 |
by default startup views are indexed |
|
338 |
""" |
|
339 |
return [] |
|
340 |
||
341 |
||
342 |
class EntityStartupView(EntityView): |
|
343 |
"""base class for entity views which may also be applied to None |
|
344 |
result set (usually a default rql is provided by the view class) |
|
345 |
""" |
|
781
323656dd85a9
fix import, use non_final_entity instead of implements('Any')
sylvain.thenault@logilab.fr
parents:
757
diff
changeset
|
346 |
__select__ = none_rset() | non_final_entity() |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
347 |
|
0 | 348 |
default_rql = None |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
349 |
|
2418
8f06e4f02733
optional rset
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
350 |
def __init__(self, req, rset=None, **kwargs): |
1143
8d097defbf2c
change the way selected/__init__ are used for AppRsetObject
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
351 |
super(EntityStartupView, self).__init__(req, rset, **kwargs) |
0 | 352 |
if rset is None: |
353 |
# this instance is not in the "entityview" category |
|
354 |
self.category = 'startupview' |
|
355 |
||
356 |
def startup_rql(self): |
|
873 | 357 |
"""return some rql to be executed if the result set is None""" |
0 | 358 |
return self.default_rql |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
359 |
|
0 | 360 |
def call(self, **kwargs): |
873 | 361 |
"""override call to execute rql returned by the .startup_rql method if |
362 |
necessary |
|
0 | 363 |
""" |
364 |
if self.rset is None: |
|
365 |
self.rset = self.req.execute(self.startup_rql()) |
|
366 |
rset = self.rset |
|
367 |
for i in xrange(len(rset)): |
|
368 |
self.wview(self.id, rset, row=i, **kwargs) |
|
369 |
||
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
370 |
|
0 | 371 |
class AnyRsetView(View): |
372 |
"""base class for views applying on any non empty result sets""" |
|
707 | 373 |
__select__ = nonempty_rset() |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
374 |
|
0 | 375 |
category = 'anyrsetview' |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
376 |
|
2203
efdd3a9f9028
update to new rql get_description api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1999
diff
changeset
|
377 |
def columns_labels(self, mainindex=0, tr=True): |
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
378 |
if tr: |
2207
5fad58e603f4
fix translate argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2203
diff
changeset
|
379 |
translate = lambda val, req=self.req: display_name(req, val) |
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
380 |
else: |
2207
5fad58e603f4
fix translate argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2203
diff
changeset
|
381 |
translate = lambda val: val |
2203
efdd3a9f9028
update to new rql get_description api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1999
diff
changeset
|
382 |
# XXX [0] because of missing Union support |
efdd3a9f9028
update to new rql get_description api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1999
diff
changeset
|
383 |
rqlstdescr = self.rset.syntax_tree().get_description(mainindex, |
efdd3a9f9028
update to new rql get_description api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1999
diff
changeset
|
384 |
translate)[0] |
369
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
361
diff
changeset
|
385 |
labels = [] |
2203
efdd3a9f9028
update to new rql get_description api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1999
diff
changeset
|
386 |
for colindex, label in enumerate(rqlstdescr): |
369
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
361
diff
changeset
|
387 |
# compute column header |
2203
efdd3a9f9028
update to new rql get_description api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1999
diff
changeset
|
388 |
if label == 'Any': # find a better label |
2207
5fad58e603f4
fix translate argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2203
diff
changeset
|
389 |
label = ','.join(translate(et) |
369
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
361
diff
changeset
|
390 |
for et in self.rset.column_types(colindex)) |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
361
diff
changeset
|
391 |
labels.append(label) |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
361
diff
changeset
|
392 |
return labels |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
393 |
|
1433 | 394 |
|
0 | 395 |
# concrete template base classes ############################################## |
396 |
||
816
9cd49a910fce
kill Template class and 'templates' registry
sylvain.thenault@logilab.fr
parents:
781
diff
changeset
|
397 |
class MainTemplate(View): |
0 | 398 |
"""main template are primary access point to render a full HTML page. |
399 |
There is usually at least a regular main template and a simple fallback |
|
400 |
one to display error if the first one failed |
|
401 |
""" |
|
816
9cd49a910fce
kill Template class and 'templates' registry
sylvain.thenault@logilab.fr
parents:
781
diff
changeset
|
402 |
registered = require_group_compat(View.registered) |
0 | 403 |
|
404 |
@property |
|
405 |
def doctype(self): |
|
2558
81c8b5312f9c
move test on force-html-content-type to xhtml_browser method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2555
diff
changeset
|
406 |
if self.req.xhtml_browser(): |
1421
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
407 |
return STRICT_DOCTYPE |
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
408 |
return STRICT_DOCTYPE_NOEXT |
0 | 409 |
|
1424 | 410 |
def set_stream(self, w=None): |
411 |
if self.w is not None: |
|
0 | 412 |
return |
413 |
if w is None: |
|
414 |
if self.binary: |
|
415 |
self._stream = stream = StringIO() |
|
416 |
else: |
|
417 |
self._stream = stream = HTMLStream(self.req) |
|
418 |
w = stream.write |
|
419 |
else: |
|
420 |
stream = None |
|
421 |
self.w = w |
|
422 |
return stream |
|
423 |
||
424 |
def write_doctype(self, xmldecl=True): |
|
425 |
assert isinstance(self._stream, HTMLStream) |
|
426 |
self._stream.doctype = self.doctype |
|
427 |
if not xmldecl: |
|
428 |
self._stream.xmldecl = u'' |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
429 |
|
822 | 430 |
def linkable(self): |
431 |
return False |
|
1433 | 432 |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
433 |
# concrete component base classes ############################################# |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
434 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
435 |
class ReloadableMixIn(object): |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
436 |
"""simple mixin for reloadable parts of UI""" |
1433 | 437 |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
438 |
def user_callback(self, cb, args, msg=None, nonify=False): |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
439 |
"""register the given user callback and return an url to call it ready to be |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
440 |
inserted in html |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
441 |
""" |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
442 |
self.req.add_js('cubicweb.ajax.js') |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
443 |
if nonify: |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
444 |
_cb = cb |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
445 |
def cb(*args): |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
446 |
_cb(*args) |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
447 |
cbname = self.req.register_onetime_callback(cb, *args) |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2217
diff
changeset
|
448 |
return self.build_js(cbname, xml_escape(msg or '')) |
1433 | 449 |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
450 |
def build_update_js_call(self, cbname, msg): |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2217
diff
changeset
|
451 |
rql = xml_escape(self.rset.printable_rql()) |
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
452 |
return "javascript:userCallbackThenUpdateUI('%s', '%s', '%s', '%s', '%s', '%s')" % ( |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
453 |
cbname, self.id, rql, msg, self.__registry__, self.div_id()) |
1433 | 454 |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
455 |
def build_reload_js_call(self, cbname, msg): |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
456 |
return "javascript:userCallbackThenReloadPage('%s', '%s')" % (cbname, msg) |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
457 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
458 |
build_js = build_update_js_call # expect updatable component by default |
1433 | 459 |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
460 |
def div_id(self): |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
461 |
return '' |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
462 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
463 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
464 |
class Component(ReloadableMixIn, View): |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
465 |
"""base class for components""" |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
466 |
__registry__ = 'components' |
707 | 467 |
__select__ = yes() |
1511
514e4e53a3c7
do not set visible property by default
sylvain.thenault@logilab.fr
parents:
1498
diff
changeset
|
468 |
property_defs = {} |
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
469 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
470 |
def div_class(self): |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
471 |
return '%s %s' % (self.propval('htmlclass'), self.id) |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
472 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
473 |
def div_id(self): |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
474 |
return '%sComponent' % self.id |