author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 05 Mar 2010 12:18:22 +0100 | |
changeset 4812 | 2eb6a9b33aa5 |
parent 4741 | f9a176ebe090 |
child 4878 | 2f75a8439338 |
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 |
||
2819
b864288fd316
remove more 3.2 deprecated code, reintroduce checkbox used by formrenderers
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2792
diff
changeset
|
11 |
from cubicweb.selectors import yes |
722 | 12 |
from cubicweb.appobject import AppObject |
0 | 13 |
from cubicweb.web import LOGGER, Redirect, RequestError |
14 |
||
15 |
||
16 |
NAVIGATION_PARAMETERS = (('vid', '__redirectvid'), |
|
17 |
('rql', '__redirectrql'), |
|
18 |
('__redirectpath', '__redirectpath'), |
|
19 |
('__redirectparams', '__redirectparams'), |
|
20 |
) |
|
1103
f719caf263de
NAV_FORM_PARAMETERS should be a tuple
sylvain.thenault@logilab.fr
parents:
1092
diff
changeset
|
21 |
NAV_FORM_PARAMETERS = tuple(fp for ap, fp in NAVIGATION_PARAMETERS) |
0 | 22 |
|
23 |
def redirect_params(form): |
|
24 |
"""transform redirection parameters into navigation parameters |
|
25 |
""" |
|
26 |
params = {} |
|
27 |
# extract navigation parameters from redirection parameters |
|
28 |
for navparam, redirectparam in NAVIGATION_PARAMETERS: |
|
29 |
if navparam == redirectparam: |
|
30 |
continue |
|
31 |
if redirectparam in form: |
|
32 |
params[navparam] = form[redirectparam] |
|
33 |
return params |
|
34 |
||
35 |
def append_url_params(url, params): |
|
36 |
"""append raw parameters to the url. Given parameters, if any, are expected |
|
37 |
to be already url-quoted. |
|
38 |
""" |
|
39 |
if params: |
|
40 |
if not '?' in url: |
|
41 |
url += '?' |
|
42 |
else: |
|
43 |
url += '&' |
|
44 |
url += params |
|
45 |
return url |
|
46 |
||
47 |
||
48 |
class Controller(AppObject): |
|
49 |
"""a controller is responsible to make necessary stuff to publish |
|
50 |
a request. There is usually at least one standard "view" controller |
|
51 |
and another linked by forms to edit objects ("edit"). |
|
52 |
""" |
|
53 |
__registry__ = 'controllers' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
722
diff
changeset
|
54 |
__select__ = yes() |
0 | 55 |
|
56 |
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
|
57 |
self.appli = kwargs.pop('appli', None) |
0 | 58 |
super(Controller, self).__init__(*args, **kwargs) |
59 |
# attributes use to control after edition redirection |
|
60 |
self._after_deletion_path = None |
|
61 |
self._edited_entity = None |
|
1433 | 62 |
|
0 | 63 |
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
|
64 |
"""publish the current request, with an optional input rset""" |
0 | 65 |
raise NotImplementedError |
66 |
||
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
|
67 |
# generic methods useful for concrete implementations ###################### |
1092
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
68 |
|
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
69 |
def process_rql(self, rql): |
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
70 |
"""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
|
71 |
# XXX assigning to self really necessary? |
2890
fdcb8a2bb6eb
fix __init__ parameters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2819
diff
changeset
|
72 |
self.cw_rset = None |
1092
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
73 |
if rql: |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
74 |
self._cw.ensure_ro_rql(rql) |
1092
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
75 |
if not isinstance(rql, unicode): |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
76 |
rql = unicode(rql, self._cw.encoding) |
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
77 |
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
|
78 |
if pp is not None: |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
79 |
self.cw_rset = pp.process_query(rql) |
2890
fdcb8a2bb6eb
fix __init__ parameters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2819
diff
changeset
|
80 |
return self.cw_rset |
1433 | 81 |
|
0 | 82 |
def check_expected_params(self, params): |
83 |
"""check that the given list of parameters are specified in the form |
|
84 |
dictionary |
|
85 |
""" |
|
86 |
missing = [] |
|
87 |
for param in params: |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
88 |
if not self._cw.form.get(param): |
0 | 89 |
missing.append(param) |
90 |
if missing: |
|
91 |
raise RequestError('missing required parameter(s): %s' |
|
92 |
% ','.join(missing)) |
|
1433 | 93 |
|
0 | 94 |
|
95 |
def notify_edited(self, entity): |
|
96 |
"""called by edit_entity() to notify which entity is edited""" |
|
97 |
# NOTE: we can't use entity.rest_path() at this point because |
|
98 |
# rest_path() could rely on schema constraints (such as a required |
|
99 |
# relation) that might not be satisfied yet (in case of creations) |
|
100 |
if not self._edited_entity: |
|
101 |
self._edited_entity = entity |
|
1433 | 102 |
|
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
|
103 |
# XXX move to EditController (only customer) |
0 | 104 |
def delete_entities(self, eidtypes): |
105 |
"""delete entities from the repository""" |
|
106 |
redirect_info = set() |
|
107 |
eidtypes = tuple(eidtypes) |
|
108 |
for eid, etype in eidtypes: |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
109 |
entity = self._cw.entity_from_eid(eid, etype) |
0 | 110 |
path, params = entity.after_deletion_path() |
111 |
redirect_info.add( (path, tuple(params.iteritems())) ) |
|
112 |
entity.delete() |
|
113 |
if len(redirect_info) > 1: |
|
114 |
# In the face of ambiguity, refuse the temptation to guess. |
|
115 |
self._after_deletion_path = 'view', () |
|
116 |
else: |
|
117 |
self._after_deletion_path = iter(redirect_info).next() |
|
118 |
if len(eidtypes) > 1: |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
119 |
self._cw.set_message(self._cw._('entities deleted')) |
0 | 120 |
else: |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
121 |
self._cw.set_message(self._cw._('entity deleted')) |
0 | 122 |
|
4557
a0571ff0cb5d
[http cache/json controller] ensure json_view does proper cache validation #390986
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4212
diff
changeset
|
123 |
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
|
124 |
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
|
125 |
self._cw.validate_cache() |
1433 | 126 |
|
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
|
127 |
# XXX is that used AT ALL ? |
0 | 128 |
def reset(self): |
129 |
"""reset form parameters and redirect to a view determinated by given |
|
130 |
parameters |
|
131 |
""" |
|
132 |
newparams = {} |
|
133 |
# sets message if needed |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
134 |
if self._cw.message: |
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
135 |
newparams['__message'] = self._cw.message |
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
136 |
if self._cw.form.has_key('__action_apply'): |
0 | 137 |
self._return_to_edition_view(newparams) |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
138 |
if self._cw.form.has_key('__action_cancel'): |
0 | 139 |
self._return_to_lastpage(newparams) |
140 |
else: |
|
141 |
self._return_to_original_view(newparams) |
|
142 |
||
143 |
||
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
|
144 |
# XXX is that used AT ALL ? |
0 | 145 |
def _return_to_original_view(self, newparams): |
146 |
"""validate-button case""" |
|
147 |
# transforms __redirect[*] parameters into regular form parameters |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
148 |
newparams.update(redirect_params(self._cw.form)) |
0 | 149 |
# find out if we have some explicit `rql` needs |
150 |
rql = newparams.pop('rql', None) |
|
151 |
# if rql is needed (explicit __redirectrql or multiple deletions for |
|
152 |
# instance), we have to use the old `view?rql=...` form |
|
153 |
if rql: |
|
154 |
path = 'view' |
|
155 |
newparams['rql'] = rql |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
156 |
elif '__redirectpath' in self._cw.form: |
0 | 157 |
# 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
|
158 |
path = self._cw.form['__redirectpath'] |
2314
d03429ad82b2
fix #344457
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2202
diff
changeset
|
159 |
if self._edited_entity and path != self._edited_entity.rest_path(): |
d03429ad82b2
fix #344457
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2202
diff
changeset
|
160 |
# XXX may be here on modification? if yes the message should be |
d03429ad82b2
fix #344457
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2202
diff
changeset
|
161 |
# modified where __createdpath is detected (cw.web.request) |
2202
cb374512949f
link to created entity when redirected to another page
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2001
diff
changeset
|
162 |
newparams['__createdpath'] = self._edited_entity.rest_path() |
0 | 163 |
elif self._after_deletion_path: |
164 |
# else it should have been set during form processing |
|
165 |
path, params = self._after_deletion_path |
|
166 |
params = dict(params) # params given as tuple |
|
167 |
params.update(newparams) |
|
168 |
newparams = params |
|
169 |
elif self._edited_entity: |
|
170 |
path = self._edited_entity.rest_path() |
|
171 |
else: |
|
172 |
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
|
173 |
url = self._cw.build_url(path, **newparams) |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
174 |
url = append_url_params(url, self._cw.form.get('__redirectparams')) |
0 | 175 |
raise Redirect(url) |
1433 | 176 |
|
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
|
177 |
# XXX is that used AT ALL ? |
0 | 178 |
def _return_to_edition_view(self, newparams): |
179 |
"""apply-button case""" |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
180 |
form = self._cw.form |
0 | 181 |
if self._edited_entity: |
182 |
path = self._edited_entity.rest_path() |
|
183 |
newparams.pop('rql', None) |
|
184 |
# 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
|
185 |
elif 'rql' in self._cw.form: |
0 | 186 |
path = 'view' |
187 |
newparams['rql'] = form['rql'] |
|
188 |
else: |
|
189 |
self.warning("the edited data seems inconsistent") |
|
190 |
path = 'view' |
|
191 |
# pick up the correction edition view |
|
192 |
if form.get('__form_id'): |
|
193 |
newparams['vid'] = form['__form_id'] |
|
194 |
# re-insert copy redirection parameters |
|
195 |
for redirectparam in NAV_FORM_PARAMETERS: |
|
196 |
if redirectparam in form: |
|
197 |
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
|
198 |
raise Redirect(self._cw.build_url(path, **newparams)) |
0 | 199 |
|
200 |
||
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
|
201 |
# XXX is that used AT ALL ? |
0 | 202 |
def _return_to_lastpage(self, newparams): |
203 |
"""cancel-button case: in this case we are always expecting to go back |
|
204 |
where we came from, and this is not easy. Currently we suppose that |
|
205 |
__redirectpath is specifying that place if found, else we look in the |
|
206 |
request breadcrumbs for the last visited page. |
|
207 |
""" |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
208 |
if '__redirectpath' in self._cw.form: |
0 | 209 |
# 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
|
210 |
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
|
211 |
url = self._cw.build_url(path, **newparams) |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
212 |
url = append_url_params(url, self._cw.form.get('__redirectparams')) |
0 | 213 |
else: |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
214 |
url = self._cw.last_visited_page() |
0 | 215 |
raise Redirect(url) |
216 |
||
217 |
||
218 |
from cubicweb import set_log_methods |
|
219 |
set_log_methods(Controller, LOGGER) |
|
220 |