author | Sandrine Ribeau <sandrine.ribeau@logilab.fr> |
Tue, 08 Dec 2009 16:50:36 +0100 | |
changeset 4045 | f4a52abb6f4f |
parent 3998 | 94cc7cad3d2d |
child 4059 | f1da1a7ef29b |
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 |
2644
100b81ba3a98
F [view] missing import
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
16 |
from rql import nodes |
0 | 17 |
|
1181
620ec8e6ae19
cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents:
1144
diff
changeset
|
18 |
from cubicweb import NotAnEntity |
1132 | 19 |
from cubicweb.selectors import yes, non_final_entity, nonempty_rset, none_rset |
2656
a93ae0f6c0ad
R [base classes] only AppObject remaning, no more AppRsetObject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2644
diff
changeset
|
20 |
from cubicweb.appobject import AppObject |
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 |
||
3782
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
37 |
cubicweb:accesskey CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
38 |
cubicweb:actualrql CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
39 |
cubicweb:dataurl CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
40 |
cubicweb:displayactions CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
41 |
cubicweb:facetName CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
42 |
cubicweb:facetargs CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
43 |
cubicweb:fallbackvid CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
44 |
cubicweb:fname CDATA #IMPLIED |
0 | 45 |
cubicweb:initfunc CDATA #IMPLIED |
46 |
cubicweb:inputid CDATA #IMPLIED |
|
47 |
cubicweb:inputname CDATA #IMPLIED |
|
3782
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
48 |
cubicweb:limit CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
49 |
cubicweb:loadtype CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
50 |
cubicweb:loadurl CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
51 |
cubicweb:maxlength CDATA #IMPLIED |
0 | 52 |
cubicweb:required CDATA #IMPLIED |
3782
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
53 |
cubicweb:rooteid CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
54 |
cubicweb:rql CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
55 |
cubicweb:size CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
56 |
cubicweb:sortvalue CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
57 |
cubicweb:target CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
58 |
cubicweb:tindex CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
59 |
cubicweb:tlunit CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
60 |
cubicweb:type CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
61 |
cubicweb:uselabel CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
62 |
cubicweb:value CDATA #IMPLIED |
0 | 63 |
cubicweb:variables CDATA #IMPLIED |
64 |
cubicweb:vid CDATA #IMPLIED |
|
3782
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
65 |
cubicweb:wdgtype 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 |
|
2656
a93ae0f6c0ad
R [base classes] only AppObject remaning, no more AppRsetObject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2644
diff
changeset
|
75 |
class View(AppObject): |
0 | 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' |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
95 |
|
0 | 96 |
templatable = True |
97 |
need_navigation = True |
|
98 |
# content_type = 'application/xhtml+xml' # text/xhtml' |
|
99 |
binary = False |
|
100 |
add_to_breadcrumbs = True |
|
101 |
category = 'view' |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
102 |
|
1143
8d097defbf2c
change the way selected/__init__ are used for AppRsetObject
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
103 |
def __init__(self, req=None, rset=None, **kwargs): |
2822
f26578339214
deprecate appobject.vreg and rename appobject instance attributes using cw_ prefix
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2819
diff
changeset
|
104 |
super(View, self).__init__(req, rset=rset, **kwargs) |
0 | 105 |
self.w = None |
106 |
||
107 |
@property |
|
108 |
def content_type(self): |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
109 |
return self._cw.html_content_type() |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
110 |
|
0 | 111 |
def set_stream(self, w=None): |
112 |
if self.w is not None: |
|
113 |
return |
|
114 |
if w is None: |
|
115 |
if self.binary: |
|
116 |
self._stream = stream = StringIO() |
|
117 |
else: |
|
118 |
self._stream = stream = UStringIO() |
|
119 |
w = stream.write |
|
120 |
else: |
|
121 |
stream = None |
|
122 |
self.w = w |
|
123 |
return stream |
|
124 |
||
125 |
# main view interface ##################################################### |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
126 |
|
1723 | 127 |
def render(self, w=None, **context): |
0 | 128 |
"""called to render a view object for a result set. |
129 |
||
130 |
This method is a dispatched to an actual method selected |
|
131 |
according to optional row and col parameters, which are locating |
|
132 |
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
|
133 |
|
1132 | 134 |
* if row is specified, `cell_call` is called |
0 | 135 |
* if none of them is supplied, the view is considered to apply on |
136 |
the whole result set (which may be None in this case), `call` is |
|
137 |
called |
|
138 |
""" |
|
1132 | 139 |
row = context.get('row') |
0 | 140 |
if row is not None: |
141 |
context.setdefault('col', 0) |
|
142 |
view_func = self.cell_call |
|
143 |
else: |
|
144 |
view_func = self.call |
|
145 |
stream = self.set_stream(w) |
|
146 |
# stream = self.set_stream(context) |
|
147 |
view_func(**context) |
|
148 |
# return stream content if we have created it |
|
149 |
if stream is not None: |
|
150 |
return self._stream.getvalue() |
|
151 |
||
2795
59965167bef3
move tal_render to View
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
152 |
def tal_render(self, template, variables): |
59965167bef3
move tal_render to View
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
153 |
"""render a precompiled page template with variables in the given |
59965167bef3
move tal_render to View
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
154 |
dictionary as context |
59965167bef3
move tal_render to View
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
155 |
""" |
59965167bef3
move tal_render to View
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
156 |
from cubicweb.ext.tal import CubicWebContext |
59965167bef3
move tal_render to View
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
157 |
context = CubicWebContext() |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
158 |
context.update({'self': self, 'rset': self.cw_rset, '_' : self._cw._, |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
159 |
'req': self._cw, 'user': self._cw.user}) |
2795
59965167bef3
move tal_render to View
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
160 |
context.update(variables) |
59965167bef3
move tal_render to View
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
161 |
output = UStringIO() |
59965167bef3
move tal_render to View
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
162 |
template.expand(context, output) |
59965167bef3
move tal_render to View
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
163 |
return output.getvalue() |
59965167bef3
move tal_render to View
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
164 |
|
2788
8d3dbe577d3a
R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2656
diff
changeset
|
165 |
dispatch = deprecated('[3.4] .dispatch is deprecated, use .render')(render) |
1723 | 166 |
|
0 | 167 |
# should default .call() method add a <div classs="section"> around each |
168 |
# rset item |
|
169 |
add_div_section = True |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
170 |
|
0 | 171 |
def call(self, **kwargs): |
172 |
"""the view is called for an entire result set, by default loop |
|
173 |
other rows of the result set and call the same view on the |
|
174 |
particular row |
|
175 |
||
176 |
Views applicable on None result sets have to override this method |
|
177 |
""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
178 |
rset = self.cw_rset |
0 | 179 |
if rset is None: |
180 |
raise NotImplementedError, self |
|
181 |
wrap = self.templatable and len(rset) > 1 and self.add_div_section |
|
3973
1c09d2fd38c5
allow to override is_primary result using an argument given to object constructor (have to be given to 'initargs' argument of .render like initargs={'is_primary': False}
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3969
diff
changeset
|
182 |
# XXX propagate self.extra_kwars? |
0 | 183 |
for i in xrange(len(rset)): |
184 |
if wrap: |
|
185 |
self.w(u'<div class="section">') |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
186 |
self.wview(self.__regid__, rset, row=i, **kwargs) |
0 | 187 |
if wrap: |
188 |
self.w(u"</div>") |
|
189 |
||
190 |
def cell_call(self, row, col, **kwargs): |
|
191 |
"""the view is called for a particular result set cell""" |
|
192 |
raise NotImplementedError, self |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
193 |
|
0 | 194 |
def linkable(self): |
195 |
"""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
|
196 |
|
0 | 197 |
by default views without title are not meant to be displayed |
198 |
""" |
|
199 |
if not getattr(self, 'title', None): |
|
200 |
return False |
|
201 |
return True |
|
202 |
||
203 |
def is_primary(self): |
|
4045
f4a52abb6f4f
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3998
diff
changeset
|
204 |
return self.cw_extra_kwargs.get('is_primary', self.__regid__ == 'primary') |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
205 |
|
0 | 206 |
def url(self): |
207 |
"""return the url associated with this view. Should not be |
|
208 |
necessary for non linkable views, but a default implementation |
|
209 |
is provided anyway. |
|
210 |
""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
211 |
rset = self.cw_rset |
2468
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
212 |
if rset is None: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
213 |
return self._cw.build_url('view', vid=self.__regid__) |
2468
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
214 |
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
|
215 |
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
|
216 |
etype = iter(coltypes).next() |
4045
f4a52abb6f4f
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3998
diff
changeset
|
217 |
if not self._cw.vreg.schema.eschema(etype).final: |
2468
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
218 |
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
|
219 |
entity = rset.get_entity(0, 0) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
220 |
return entity.absolute_url(vid=self.__regid__) |
2468
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
221 |
# 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
|
222 |
# 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
|
223 |
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
|
224 |
# 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
|
225 |
# "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
|
226 |
# True |
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
227 |
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
|
228 |
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
|
229 |
if norestriction: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
230 |
return self._cw.build_url(etype.lower(), vid=self.__regid__) |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
231 |
return self._cw.build_url('view', rql=rset.printable_rql(), vid=self.__regid__) |
0 | 232 |
|
233 |
def set_request_content_type(self): |
|
234 |
"""set the content type returned by this view""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
235 |
self._cw.set_content_type(self.content_type) |
0 | 236 |
|
237 |
# view utilities ########################################################## |
|
1433 | 238 |
|
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
|
239 |
def wview(self, __vid, rset=None, __fallback_vid=None, **kwargs): |
0 | 240 |
"""shortcut to self.view method automatically passing self.w as argument |
241 |
""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
242 |
self._cw.view(__vid, rset, __fallback_vid, w=self.w, **kwargs) |
1433 | 243 |
|
819 | 244 |
# XXX Template bw compat |
2788
8d3dbe577d3a
R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2656
diff
changeset
|
245 |
template = deprecated('[3.4] .template is deprecated, use .view')(wview) |
0 | 246 |
|
247 |
def whead(self, data): |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
248 |
self._cw.html_headers.write(data) |
0 | 249 |
|
250 |
def wdata(self, data): |
|
251 |
"""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
|
252 |
self.w(xml_escape(data)) |
0 | 253 |
|
254 |
def html_headers(self): |
|
255 |
"""return a list of html headers (eg something to be inserted between |
|
256 |
<head> and </head> of the returned page |
|
257 |
||
258 |
by default return a meta tag to disable robot indexation of the page |
|
259 |
""" |
|
260 |
return [NOINDEX] |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
261 |
|
0 | 262 |
def page_title(self): |
263 |
"""returns a title according to the result set - used for the |
|
264 |
title in the HTML header |
|
265 |
""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
266 |
vtitle = self._cw.form.get('vtitle') |
0 | 267 |
if vtitle: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
268 |
return self._cw._(vtitle) |
0 | 269 |
# class defined title will only be used if the resulting title doesn't |
270 |
# seem clear enough |
|
271 |
vtitle = getattr(self, 'title', None) or u'' |
|
272 |
if vtitle: |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
273 |
vtitle = self._cw._(vtitle) |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
274 |
rset = self.cw_rset |
0 | 275 |
if rset and rset.rowcount: |
276 |
if rset.rowcount == 1: |
|
277 |
try: |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
278 |
entity = rset.complete_entity(0, 0) |
0 | 279 |
# use long_title to get context information if any |
280 |
clabel = entity.dc_long_title() |
|
281 |
except NotAnEntity: |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
282 |
clabel = display_name(self._cw, rset.description[0][0]) |
0 | 283 |
clabel = u'%s (%s)' % (clabel, vtitle) |
284 |
else : |
|
285 |
etypes = rset.column_types(0) |
|
286 |
if len(etypes) == 1: |
|
287 |
etype = iter(etypes).next() |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
288 |
clabel = display_name(self._cw, etype, 'plural') |
0 | 289 |
else : |
290 |
clabel = u'#[*] (%s)' % vtitle |
|
291 |
else: |
|
292 |
clabel = vtitle |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
293 |
return u'%s (%s)' % (clabel, self._cw.property_value('ui.site-title')) |
0 | 294 |
|
295 |
def output_url_builder( self, name, url, args ): |
|
296 |
self.w(u'<script language="JavaScript"><!--\n' \ |
|
297 |
u'function %s( %s ) {\n' % (name, ','.join(args) ) ) |
|
298 |
url_parts = url.split("%s") |
|
299 |
self.w(u' url="%s"' % url_parts[0] ) |
|
300 |
for arg, part in zip(args, url_parts[1:]): |
|
301 |
self.w(u'+str(%s)' % arg ) |
|
302 |
if part: |
|
303 |
self.w(u'+"%s"' % part) |
|
304 |
self.w('\n document.window.href=url;\n') |
|
305 |
self.w('}\n-->\n</script>\n') |
|
306 |
||
307 |
def create_url(self, etype, **kwargs): |
|
308 |
""" return the url of the entity creation form for a given entity type""" |
|
3890
d7a270f50f54
backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
309 |
return self._cw.build_url('add/%s' % etype, **kwargs) |
0 | 310 |
|
3809
1b9b51ee543e
[web ui] PrimaryView.render_attributes now uses a table
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3782
diff
changeset
|
311 |
def field(self, label, value, row=True, show_label=True, w=None, tr=True, table=False): |
1b9b51ee543e
[web ui] PrimaryView.render_attributes now uses a table
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3782
diff
changeset
|
312 |
"""read-only field""" |
0 | 313 |
if w is None: |
314 |
w = self.w |
|
3809
1b9b51ee543e
[web ui] PrimaryView.render_attributes now uses a table
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3782
diff
changeset
|
315 |
if table: |
1b9b51ee543e
[web ui] PrimaryView.render_attributes now uses a table
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3782
diff
changeset
|
316 |
w(u'<tr class="entityfield">') |
1b9b51ee543e
[web ui] PrimaryView.render_attributes now uses a table
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3782
diff
changeset
|
317 |
else: |
1b9b51ee543e
[web ui] PrimaryView.render_attributes now uses a table
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3782
diff
changeset
|
318 |
w(u'<div class="entityfield">') |
3326
e3089ceee1ea
don't show label if no label given
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3010
diff
changeset
|
319 |
if show_label and label: |
0 | 320 |
if tr: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
321 |
label = display_name(self._cw, label) |
3809
1b9b51ee543e
[web ui] PrimaryView.render_attributes now uses a table
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3782
diff
changeset
|
322 |
if table: |
1b9b51ee543e
[web ui] PrimaryView.render_attributes now uses a table
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3782
diff
changeset
|
323 |
w(u'<th>%s</th>' % label) |
1b9b51ee543e
[web ui] PrimaryView.render_attributes now uses a table
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3782
diff
changeset
|
324 |
else: |
3839
84c9aab84d8c
missing space between label and value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3809
diff
changeset
|
325 |
w(u'<span>%s</span> ' % label) |
3809
1b9b51ee543e
[web ui] PrimaryView.render_attributes now uses a table
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3782
diff
changeset
|
326 |
if table: |
3969
970a748684b5
when fields are displayed in a table and label should'nt be displayed, set colspan to 2 on the field value cell
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3839
diff
changeset
|
327 |
if not (show_label and label): |
970a748684b5
when fields are displayed in a table and label should'nt be displayed, set colspan to 2 on the field value cell
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3839
diff
changeset
|
328 |
w(u'<td colspan="2">%s</td></tr>' % value) |
970a748684b5
when fields are displayed in a table and label should'nt be displayed, set colspan to 2 on the field value cell
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3839
diff
changeset
|
329 |
else: |
970a748684b5
when fields are displayed in a table and label should'nt be displayed, set colspan to 2 on the field value cell
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3839
diff
changeset
|
330 |
w(u'<td>%s</td></tr>' % value) |
3809
1b9b51ee543e
[web ui] PrimaryView.render_attributes now uses a table
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3782
diff
changeset
|
331 |
else: |
1b9b51ee543e
[web ui] PrimaryView.render_attributes now uses a table
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3782
diff
changeset
|
332 |
w(u'<span>%s</span></div>' % value) |
1433 | 333 |
|
0 | 334 |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
335 |
|
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
336 |
# concrete views base classes ################################################# |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
337 |
|
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
338 |
class EntityView(View): |
843 | 339 |
"""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
|
340 |
__select__ = non_final_entity() |
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
341 |
category = 'entityview' |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
342 |
|
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
343 |
|
0 | 344 |
class StartupView(View): |
843 | 345 |
"""base class for views which doesn't need a particular result set to be |
346 |
displayed (so they can always be displayed !) |
|
0 | 347 |
""" |
707 | 348 |
__select__ = none_rset() |
1433 | 349 |
|
0 | 350 |
category = 'startupview' |
1433 | 351 |
|
0 | 352 |
def html_headers(self): |
353 |
"""return a list of html headers (eg something to be inserted between |
|
354 |
<head> and </head> of the returned page |
|
355 |
||
356 |
by default startup views are indexed |
|
357 |
""" |
|
358 |
return [] |
|
359 |
||
360 |
||
361 |
class EntityStartupView(EntityView): |
|
362 |
"""base class for entity views which may also be applied to None |
|
363 |
result set (usually a default rql is provided by the view class) |
|
364 |
""" |
|
781
323656dd85a9
fix import, use non_final_entity instead of implements('Any')
sylvain.thenault@logilab.fr
parents:
757
diff
changeset
|
365 |
__select__ = none_rset() | non_final_entity() |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
366 |
|
0 | 367 |
default_rql = None |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
368 |
|
2418
8f06e4f02733
optional rset
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
369 |
def __init__(self, req, rset=None, **kwargs): |
2890
fdcb8a2bb6eb
fix __init__ parameters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
370 |
super(EntityStartupView, self).__init__(req, rset=rset, **kwargs) |
0 | 371 |
if rset is None: |
372 |
# this instance is not in the "entityview" category |
|
373 |
self.category = 'startupview' |
|
374 |
||
375 |
def startup_rql(self): |
|
873 | 376 |
"""return some rql to be executed if the result set is None""" |
0 | 377 |
return self.default_rql |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
378 |
|
0 | 379 |
def call(self, **kwargs): |
873 | 380 |
"""override call to execute rql returned by the .startup_rql method if |
381 |
necessary |
|
0 | 382 |
""" |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
383 |
rset = self.cw_rset |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
384 |
if rset is None: |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
385 |
rset = self.cw_rset = self._cw.execute(self.startup_rql()) |
0 | 386 |
for i in xrange(len(rset)): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
387 |
self.wview(self.__regid__, rset, row=i, **kwargs) |
0 | 388 |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
389 |
|
0 | 390 |
class AnyRsetView(View): |
391 |
"""base class for views applying on any non empty result sets""" |
|
707 | 392 |
__select__ = nonempty_rset() |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
393 |
|
0 | 394 |
category = 'anyrsetview' |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
395 |
|
2203
efdd3a9f9028
update to new rql get_description api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1999
diff
changeset
|
396 |
def columns_labels(self, mainindex=0, tr=True): |
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
397 |
if tr: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
398 |
translate = lambda val, req=self._cw: display_name(req, val) |
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
399 |
else: |
2207
5fad58e603f4
fix translate argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2203
diff
changeset
|
400 |
translate = lambda val: val |
2203
efdd3a9f9028
update to new rql get_description api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1999
diff
changeset
|
401 |
# XXX [0] because of missing Union support |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
402 |
rqlstdescr = self.cw_rset.syntax_tree().get_description(mainindex, |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
403 |
translate)[0] |
369
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
361
diff
changeset
|
404 |
labels = [] |
2203
efdd3a9f9028
update to new rql get_description api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1999
diff
changeset
|
405 |
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
|
406 |
# compute column header |
2203
efdd3a9f9028
update to new rql get_description api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1999
diff
changeset
|
407 |
if label == 'Any': # find a better label |
2207
5fad58e603f4
fix translate argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2203
diff
changeset
|
408 |
label = ','.join(translate(et) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
409 |
for et in self.cw_rset.column_types(colindex)) |
369
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
361
diff
changeset
|
410 |
labels.append(label) |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
361
diff
changeset
|
411 |
return labels |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
412 |
|
1433 | 413 |
|
0 | 414 |
# concrete template base classes ############################################## |
415 |
||
816
9cd49a910fce
kill Template class and 'templates' registry
sylvain.thenault@logilab.fr
parents:
781
diff
changeset
|
416 |
class MainTemplate(View): |
0 | 417 |
"""main template are primary access point to render a full HTML page. |
418 |
There is usually at least a regular main template and a simple fallback |
|
419 |
one to display error if the first one failed |
|
420 |
""" |
|
421 |
||
422 |
@property |
|
423 |
def doctype(self): |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
424 |
if self._cw.xhtml_browser(): |
1421
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
425 |
return STRICT_DOCTYPE |
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
426 |
return STRICT_DOCTYPE_NOEXT |
0 | 427 |
|
1424 | 428 |
def set_stream(self, w=None): |
429 |
if self.w is not None: |
|
0 | 430 |
return |
431 |
if w is None: |
|
432 |
if self.binary: |
|
433 |
self._stream = stream = StringIO() |
|
434 |
else: |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
435 |
self._stream = stream = HTMLStream(self._cw) |
0 | 436 |
w = stream.write |
437 |
else: |
|
438 |
stream = None |
|
439 |
self.w = w |
|
440 |
return stream |
|
441 |
||
442 |
def write_doctype(self, xmldecl=True): |
|
443 |
assert isinstance(self._stream, HTMLStream) |
|
444 |
self._stream.doctype = self.doctype |
|
445 |
if not xmldecl: |
|
446 |
self._stream.xmldecl = u'' |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
447 |
|
822 | 448 |
def linkable(self): |
449 |
return False |
|
1433 | 450 |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
451 |
# concrete component base classes ############################################# |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
452 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
453 |
class ReloadableMixIn(object): |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
454 |
"""simple mixin for reloadable parts of UI""" |
1433 | 455 |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
456 |
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
|
457 |
"""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
|
458 |
inserted in html |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
459 |
""" |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
460 |
self._cw.add_js('cubicweb.ajax.js') |
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
461 |
if nonify: |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
462 |
_cb = cb |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
463 |
def cb(*args): |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
464 |
_cb(*args) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
465 |
cbname = self._cw.register_onetime_callback(cb, *args) |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2217
diff
changeset
|
466 |
return self.build_js(cbname, xml_escape(msg or '')) |
1433 | 467 |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
468 |
def build_update_js_call(self, cbname, msg): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
469 |
rql = xml_escape(self.cw_rset.printable_rql()) |
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
470 |
return "javascript:userCallbackThenUpdateUI('%s', '%s', '%s', '%s', '%s', '%s')" % ( |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
471 |
cbname, self.__regid__, rql, msg, self.__registry__, self.div_id()) |
1433 | 472 |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
473 |
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
|
474 |
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
|
475 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
476 |
build_js = build_update_js_call # expect updatable component by default |
1433 | 477 |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
478 |
def div_id(self): |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
479 |
return '' |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
480 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
481 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
482 |
class Component(ReloadableMixIn, View): |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
483 |
"""base class for components""" |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
484 |
__registry__ = 'components' |
707 | 485 |
__select__ = yes() |
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
486 |
|
3010
684a0c546774
[ui props] drop htmlclass as a persistent property, this is definitly useless/unusable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
487 |
# XXX huummm, much probably useless |
684a0c546774
[ui props] drop htmlclass as a persistent property, this is definitly useless/unusable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2656
diff
changeset
|
488 |
htmlclass = 'mainRelated' |
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
489 |
def div_class(self): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
490 |
return '%s %s' % (self.htmlclass, self.__regid__) |
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
491 |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
492 |
# XXX a generic '%s%s' % (self.__regid__, self.__registry__.capitalize()) would probably be nicer |
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
493 |
def div_id(self): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
494 |
return '%sComponent' % self.__regid__ |