author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 01 Oct 2010 15:45:38 +0200 | |
changeset 6378 | 5a86a0c147bd |
parent 6140 | 65a619eb31c4 |
child 6458 | e4eb95d86172 |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5389
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
5528 | 18 |
"""abstract views and templates classes for CubicWeb web client""" |
0 | 19 |
|
20 |
__docformat__ = "restructuredtext en" |
|
1999
b9a8f5995658
obsolete methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1985
diff
changeset
|
21 |
_ = unicode |
0 | 22 |
|
23 |
from cStringIO import StringIO |
|
4244
2c3de1953d00
rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
24 |
from warnings import warn |
0 | 25 |
|
2613
5e19c2bb370e
R [all] logilab.common 0.44 provides only deprecated
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2558
diff
changeset
|
26 |
from logilab.common.deprecation import deprecated |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2217
diff
changeset
|
27 |
from logilab.mtconverter import xml_escape |
4058
b3578583b57b
properly escape arguments of user callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3973
diff
changeset
|
28 |
|
2644
100b81ba3a98
F [view] missing import
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2613
diff
changeset
|
29 |
from rql import nodes |
0 | 30 |
|
1181
620ec8e6ae19
cleanup, various fix to get something working
sylvain.thenault@logilab.fr
parents:
1144
diff
changeset
|
31 |
from cubicweb import NotAnEntity |
1132 | 32 |
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
|
33 |
from cubicweb.appobject import AppObject |
708
b4728112625f
move utils from cw.common to cw
sylvain.thenault@logilab.fr
parents:
707
diff
changeset
|
34 |
from cubicweb.utils import UStringIO, HTMLStream |
6044
9e48ebd5400c
[view] use the new magic js object to generate javascript func call for auto reload user callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
35 |
from cubicweb.uilib import domid, js |
2217
3a8cee8f8778
explicit display_name import
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2207
diff
changeset
|
36 |
from cubicweb.schema import display_name |
5725
b5d595b66c35
[adapters] enhanced warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5715
diff
changeset
|
37 |
from cubicweb.vregistry import classid |
633 | 38 |
|
0 | 39 |
# robots control |
40 |
NOINDEX = u'<meta name="ROBOTS" content="NOINDEX" />' |
|
41 |
NOFOLLOW = u'<meta name="ROBOTS" content="NOFOLLOW" />' |
|
42 |
||
43 |
CW_XHTML_EXTENSIONS = '''[ |
|
44 |
<!ATTLIST html xmlns:cubicweb CDATA #FIXED \'http://www.logilab.org/2008/cubicweb\' > |
|
45 |
||
46 |
<!ENTITY % coreattrs |
|
522
385ce5e0b30b
cubicweb__loadurl -> cubicweb:lazyloadurl
sylvain.thenault@logilab.fr
parents:
497
diff
changeset
|
47 |
"id ID #IMPLIED |
385ce5e0b30b
cubicweb__loadurl -> cubicweb:lazyloadurl
sylvain.thenault@logilab.fr
parents:
497
diff
changeset
|
48 |
class CDATA #IMPLIED |
385ce5e0b30b
cubicweb__loadurl -> cubicweb:lazyloadurl
sylvain.thenault@logilab.fr
parents:
497
diff
changeset
|
49 |
style CDATA #IMPLIED |
0 | 50 |
title CDATA #IMPLIED |
51 |
||
3782
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
52 |
cubicweb:accesskey CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
53 |
cubicweb:actualrql CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
54 |
cubicweb:dataurl CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
55 |
cubicweb:facetName CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
56 |
cubicweb:facetargs CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
57 |
cubicweb:fallbackvid CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
58 |
cubicweb:fname CDATA #IMPLIED |
0 | 59 |
cubicweb:initfunc CDATA #IMPLIED |
60 |
cubicweb:inputid CDATA #IMPLIED |
|
61 |
cubicweb:inputname CDATA #IMPLIED |
|
3782
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
62 |
cubicweb:limit CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
63 |
cubicweb:loadtype CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
64 |
cubicweb:loadurl CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
65 |
cubicweb:maxlength CDATA #IMPLIED |
0 | 66 |
cubicweb:required CDATA #IMPLIED |
3782
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
67 |
cubicweb:rooteid CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
68 |
cubicweb:rql CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
69 |
cubicweb:size CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
70 |
cubicweb:sortvalue CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
71 |
cubicweb:target CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
72 |
cubicweb:tindex CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
73 |
cubicweb:tlunit CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
74 |
cubicweb:type CDATA #IMPLIED |
5533
5cb121d4f02f
[dtd] add unselimg attribute use by widget for HasRelationFacet
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5528
diff
changeset
|
75 |
cubicweb:unselimg CDATA #IMPLIED |
3782
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
76 |
cubicweb:uselabel CDATA #IMPLIED |
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
77 |
cubicweb:value CDATA #IMPLIED |
0 | 78 |
cubicweb:variables CDATA #IMPLIED |
79 |
cubicweb:vid CDATA #IMPLIED |
|
3782
c69fe7493b91
[cleanup] reorder cubicweb namespace attributes declaration
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3689
diff
changeset
|
80 |
cubicweb:wdgtype CDATA #IMPLIED |
0 | 81 |
"> ] ''' |
82 |
||
1421
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
83 |
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
|
84 |
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
|
85 |
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
|
86 |
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 | 87 |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
88 |
# base view object ############################################################ |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
89 |
|
2656
a93ae0f6c0ad
R [base classes] only AppObject remaning, no more AppRsetObject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2644
diff
changeset
|
90 |
class View(AppObject): |
0 | 91 |
"""abstract view class, used as base for every renderable object such |
92 |
as views, templates, some components...web |
|
93 |
||
94 |
A view is instantiated to render a [part of a] result set. View |
|
95 |
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
|
96 |
|
0 | 97 |
* `templatable` indicates if the view may be embeded in a main |
98 |
template or if it has to be rendered standalone (i.e. XML for |
|
99 |
instance) |
|
100 |
* if the view is not templatable, it should set the `content_type` class |
|
101 |
attribute to the correct MIME type (text/xhtml by default) |
|
102 |
* the `category` attribute may be used in the interface to regroup related |
|
103 |
objects together |
|
104 |
||
4702
d9e51820d0c2
fix docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4459
diff
changeset
|
105 |
At instantiation time, the standard `_cw`, and `cw_rset` attributes are |
d9e51820d0c2
fix docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4459
diff
changeset
|
106 |
added and the `w` attribute will be set at rendering time to a write |
d9e51820d0c2
fix docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4459
diff
changeset
|
107 |
function to use. |
0 | 108 |
""" |
816
9cd49a910fce
kill Template class and 'templates' registry
sylvain.thenault@logilab.fr
parents:
781
diff
changeset
|
109 |
__registry__ = 'views' |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
110 |
|
0 | 111 |
templatable = True |
112 |
# content_type = 'application/xhtml+xml' # text/xhtml' |
|
113 |
binary = False |
|
114 |
add_to_breadcrumbs = True |
|
115 |
category = 'view' |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
116 |
|
4244
2c3de1953d00
rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
117 |
@property |
2c3de1953d00
rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
118 |
@deprecated('[3.6] need_navigation is deprecated, use .paginable') |
2c3de1953d00
rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
119 |
def need_navigation(self): |
2c3de1953d00
rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
120 |
return True |
2c3de1953d00
rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
121 |
|
2c3de1953d00
rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
122 |
@property |
2c3de1953d00
rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
123 |
def paginable(self): |
2c3de1953d00
rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
124 |
if not isinstance(self.__class__.need_navigation, property): |
2c3de1953d00
rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
125 |
warn('[3.6] %s.need_navigation is deprecated, use .paginable' |
4456 | 126 |
% self.__class__, DeprecationWarning) |
4244
2c3de1953d00
rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
127 |
return self.need_navigation |
2c3de1953d00
rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
128 |
return True |
2c3de1953d00
rename need_navigation into paginable
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
129 |
|
1143
8d097defbf2c
change the way selected/__init__ are used for AppRsetObject
sylvain.thenault@logilab.fr
parents:
1132
diff
changeset
|
130 |
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
|
131 |
super(View, self).__init__(req, rset=rset, **kwargs) |
0 | 132 |
self.w = None |
133 |
||
134 |
@property |
|
135 |
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
|
136 |
return self._cw.html_content_type() |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
137 |
|
0 | 138 |
def set_stream(self, w=None): |
139 |
if self.w is not None: |
|
140 |
return |
|
141 |
if w is None: |
|
142 |
if self.binary: |
|
143 |
self._stream = stream = StringIO() |
|
144 |
else: |
|
145 |
self._stream = stream = UStringIO() |
|
146 |
w = stream.write |
|
147 |
else: |
|
148 |
stream = None |
|
149 |
self.w = w |
|
150 |
return stream |
|
151 |
||
152 |
# main view interface ##################################################### |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
153 |
|
1723 | 154 |
def render(self, w=None, **context): |
0 | 155 |
"""called to render a view object for a result set. |
156 |
||
157 |
This method is a dispatched to an actual method selected |
|
158 |
according to optional row and col parameters, which are locating |
|
159 |
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
|
160 |
|
1132 | 161 |
* if row is specified, `cell_call` is called |
0 | 162 |
* if none of them is supplied, the view is considered to apply on |
163 |
the whole result set (which may be None in this case), `call` is |
|
164 |
called |
|
165 |
""" |
|
1132 | 166 |
row = context.get('row') |
0 | 167 |
if row is not None: |
168 |
context.setdefault('col', 0) |
|
169 |
view_func = self.cell_call |
|
170 |
else: |
|
171 |
view_func = self.call |
|
172 |
stream = self.set_stream(w) |
|
173 |
# stream = self.set_stream(context) |
|
174 |
view_func(**context) |
|
175 |
# return stream content if we have created it |
|
176 |
if stream is not None: |
|
177 |
return self._stream.getvalue() |
|
178 |
||
2795
59965167bef3
move tal_render to View
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
179 |
def tal_render(self, template, variables): |
59965167bef3
move tal_render to View
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
180 |
"""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
|
181 |
dictionary as context |
59965167bef3
move tal_render to View
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
182 |
""" |
59965167bef3
move tal_render to View
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
183 |
from cubicweb.ext.tal import CubicWebContext |
59965167bef3
move tal_render to View
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
184 |
context = CubicWebContext() |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
185 |
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
|
186 |
'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
|
187 |
context.update(variables) |
59965167bef3
move tal_render to View
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
188 |
output = UStringIO() |
59965167bef3
move tal_render to View
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
189 |
template.expand(context, output) |
59965167bef3
move tal_render to View
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
190 |
return output.getvalue() |
59965167bef3
move tal_render to View
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2788
diff
changeset
|
191 |
|
2788
8d3dbe577d3a
R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2656
diff
changeset
|
192 |
dispatch = deprecated('[3.4] .dispatch is deprecated, use .render')(render) |
1723 | 193 |
|
0 | 194 |
# should default .call() method add a <div classs="section"> around each |
195 |
# rset item |
|
196 |
add_div_section = True |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
197 |
|
0 | 198 |
def call(self, **kwargs): |
199 |
"""the view is called for an entire result set, by default loop |
|
200 |
other rows of the result set and call the same view on the |
|
201 |
particular row |
|
202 |
||
203 |
Views applicable on None result sets have to override this method |
|
204 |
""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
205 |
rset = self.cw_rset |
0 | 206 |
if rset is None: |
4141
637170c17643
improve error message
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
4063
diff
changeset
|
207 |
raise NotImplementedError, (self, "an rset is required") |
0 | 208 |
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
|
209 |
# XXX propagate self.extra_kwars? |
0 | 210 |
for i in xrange(len(rset)): |
211 |
if wrap: |
|
212 |
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
|
213 |
self.wview(self.__regid__, rset, row=i, **kwargs) |
0 | 214 |
if wrap: |
215 |
self.w(u"</div>") |
|
216 |
||
217 |
def cell_call(self, row, col, **kwargs): |
|
218 |
"""the view is called for a particular result set cell""" |
|
219 |
raise NotImplementedError, self |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
220 |
|
0 | 221 |
def linkable(self): |
222 |
"""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
|
223 |
|
0 | 224 |
by default views without title are not meant to be displayed |
225 |
""" |
|
226 |
if not getattr(self, 'title', None): |
|
227 |
return False |
|
228 |
return True |
|
229 |
||
230 |
def is_primary(self): |
|
4045
f4a52abb6f4f
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3998
diff
changeset
|
231 |
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
|
232 |
|
0 | 233 |
def url(self): |
234 |
"""return the url associated with this view. Should not be |
|
235 |
necessary for non linkable views, but a default implementation |
|
236 |
is provided anyway. |
|
237 |
""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
238 |
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
|
239 |
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
|
240 |
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
|
241 |
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
|
242 |
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
|
243 |
etype = iter(coltypes).next() |
4045
f4a52abb6f4f
cw 3.6 api update
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents:
3998
diff
changeset
|
244 |
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
|
245 |
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
|
246 |
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
|
247 |
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
|
248 |
# 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
|
249 |
# 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
|
250 |
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
|
251 |
# 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
|
252 |
# "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
|
253 |
# True |
93853b6f3bad
move all view.url() logic in a single implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2418
diff
changeset
|
254 |
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
|
255 |
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
|
256 |
if norestriction: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
257 |
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
|
258 |
return self._cw.build_url('view', rql=rset.printable_rql(), vid=self.__regid__) |
0 | 259 |
|
260 |
def set_request_content_type(self): |
|
261 |
"""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
|
262 |
self._cw.set_content_type(self.content_type) |
0 | 263 |
|
264 |
# view utilities ########################################################## |
|
1433 | 265 |
|
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
|
266 |
def wview(self, __vid, rset=None, __fallback_vid=None, **kwargs): |
0 | 267 |
"""shortcut to self.view method automatically passing self.w as argument |
268 |
""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
269 |
self._cw.view(__vid, rset, __fallback_vid, w=self.w, **kwargs) |
1433 | 270 |
|
819 | 271 |
# XXX Template bw compat |
2788
8d3dbe577d3a
R put version info in deprecation warnings
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2656
diff
changeset
|
272 |
template = deprecated('[3.4] .template is deprecated, use .view')(wview) |
0 | 273 |
|
274 |
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
|
275 |
self._cw.html_headers.write(data) |
0 | 276 |
|
277 |
def wdata(self, data): |
|
278 |
"""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
|
279 |
self.w(xml_escape(data)) |
0 | 280 |
|
281 |
def html_headers(self): |
|
282 |
"""return a list of html headers (eg something to be inserted between |
|
283 |
<head> and </head> of the returned page |
|
284 |
||
285 |
by default return a meta tag to disable robot indexation of the page |
|
286 |
""" |
|
287 |
return [NOINDEX] |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
288 |
|
0 | 289 |
def page_title(self): |
290 |
"""returns a title according to the result set - used for the |
|
291 |
title in the HTML header |
|
292 |
""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
293 |
vtitle = self._cw.form.get('vtitle') |
0 | 294 |
if vtitle: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
295 |
return self._cw._(vtitle) |
0 | 296 |
# class defined title will only be used if the resulting title doesn't |
297 |
# seem clear enough |
|
298 |
vtitle = getattr(self, 'title', None) or u'' |
|
299 |
if vtitle: |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
300 |
vtitle = self._cw._(vtitle) |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
301 |
rset = self.cw_rset |
0 | 302 |
if rset and rset.rowcount: |
303 |
if rset.rowcount == 1: |
|
304 |
try: |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
305 |
entity = rset.complete_entity(0, 0) |
0 | 306 |
# use long_title to get context information if any |
307 |
clabel = entity.dc_long_title() |
|
308 |
except NotAnEntity: |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
309 |
clabel = display_name(self._cw, rset.description[0][0]) |
0 | 310 |
clabel = u'%s (%s)' % (clabel, vtitle) |
311 |
else : |
|
312 |
etypes = rset.column_types(0) |
|
313 |
if len(etypes) == 1: |
|
314 |
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
|
315 |
clabel = display_name(self._cw, etype, 'plural') |
0 | 316 |
else : |
317 |
clabel = u'#[*] (%s)' % vtitle |
|
318 |
else: |
|
319 |
clabel = vtitle |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
320 |
return u'%s (%s)' % (clabel, self._cw.property_value('ui.site-title')) |
0 | 321 |
|
6039
6e84db1b3e44
[view] deprecate create_url in favor of a cw_create_url class method on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5725
diff
changeset
|
322 |
@deprecated('[3.10] use vreg["etypes"].etype_class(etype).cw_create_url(req)') |
0 | 323 |
def create_url(self, etype, **kwargs): |
324 |
""" return the url of the entity creation form for a given entity type""" |
|
6039
6e84db1b3e44
[view] deprecate create_url in favor of a cw_create_url class method on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5725
diff
changeset
|
325 |
return self._cw.vreg["etypes"].etype_class(etype).cw_create_url( |
6e84db1b3e44
[view] deprecate create_url in favor of a cw_create_url class method on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5725
diff
changeset
|
326 |
self._cw, **kwargs) |
0 | 327 |
|
6038
5c5d819e8e93
small cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5725
diff
changeset
|
328 |
def field(self, label, value, row=True, show_label=True, w=None, tr=True, |
5c5d819e8e93
small cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5725
diff
changeset
|
329 |
table=False): |
3809
1b9b51ee543e
[web ui] PrimaryView.render_attributes now uses a table
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3782
diff
changeset
|
330 |
"""read-only field""" |
0 | 331 |
if w is None: |
332 |
w = self.w |
|
3809
1b9b51ee543e
[web ui] PrimaryView.render_attributes now uses a table
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3782
diff
changeset
|
333 |
if table: |
1b9b51ee543e
[web ui] PrimaryView.render_attributes now uses a table
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3782
diff
changeset
|
334 |
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
|
335 |
else: |
1b9b51ee543e
[web ui] PrimaryView.render_attributes now uses a table
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3782
diff
changeset
|
336 |
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
|
337 |
if show_label and label: |
0 | 338 |
if tr: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
339 |
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
|
340 |
if table: |
1b9b51ee543e
[web ui] PrimaryView.render_attributes now uses a table
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3782
diff
changeset
|
341 |
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
|
342 |
else: |
3839
84c9aab84d8c
missing space between label and value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3809
diff
changeset
|
343 |
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
|
344 |
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
|
345 |
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
|
346 |
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
|
347 |
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
|
348 |
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
|
349 |
else: |
1b9b51ee543e
[web ui] PrimaryView.render_attributes now uses a table
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3782
diff
changeset
|
350 |
w(u'<span>%s</span></div>' % value) |
1433 | 351 |
|
0 | 352 |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
353 |
|
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
354 |
# concrete views base classes ################################################# |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
355 |
|
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
356 |
class EntityView(View): |
843 | 357 |
"""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
|
358 |
__select__ = non_final_entity() |
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
359 |
category = 'entityview' |
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
360 |
|
5715
2c3e83817a8e
[view] add a new entity_call method to entity view protocol, allowing some to work with not yet created entities. Also, start considering 'eid' form parameters where we only consider 'rql', so we can move on bloquing arbitrary rql inputs (more to do on this...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5679
diff
changeset
|
361 |
def call(self, **kwargs): |
2c3e83817a8e
[view] add a new entity_call method to entity view protocol, allowing some to work with not yet created entities. Also, start considering 'eid' form parameters where we only consider 'rql', so we can move on bloquing arbitrary rql inputs (more to do on this...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5679
diff
changeset
|
362 |
if self.cw_rset is None: |
2c3e83817a8e
[view] add a new entity_call method to entity view protocol, allowing some to work with not yet created entities. Also, start considering 'eid' form parameters where we only consider 'rql', so we can move on bloquing arbitrary rql inputs (more to do on this...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5679
diff
changeset
|
363 |
self.entity_call(self.cw_extra_kwargs.pop('entity')) |
2c3e83817a8e
[view] add a new entity_call method to entity view protocol, allowing some to work with not yet created entities. Also, start considering 'eid' form parameters where we only consider 'rql', so we can move on bloquing arbitrary rql inputs (more to do on this...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5679
diff
changeset
|
364 |
else: |
2c3e83817a8e
[view] add a new entity_call method to entity view protocol, allowing some to work with not yet created entities. Also, start considering 'eid' form parameters where we only consider 'rql', so we can move on bloquing arbitrary rql inputs (more to do on this...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5679
diff
changeset
|
365 |
super(EntityView, self).call(**kwargs) |
2c3e83817a8e
[view] add a new entity_call method to entity view protocol, allowing some to work with not yet created entities. Also, start considering 'eid' form parameters where we only consider 'rql', so we can move on bloquing arbitrary rql inputs (more to do on this...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5679
diff
changeset
|
366 |
|
2c3e83817a8e
[view] add a new entity_call method to entity view protocol, allowing some to work with not yet created entities. Also, start considering 'eid' form parameters where we only consider 'rql', so we can move on bloquing arbitrary rql inputs (more to do on this...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5679
diff
changeset
|
367 |
def cell_call(self, row, col, **kwargs): |
2c3e83817a8e
[view] add a new entity_call method to entity view protocol, allowing some to work with not yet created entities. Also, start considering 'eid' form parameters where we only consider 'rql', so we can move on bloquing arbitrary rql inputs (more to do on this...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5679
diff
changeset
|
368 |
self.entity_call(self.cw_rset.get_entity(row, col), **kwargs) |
2c3e83817a8e
[view] add a new entity_call method to entity view protocol, allowing some to work with not yet created entities. Also, start considering 'eid' form parameters where we only consider 'rql', so we can move on bloquing arbitrary rql inputs (more to do on this...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5679
diff
changeset
|
369 |
|
2c3e83817a8e
[view] add a new entity_call method to entity view protocol, allowing some to work with not yet created entities. Also, start considering 'eid' form parameters where we only consider 'rql', so we can move on bloquing arbitrary rql inputs (more to do on this...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5679
diff
changeset
|
370 |
def entity_call(self, entity, **kwargs): |
2c3e83817a8e
[view] add a new entity_call method to entity view protocol, allowing some to work with not yet created entities. Also, start considering 'eid' form parameters where we only consider 'rql', so we can move on bloquing arbitrary rql inputs (more to do on this...)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5679
diff
changeset
|
371 |
raise NotImplementedError() |
640
8e64f12be69c
drop EntityAction usage in cw, upgrade rql_condition and friends
sylvain.thenault@logilab.fr
parents:
635
diff
changeset
|
372 |
|
0 | 373 |
class StartupView(View): |
843 | 374 |
"""base class for views which doesn't need a particular result set to be |
375 |
displayed (so they can always be displayed !) |
|
0 | 376 |
""" |
707 | 377 |
__select__ = none_rset() |
1433 | 378 |
|
0 | 379 |
category = 'startupview' |
1433 | 380 |
|
0 | 381 |
def html_headers(self): |
382 |
"""return a list of html headers (eg something to be inserted between |
|
383 |
<head> and </head> of the returned page |
|
384 |
||
385 |
by default startup views are indexed |
|
386 |
""" |
|
387 |
return [] |
|
388 |
||
389 |
||
390 |
class EntityStartupView(EntityView): |
|
391 |
"""base class for entity views which may also be applied to None |
|
392 |
result set (usually a default rql is provided by the view class) |
|
393 |
""" |
|
781
323656dd85a9
fix import, use non_final_entity instead of implements('Any')
sylvain.thenault@logilab.fr
parents:
757
diff
changeset
|
394 |
__select__ = none_rset() | non_final_entity() |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
395 |
|
0 | 396 |
default_rql = None |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
397 |
|
2418
8f06e4f02733
optional rset
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2312
diff
changeset
|
398 |
def __init__(self, req, rset=None, **kwargs): |
2890
fdcb8a2bb6eb
fix __init__ parameters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2822
diff
changeset
|
399 |
super(EntityStartupView, self).__init__(req, rset=rset, **kwargs) |
0 | 400 |
if rset is None: |
401 |
# this instance is not in the "entityview" category |
|
402 |
self.category = 'startupview' |
|
403 |
||
404 |
def startup_rql(self): |
|
873 | 405 |
"""return some rql to be executed if the result set is None""" |
0 | 406 |
return self.default_rql |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
407 |
|
0 | 408 |
def call(self, **kwargs): |
873 | 409 |
"""override call to execute rql returned by the .startup_rql method if |
410 |
necessary |
|
0 | 411 |
""" |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
412 |
rset = self.cw_rset |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
413 |
if rset is None: |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
414 |
rset = self.cw_rset = self._cw.execute(self.startup_rql()) |
0 | 415 |
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
|
416 |
self.wview(self.__regid__, rset, row=i, **kwargs) |
0 | 417 |
|
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
418 |
|
0 | 419 |
class AnyRsetView(View): |
420 |
"""base class for views applying on any non empty result sets""" |
|
707 | 421 |
__select__ = nonempty_rset() |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
422 |
|
0 | 423 |
category = 'anyrsetview' |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
424 |
|
2203
efdd3a9f9028
update to new rql get_description api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1999
diff
changeset
|
425 |
def columns_labels(self, mainindex=0, tr=True): |
370
7e76f651314b
fix rset xml view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
369
diff
changeset
|
426 |
if tr: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
427 |
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
|
428 |
else: |
2207
5fad58e603f4
fix translate argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2203
diff
changeset
|
429 |
translate = lambda val: val |
2203
efdd3a9f9028
update to new rql get_description api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1999
diff
changeset
|
430 |
# 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
|
431 |
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
|
432 |
translate)[0] |
369
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
361
diff
changeset
|
433 |
labels = [] |
2203
efdd3a9f9028
update to new rql get_description api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1999
diff
changeset
|
434 |
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
|
435 |
# compute column header |
2203
efdd3a9f9028
update to new rql get_description api
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1999
diff
changeset
|
436 |
if label == 'Any': # find a better label |
2207
5fad58e603f4
fix translate argument
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2203
diff
changeset
|
437 |
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
|
438 |
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
|
439 |
labels.append(label) |
c8a6edc224bb
new rsetxml view, reusing most code from csvexport view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
361
diff
changeset
|
440 |
return labels |
543
c0f2b6378f70
simplification of lazy tabs mechanism
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
522
diff
changeset
|
441 |
|
1433 | 442 |
|
0 | 443 |
# concrete template base classes ############################################## |
444 |
||
816
9cd49a910fce
kill Template class and 'templates' registry
sylvain.thenault@logilab.fr
parents:
781
diff
changeset
|
445 |
class MainTemplate(View): |
0 | 446 |
"""main template are primary access point to render a full HTML page. |
447 |
There is usually at least a regular main template and a simple fallback |
|
448 |
one to display error if the first one failed |
|
449 |
""" |
|
450 |
||
451 |
@property |
|
452 |
def doctype(self): |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
453 |
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
|
454 |
return STRICT_DOCTYPE |
77ee26df178f
doc type handling refactoring: do the ext substitution at the module level
sylvain.thenault@logilab.fr
parents:
1181
diff
changeset
|
455 |
return STRICT_DOCTYPE_NOEXT |
0 | 456 |
|
1424 | 457 |
def set_stream(self, w=None): |
458 |
if self.w is not None: |
|
0 | 459 |
return |
460 |
if w is None: |
|
461 |
if self.binary: |
|
462 |
self._stream = stream = StringIO() |
|
463 |
else: |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
464 |
self._stream = stream = HTMLStream(self._cw) |
0 | 465 |
w = stream.write |
466 |
else: |
|
467 |
stream = None |
|
468 |
self.w = w |
|
469 |
return stream |
|
470 |
||
471 |
def write_doctype(self, xmldecl=True): |
|
472 |
assert isinstance(self._stream, HTMLStream) |
|
473 |
self._stream.doctype = self.doctype |
|
474 |
if not xmldecl: |
|
475 |
self._stream.xmldecl = u'' |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
476 |
|
822 | 477 |
def linkable(self): |
478 |
return False |
|
1433 | 479 |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
480 |
# concrete component base classes ############################################# |
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 ReloadableMixIn(object): |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
483 |
"""simple mixin for reloadable parts of UI""" |
1433 | 484 |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
485 |
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
|
486 |
"""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
|
487 |
inserted in html |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
488 |
""" |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
489 |
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
|
490 |
if nonify: |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
491 |
_cb = cb |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
492 |
def cb(*args): |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
493 |
_cb(*args) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3369
diff
changeset
|
494 |
cbname = self._cw.register_onetime_callback(cb, *args) |
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2217
diff
changeset
|
495 |
return self.build_js(cbname, xml_escape(msg or '')) |
1433 | 496 |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
497 |
def build_update_js_call(self, cbname, msg): |
4059 | 498 |
rql = self.cw_rset.printable_rql() |
6044
9e48ebd5400c
[view] use the new magic js object to generate javascript func call for auto reload user callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
499 |
return "javascript: %s" % js.userCallbackThenUpdateUI( |
9e48ebd5400c
[view] use the new magic js object to generate javascript func call for auto reload user callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
500 |
cbname, self.__regid__, rql, msg, self.__registry__, self.domid) |
1433 | 501 |
|
4062
d16b2927afe6
oops, bad indent
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4060
diff
changeset
|
502 |
def build_reload_js_call(self, cbname, msg): |
6044
9e48ebd5400c
[view] use the new magic js object to generate javascript func call for auto reload user callbacks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6038
diff
changeset
|
503 |
return "javascript: %s" % js.userCallbackThenReloadPage(cbname, msg) |
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
504 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
505 |
build_js = build_update_js_call # expect updatable component by default |
1433 | 506 |
|
6140
65a619eb31c4
[boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6047
diff
changeset
|
507 |
@property |
65a619eb31c4
[boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6047
diff
changeset
|
508 |
def domid(self): |
65a619eb31c4
[boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6047
diff
changeset
|
509 |
return domid(self.__regid__) |
65a619eb31c4
[boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6047
diff
changeset
|
510 |
|
65a619eb31c4
[boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6047
diff
changeset
|
511 |
@deprecated('[3.10] use .domid property') |
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
512 |
def div_id(self): |
6140
65a619eb31c4
[boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6047
diff
changeset
|
513 |
return self.domid |
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
514 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
515 |
|
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
516 |
class Component(ReloadableMixIn, View): |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
517 |
"""base class for components""" |
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
518 |
__registry__ = 'components' |
707 | 519 |
__select__ = yes() |
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
520 |
|
6140
65a619eb31c4
[boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6047
diff
changeset
|
521 |
# XXX huummm, much probably useless (should be...) |
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
|
522 |
htmlclass = 'mainRelated' |
6140
65a619eb31c4
[boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6047
diff
changeset
|
523 |
@property |
65a619eb31c4
[boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6047
diff
changeset
|
524 |
def cssclass(self): |
65a619eb31c4
[boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6047
diff
changeset
|
525 |
return '%s %s' % (self.htmlclass, domid(self.__regid__)) |
65a619eb31c4
[boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6047
diff
changeset
|
526 |
|
65a619eb31c4
[boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6047
diff
changeset
|
527 |
# XXX should rely on ReloadableMixIn.domid |
65a619eb31c4
[boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6047
diff
changeset
|
528 |
@property |
65a619eb31c4
[boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6047
diff
changeset
|
529 |
def domid(self): |
65a619eb31c4
[boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6047
diff
changeset
|
530 |
return '%sComponent' % domid(self.__regid__) |
65a619eb31c4
[boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6047
diff
changeset
|
531 |
|
65a619eb31c4
[boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6047
diff
changeset
|
532 |
@deprecated('[3.10] use .cssclass property') |
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
660
diff
changeset
|
533 |
def div_class(self): |
6140
65a619eb31c4
[boxes] introduce new boxes system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6047
diff
changeset
|
534 |
return self.cssclass |
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
535 |
|
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
536 |
|
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
537 |
class Adapter(AppObject): |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
538 |
"""base class for adapters""" |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
539 |
__registry__ = 'adapters' |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
540 |
|
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
541 |
|
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
542 |
class EntityAdapter(Adapter): |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
543 |
"""base class for entity adapters (eg adapt an entity to an interface)""" |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
544 |
def __init__(self, _cw, **kwargs): |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
545 |
try: |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
546 |
self.entity = kwargs.pop('entity') |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
547 |
except KeyError: |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
548 |
self.entity = kwargs['rset'].get_entity(kwargs.get('row') or 0, |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
549 |
kwargs.get('col') or 0) |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
550 |
Adapter.__init__(self, _cw, **kwargs) |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
551 |
|
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
552 |
|
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
553 |
def implements_adapter_compat(iface): |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
554 |
def _pre39_compat(func): |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
555 |
def decorated(self, *args, **kwargs): |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
556 |
entity = self.entity |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
557 |
if hasattr(entity, func.__name__): |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
558 |
warn('[3.9] %s method is deprecated, define it on a custom ' |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
559 |
'%s for %s instead' % (func.__name__, iface, |
5725
b5d595b66c35
[adapters] enhanced warning
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5715
diff
changeset
|
560 |
classid(entity.__class__)), |
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
561 |
DeprecationWarning) |
5571
5d024bf9424b
[adapters compat] make implements_adapter_compat selector compatible with attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
562 |
member = getattr(entity, func.__name__) |
5d024bf9424b
[adapters compat] make implements_adapter_compat selector compatible with attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
563 |
if callable(member): |
5d024bf9424b
[adapters compat] make implements_adapter_compat selector compatible with attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
564 |
return member(*args, **kwargs) |
5d024bf9424b
[adapters compat] make implements_adapter_compat selector compatible with attributes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5556
diff
changeset
|
565 |
return member |
5556
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
566 |
return func(self, *args, **kwargs) |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
567 |
return decorated |
9ab2b4c74baf
[entity] introduce a new 'adapters' registry
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5533
diff
changeset
|
568 |
return _pre39_compat |