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