author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Thu, 15 Apr 2010 10:58:21 +0200 | |
branch | stable |
changeset 5266 | 84f285d96363 |
parent 5040 | 00782905b720 |
child 5421 | 8167de96c523 |
permissions | -rw-r--r-- |
4669
2a77a0d9075f
cleanup, typos
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4666
diff
changeset
|
1 |
"""abstract controller classe for CubicWeb web client |
0 | 2 |
|
3 |
||
4 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2680
diff
changeset
|
5 |
:copyright: 2001-2010 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:
1649
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 8 |
""" |
9 |
__docformat__ = "restructuredtext en" |
|
10 |
||
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4884
diff
changeset
|
11 |
from logilab.mtconverter import xml_escape |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4884
diff
changeset
|
12 |
|
2819
b864288fd316
remove more 3.2 deprecated code, reintroduce checkbox used by formrenderers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
13 |
from cubicweb.selectors import yes |
722 | 14 |
from cubicweb.appobject import AppObject |
0 | 15 |
from cubicweb.web import LOGGER, Redirect, RequestError |
16 |
||
17 |
||
18 |
NAVIGATION_PARAMETERS = (('vid', '__redirectvid'), |
|
19 |
('rql', '__redirectrql'), |
|
20 |
('__redirectpath', '__redirectpath'), |
|
21 |
('__redirectparams', '__redirectparams'), |
|
22 |
) |
|
1103
f719caf263de
NAV_FORM_PARAMETERS should be a tuple
sylvain.thenault@logilab.fr
parents:
1092
diff
changeset
|
23 |
NAV_FORM_PARAMETERS = tuple(fp for ap, fp in NAVIGATION_PARAMETERS) |
0 | 24 |
|
25 |
def redirect_params(form): |
|
26 |
"""transform redirection parameters into navigation parameters |
|
27 |
""" |
|
28 |
params = {} |
|
29 |
# extract navigation parameters from redirection parameters |
|
30 |
for navparam, redirectparam in NAVIGATION_PARAMETERS: |
|
31 |
if navparam == redirectparam: |
|
32 |
continue |
|
33 |
if redirectparam in form: |
|
34 |
params[navparam] = form[redirectparam] |
|
35 |
return params |
|
36 |
||
37 |
def append_url_params(url, params): |
|
38 |
"""append raw parameters to the url. Given parameters, if any, are expected |
|
39 |
to be already url-quoted. |
|
40 |
""" |
|
41 |
if params: |
|
42 |
if not '?' in url: |
|
43 |
url += '?' |
|
44 |
else: |
|
45 |
url += '&' |
|
46 |
url += params |
|
47 |
return url |
|
48 |
||
49 |
||
50 |
class Controller(AppObject): |
|
51 |
"""a controller is responsible to make necessary stuff to publish |
|
52 |
a request. There is usually at least one standard "view" controller |
|
53 |
and another linked by forms to edit objects ("edit"). |
|
54 |
""" |
|
55 |
__registry__ = 'controllers' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
722
diff
changeset
|
56 |
__select__ = yes() |
0 | 57 |
|
58 |
def __init__(self, *args, **kwargs): |
|
2663
2bb628e0cc3b
[controller] should catch and set appli argument here now that the old AppObject class is gone
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
59 |
self.appli = kwargs.pop('appli', None) |
0 | 60 |
super(Controller, self).__init__(*args, **kwargs) |
61 |
# attributes use to control after edition redirection |
|
62 |
self._after_deletion_path = None |
|
63 |
self._edited_entity = None |
|
1433 | 64 |
|
0 | 65 |
def publish(self, rset=None): |
4741
f9a176ebe090
[book/controllers] add some content (overview, api super sketch) for the cubicweb controllers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4719
diff
changeset
|
66 |
"""publish the current request, with an optional input rset""" |
0 | 67 |
raise NotImplementedError |
68 |
||
4741
f9a176ebe090
[book/controllers] add some content (overview, api super sketch) for the cubicweb controllers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4719
diff
changeset
|
69 |
# generic methods useful for concrete implementations ###################### |
1092
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
70 |
|
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
71 |
def process_rql(self, rql): |
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
72 |
"""execute rql if specified""" |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2001
diff
changeset
|
73 |
# XXX assigning to self really necessary? |
2890
fdcb8a2bb6eb
fix __init__ parameters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2819
diff
changeset
|
74 |
self.cw_rset = None |
1092
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
75 |
if rql: |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
76 |
self._cw.ensure_ro_rql(rql) |
1092
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
77 |
if not isinstance(rql, unicode): |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
78 |
rql = unicode(rql, self._cw.encoding) |
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
79 |
pp = self._cw.vreg['components'].select_or_none('magicsearch', self._cw) |
2058
7ef12c03447c
nicer vreg api, try to make rset an optional named argument in select and derivated (including selectors)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2001
diff
changeset
|
80 |
if pp is not None: |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
81 |
self.cw_rset = pp.process_query(rql) |
2890
fdcb8a2bb6eb
fix __init__ parameters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2819
diff
changeset
|
82 |
return self.cw_rset |
1433 | 83 |
|
0 | 84 |
def notify_edited(self, entity): |
85 |
"""called by edit_entity() to notify which entity is edited""" |
|
86 |
# NOTE: we can't use entity.rest_path() at this point because |
|
87 |
# rest_path() could rely on schema constraints (such as a required |
|
88 |
# relation) that might not be satisfied yet (in case of creations) |
|
89 |
if not self._edited_entity: |
|
90 |
self._edited_entity = entity |
|
1433 | 91 |
|
4557
a0571ff0cb5d
[http cache/json controller] ensure json_view does proper cache validation #390986
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4212
diff
changeset
|
92 |
def validate_cache(self, view): |
a0571ff0cb5d
[http cache/json controller] ensure json_view does proper cache validation #390986
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4212
diff
changeset
|
93 |
view.set_http_cache_headers() |
4666
737cbdb87e87
3.6 api update (introduced by merge)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4636
diff
changeset
|
94 |
self._cw.validate_cache() |
1433 | 95 |
|
0 | 96 |
def reset(self): |
97 |
"""reset form parameters and redirect to a view determinated by given |
|
98 |
parameters |
|
99 |
""" |
|
100 |
newparams = {} |
|
101 |
# sets message if needed |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
102 |
if self._cw.message: |
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4884
diff
changeset
|
103 |
newparams['_cwmsgid'] = self._cw.set_redirect_message(self._cw.message) |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
104 |
if self._cw.form.has_key('__action_apply'): |
0 | 105 |
self._return_to_edition_view(newparams) |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
106 |
if self._cw.form.has_key('__action_cancel'): |
0 | 107 |
self._return_to_lastpage(newparams) |
108 |
else: |
|
109 |
self._return_to_original_view(newparams) |
|
110 |
||
111 |
def _return_to_original_view(self, newparams): |
|
112 |
"""validate-button case""" |
|
113 |
# transforms __redirect[*] parameters into regular form parameters |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
114 |
newparams.update(redirect_params(self._cw.form)) |
0 | 115 |
# find out if we have some explicit `rql` needs |
116 |
rql = newparams.pop('rql', None) |
|
117 |
# if rql is needed (explicit __redirectrql or multiple deletions for |
|
118 |
# instance), we have to use the old `view?rql=...` form |
|
119 |
if rql: |
|
120 |
path = 'view' |
|
121 |
newparams['rql'] = rql |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
122 |
elif '__redirectpath' in self._cw.form: |
0 | 123 |
# if redirect path was explicitly specified in the form, use it |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
124 |
path = self._cw.form['__redirectpath'] |
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4884
diff
changeset
|
125 |
if (self._edited_entity and path != self._edited_entity.rest_path() |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4884
diff
changeset
|
126 |
and '_cwmsgid' in newparams): |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4884
diff
changeset
|
127 |
# XXX may be here on modification? |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4884
diff
changeset
|
128 |
msg = u'(<a href="%s">%s</a>)' % ( |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4884
diff
changeset
|
129 |
xml_escape(self._edited_entity.absolute_url()), |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4884
diff
changeset
|
130 |
self._cw._('click here to see created entity')) |
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4884
diff
changeset
|
131 |
self._cw.append_to_redirect_message(msg) |
0 | 132 |
elif self._after_deletion_path: |
133 |
# else it should have been set during form processing |
|
134 |
path, params = self._after_deletion_path |
|
135 |
params = dict(params) # params given as tuple |
|
136 |
params.update(newparams) |
|
137 |
newparams = params |
|
138 |
elif self._edited_entity: |
|
5040
00782905b720
[form controller] clear caches in case some attribute participating to the rest path has been modified, avoid redirection to a no more existant page (fix #753567)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4897
diff
changeset
|
139 |
# clear caches in case some attribute participating to the rest path |
00782905b720
[form controller] clear caches in case some attribute participating to the rest path has been modified, avoid redirection to a no more existant page (fix #753567)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4897
diff
changeset
|
140 |
# has been modified |
00782905b720
[form controller] clear caches in case some attribute participating to the rest path has been modified, avoid redirection to a no more existant page (fix #753567)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4897
diff
changeset
|
141 |
self._edited_entity.clear_all_caches() |
0 | 142 |
path = self._edited_entity.rest_path() |
143 |
else: |
|
144 |
path = 'view' |
|
3460
e4843535db25
[api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2890
diff
changeset
|
145 |
url = self._cw.build_url(path, **newparams) |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
146 |
url = append_url_params(url, self._cw.form.get('__redirectparams')) |
0 | 147 |
raise Redirect(url) |
1433 | 148 |
|
0 | 149 |
def _return_to_edition_view(self, newparams): |
150 |
"""apply-button case""" |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
151 |
form = self._cw.form |
0 | 152 |
if self._edited_entity: |
153 |
path = self._edited_entity.rest_path() |
|
154 |
newparams.pop('rql', None) |
|
155 |
# else, fallback on the old `view?rql=...` url form |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
156 |
elif 'rql' in self._cw.form: |
0 | 157 |
path = 'view' |
158 |
newparams['rql'] = form['rql'] |
|
159 |
else: |
|
4897
e402e0b32075
[web] start a new message system based on id of message stored in session's data
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4884
diff
changeset
|
160 |
self.warning('the edited data seems inconsistent') |
0 | 161 |
path = 'view' |
162 |
# pick up the correction edition view |
|
163 |
if form.get('__form_id'): |
|
164 |
newparams['vid'] = form['__form_id'] |
|
165 |
# re-insert copy redirection parameters |
|
166 |
for redirectparam in NAV_FORM_PARAMETERS: |
|
167 |
if redirectparam in form: |
|
168 |
newparams[redirectparam] = form[redirectparam] |
|
3460
e4843535db25
[api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2890
diff
changeset
|
169 |
raise Redirect(self._cw.build_url(path, **newparams)) |
0 | 170 |
|
171 |
||
172 |
def _return_to_lastpage(self, newparams): |
|
173 |
"""cancel-button case: in this case we are always expecting to go back |
|
174 |
where we came from, and this is not easy. Currently we suppose that |
|
175 |
__redirectpath is specifying that place if found, else we look in the |
|
176 |
request breadcrumbs for the last visited page. |
|
177 |
""" |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
178 |
if '__redirectpath' in self._cw.form: |
0 | 179 |
# if redirect path was explicitly specified in the form, use it |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
180 |
path = self._cw.form['__redirectpath'] |
3460
e4843535db25
[api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2890
diff
changeset
|
181 |
url = self._cw.build_url(path, **newparams) |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
182 |
url = append_url_params(url, self._cw.form.get('__redirectparams')) |
0 | 183 |
else: |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
184 |
url = self._cw.last_visited_page() |
0 | 185 |
raise Redirect(url) |
186 |
||
187 |
||
188 |
from cubicweb import set_log_methods |
|
189 |
set_log_methods(Controller, LOGGER) |
|
190 |