author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Fri, 12 Mar 2010 10:53:25 +0100 | |
changeset 4884 | f175997057b5 |
parent 4883 | 11899d28337f |
child 4897 | e402e0b32075 |
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 |
|
4557
a0571ff0cb5d
[http cache/json controller] ensure json_view does proper cache validation #390986
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4212
diff
changeset
|
90 |
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
|
91 |
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
|
92 |
self._cw.validate_cache() |
1433 | 93 |
|
0 | 94 |
def reset(self): |
95 |
"""reset form parameters and redirect to a view determinated by given |
|
96 |
parameters |
|
97 |
""" |
|
98 |
newparams = {} |
|
99 |
# sets message if needed |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
100 |
if self._cw.message: |
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
101 |
newparams['__message'] = self._cw.message |
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
102 |
if self._cw.form.has_key('__action_apply'): |
0 | 103 |
self._return_to_edition_view(newparams) |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
104 |
if self._cw.form.has_key('__action_cancel'): |
0 | 105 |
self._return_to_lastpage(newparams) |
106 |
else: |
|
107 |
self._return_to_original_view(newparams) |
|
108 |
||
109 |
def _return_to_original_view(self, newparams): |
|
110 |
"""validate-button case""" |
|
111 |
# transforms __redirect[*] parameters into regular form parameters |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
112 |
newparams.update(redirect_params(self._cw.form)) |
0 | 113 |
# find out if we have some explicit `rql` needs |
114 |
rql = newparams.pop('rql', None) |
|
115 |
# if rql is needed (explicit __redirectrql or multiple deletions for |
|
116 |
# instance), we have to use the old `view?rql=...` form |
|
117 |
if rql: |
|
118 |
path = 'view' |
|
119 |
newparams['rql'] = rql |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
120 |
elif '__redirectpath' in self._cw.form: |
0 | 121 |
# 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
|
122 |
path = self._cw.form['__redirectpath'] |
2314
d03429ad82b2
fix #344457
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2202
diff
changeset
|
123 |
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
|
124 |
# 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
|
125 |
# 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
|
126 |
newparams['__createdpath'] = self._edited_entity.rest_path() |
0 | 127 |
elif self._after_deletion_path: |
128 |
# else it should have been set during form processing |
|
129 |
path, params = self._after_deletion_path |
|
130 |
params = dict(params) # params given as tuple |
|
131 |
params.update(newparams) |
|
132 |
newparams = params |
|
133 |
elif self._edited_entity: |
|
134 |
path = self._edited_entity.rest_path() |
|
135 |
else: |
|
136 |
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
|
137 |
url = self._cw.build_url(path, **newparams) |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
138 |
url = append_url_params(url, self._cw.form.get('__redirectparams')) |
0 | 139 |
raise Redirect(url) |
1433 | 140 |
|
0 | 141 |
def _return_to_edition_view(self, newparams): |
142 |
"""apply-button case""" |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
143 |
form = self._cw.form |
0 | 144 |
if self._edited_entity: |
145 |
path = self._edited_entity.rest_path() |
|
146 |
newparams.pop('rql', None) |
|
147 |
# 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
|
148 |
elif 'rql' in self._cw.form: |
0 | 149 |
path = 'view' |
150 |
newparams['rql'] = form['rql'] |
|
151 |
else: |
|
152 |
self.warning("the edited data seems inconsistent") |
|
153 |
path = 'view' |
|
154 |
# pick up the correction edition view |
|
155 |
if form.get('__form_id'): |
|
156 |
newparams['vid'] = form['__form_id'] |
|
157 |
# re-insert copy redirection parameters |
|
158 |
for redirectparam in NAV_FORM_PARAMETERS: |
|
159 |
if redirectparam in form: |
|
160 |
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
|
161 |
raise Redirect(self._cw.build_url(path, **newparams)) |
0 | 162 |
|
163 |
||
164 |
def _return_to_lastpage(self, newparams): |
|
165 |
"""cancel-button case: in this case we are always expecting to go back |
|
166 |
where we came from, and this is not easy. Currently we suppose that |
|
167 |
__redirectpath is specifying that place if found, else we look in the |
|
168 |
request breadcrumbs for the last visited page. |
|
169 |
""" |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
170 |
if '__redirectpath' in self._cw.form: |
0 | 171 |
# 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
|
172 |
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
|
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 |
else: |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
176 |
url = self._cw.last_visited_page() |
0 | 177 |
raise Redirect(url) |
178 |
||
179 |
||
180 |
from cubicweb import set_log_methods |
|
181 |
set_log_methods(Controller, LOGGER) |
|
182 |