author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 12 Mar 2010 10:49:42 +0100 | |
changeset 4878 | 2f75a8439338 |
parent 4741 | f9a176ebe090 |
child 4883 | 11899d28337f |
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 notify_edited(self, entity): |
83 |
"""called by edit_entity() to notify which entity is edited""" |
|
84 |
# NOTE: we can't use entity.rest_path() at this point because |
|
85 |
# rest_path() could rely on schema constraints (such as a required |
|
86 |
# relation) that might not be satisfied yet (in case of creations) |
|
87 |
if not self._edited_entity: |
|
88 |
self._edited_entity = entity |
|
1433 | 89 |
|
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
|
90 |
# XXX move to EditController (only customer) |
0 | 91 |
def delete_entities(self, eidtypes): |
92 |
"""delete entities from the repository""" |
|
93 |
redirect_info = set() |
|
94 |
eidtypes = tuple(eidtypes) |
|
95 |
for eid, etype in eidtypes: |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
96 |
entity = self._cw.entity_from_eid(eid, etype) |
0 | 97 |
path, params = entity.after_deletion_path() |
98 |
redirect_info.add( (path, tuple(params.iteritems())) ) |
|
99 |
entity.delete() |
|
100 |
if len(redirect_info) > 1: |
|
101 |
# In the face of ambiguity, refuse the temptation to guess. |
|
102 |
self._after_deletion_path = 'view', () |
|
103 |
else: |
|
104 |
self._after_deletion_path = iter(redirect_info).next() |
|
105 |
if len(eidtypes) > 1: |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
106 |
self._cw.set_message(self._cw._('entities deleted')) |
0 | 107 |
else: |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
108 |
self._cw.set_message(self._cw._('entity deleted')) |
0 | 109 |
|
4557
a0571ff0cb5d
[http cache/json controller] ensure json_view does proper cache validation #390986
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4212
diff
changeset
|
110 |
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
|
111 |
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
|
112 |
self._cw.validate_cache() |
1433 | 113 |
|
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
|
114 |
# XXX is that used AT ALL ? |
0 | 115 |
def reset(self): |
116 |
"""reset form parameters and redirect to a view determinated by given |
|
117 |
parameters |
|
118 |
""" |
|
119 |
newparams = {} |
|
120 |
# sets message if needed |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
121 |
if self._cw.message: |
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
122 |
newparams['__message'] = self._cw.message |
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
123 |
if self._cw.form.has_key('__action_apply'): |
0 | 124 |
self._return_to_edition_view(newparams) |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
125 |
if self._cw.form.has_key('__action_cancel'): |
0 | 126 |
self._return_to_lastpage(newparams) |
127 |
else: |
|
128 |
self._return_to_original_view(newparams) |
|
129 |
||
130 |
||
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
|
131 |
# XXX is that used AT ALL ? |
0 | 132 |
def _return_to_original_view(self, newparams): |
133 |
"""validate-button case""" |
|
134 |
# transforms __redirect[*] parameters into regular form parameters |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
135 |
newparams.update(redirect_params(self._cw.form)) |
0 | 136 |
# find out if we have some explicit `rql` needs |
137 |
rql = newparams.pop('rql', None) |
|
138 |
# if rql is needed (explicit __redirectrql or multiple deletions for |
|
139 |
# instance), we have to use the old `view?rql=...` form |
|
140 |
if rql: |
|
141 |
path = 'view' |
|
142 |
newparams['rql'] = rql |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
143 |
elif '__redirectpath' in self._cw.form: |
0 | 144 |
# 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
|
145 |
path = self._cw.form['__redirectpath'] |
2314
d03429ad82b2
fix #344457
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2202
diff
changeset
|
146 |
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
|
147 |
# 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
|
148 |
# 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
|
149 |
newparams['__createdpath'] = self._edited_entity.rest_path() |
0 | 150 |
elif self._after_deletion_path: |
151 |
# else it should have been set during form processing |
|
152 |
path, params = self._after_deletion_path |
|
153 |
params = dict(params) # params given as tuple |
|
154 |
params.update(newparams) |
|
155 |
newparams = params |
|
156 |
elif self._edited_entity: |
|
157 |
path = self._edited_entity.rest_path() |
|
158 |
else: |
|
159 |
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
|
160 |
url = self._cw.build_url(path, **newparams) |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
161 |
url = append_url_params(url, self._cw.form.get('__redirectparams')) |
0 | 162 |
raise Redirect(url) |
1433 | 163 |
|
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
|
164 |
# XXX is that used AT ALL ? |
0 | 165 |
def _return_to_edition_view(self, newparams): |
166 |
"""apply-button case""" |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
167 |
form = self._cw.form |
0 | 168 |
if self._edited_entity: |
169 |
path = self._edited_entity.rest_path() |
|
170 |
newparams.pop('rql', None) |
|
171 |
# 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
|
172 |
elif 'rql' in self._cw.form: |
0 | 173 |
path = 'view' |
174 |
newparams['rql'] = form['rql'] |
|
175 |
else: |
|
176 |
self.warning("the edited data seems inconsistent") |
|
177 |
path = 'view' |
|
178 |
# pick up the correction edition view |
|
179 |
if form.get('__form_id'): |
|
180 |
newparams['vid'] = form['__form_id'] |
|
181 |
# re-insert copy redirection parameters |
|
182 |
for redirectparam in NAV_FORM_PARAMETERS: |
|
183 |
if redirectparam in form: |
|
184 |
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
|
185 |
raise Redirect(self._cw.build_url(path, **newparams)) |
0 | 186 |
|
187 |
||
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
|
188 |
# XXX is that used AT ALL ? |
0 | 189 |
def _return_to_lastpage(self, newparams): |
190 |
"""cancel-button case: in this case we are always expecting to go back |
|
191 |
where we came from, and this is not easy. Currently we suppose that |
|
192 |
__redirectpath is specifying that place if found, else we look in the |
|
193 |
request breadcrumbs for the last visited page. |
|
194 |
""" |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
195 |
if '__redirectpath' in self._cw.form: |
0 | 196 |
# 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
|
197 |
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
|
198 |
url = self._cw.build_url(path, **newparams) |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
199 |
url = append_url_params(url, self._cw.form.get('__redirectparams')) |
0 | 200 |
else: |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
201 |
url = self._cw.last_visited_page() |
0 | 202 |
raise Redirect(url) |
203 |
||
204 |
||
205 |
from cubicweb import set_log_methods |
|
206 |
set_log_methods(Controller, LOGGER) |
|
207 |