author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 13 Oct 2009 15:59:05 +0200 | |
changeset 3655 | af86ab65a282 |
parent 3460 | e4843535db25 |
child 4252 | 6c4f109c2b03 |
permissions | -rw-r--r-- |
0 | 1 |
"""abstract controler classe for CubicWeb web client |
2 |
||
3 |
||
4 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1649
diff
changeset
|
5 |
:copyright: 2001-2009 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 parse_relations_descr(rdescr): |
|
39 |
"""parse a string describing some relations, in the form |
|
40 |
subjeids:rtype:objeids |
|
41 |
where subjeids and objeids are eids separeted by a underscore |
|
42 |
||
43 |
return an iterator on (subject eid, relation type, object eid) found |
|
44 |
""" |
|
45 |
for rstr in rdescr: |
|
46 |
subjs, rtype, objs = rstr.split(':') |
|
47 |
for subj in subjs.split('_'): |
|
48 |
for obj in objs.split('_'): |
|
49 |
yield typed_eid(subj), rtype, typed_eid(obj) |
|
1433 | 50 |
|
0 | 51 |
def append_url_params(url, params): |
52 |
"""append raw parameters to the url. Given parameters, if any, are expected |
|
53 |
to be already url-quoted. |
|
54 |
""" |
|
55 |
if params: |
|
56 |
if not '?' in url: |
|
57 |
url += '?' |
|
58 |
else: |
|
59 |
url += '&' |
|
60 |
url += params |
|
61 |
return url |
|
62 |
||
63 |
||
64 |
class Controller(AppObject): |
|
65 |
"""a controller is responsible to make necessary stuff to publish |
|
66 |
a request. There is usually at least one standard "view" controller |
|
67 |
and another linked by forms to edit objects ("edit"). |
|
68 |
""" |
|
69 |
__registry__ = 'controllers' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
722
diff
changeset
|
70 |
__select__ = yes() |
0 | 71 |
|
72 |
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
|
73 |
self.appli = kwargs.pop('appli', None) |
0 | 74 |
super(Controller, self).__init__(*args, **kwargs) |
75 |
# attributes use to control after edition redirection |
|
76 |
self._after_deletion_path = None |
|
77 |
self._edited_entity = None |
|
1433 | 78 |
|
0 | 79 |
def publish(self, rset=None): |
80 |
"""publish the current request, with an option input rql string |
|
81 |
(already processed if necessary) |
|
82 |
""" |
|
83 |
raise NotImplementedError |
|
84 |
||
85 |
# generic methods useful for concret implementations ###################### |
|
1092
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
86 |
|
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
87 |
def process_rql(self, rql): |
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
88 |
"""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
|
89 |
# XXX assigning to self really necessary? |
2890
fdcb8a2bb6eb
fix __init__ parameters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2819
diff
changeset
|
90 |
self.cw_rset = None |
1092
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
91 |
if rql: |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
92 |
self._cw.ensure_ro_rql(rql) |
1092
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
93 |
if not isinstance(rql, unicode): |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
94 |
rql = unicode(rql, self._cw.encoding) |
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
95 |
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
|
96 |
if pp is not None: |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
97 |
self.cw_rset = pp.process_query(rql) |
2890
fdcb8a2bb6eb
fix __init__ parameters
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2819
diff
changeset
|
98 |
return self.cw_rset |
1433 | 99 |
|
0 | 100 |
def check_expected_params(self, params): |
101 |
"""check that the given list of parameters are specified in the form |
|
102 |
dictionary |
|
103 |
""" |
|
104 |
missing = [] |
|
105 |
for param in params: |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
106 |
if not self._cw.form.get(param): |
0 | 107 |
missing.append(param) |
108 |
if missing: |
|
109 |
raise RequestError('missing required parameter(s): %s' |
|
110 |
% ','.join(missing)) |
|
1433 | 111 |
|
0 | 112 |
|
113 |
def notify_edited(self, entity): |
|
114 |
"""called by edit_entity() to notify which entity is edited""" |
|
115 |
# NOTE: we can't use entity.rest_path() at this point because |
|
116 |
# rest_path() could rely on schema constraints (such as a required |
|
117 |
# relation) that might not be satisfied yet (in case of creations) |
|
118 |
if not self._edited_entity: |
|
119 |
self._edited_entity = entity |
|
1433 | 120 |
|
0 | 121 |
def delete_entities(self, eidtypes): |
122 |
"""delete entities from the repository""" |
|
123 |
redirect_info = set() |
|
124 |
eidtypes = tuple(eidtypes) |
|
125 |
for eid, etype in eidtypes: |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
126 |
entity = self._cw.entity_from_eid(eid, etype) |
0 | 127 |
path, params = entity.after_deletion_path() |
128 |
redirect_info.add( (path, tuple(params.iteritems())) ) |
|
129 |
entity.delete() |
|
130 |
if len(redirect_info) > 1: |
|
131 |
# In the face of ambiguity, refuse the temptation to guess. |
|
132 |
self._after_deletion_path = 'view', () |
|
133 |
else: |
|
134 |
self._after_deletion_path = iter(redirect_info).next() |
|
135 |
if len(eidtypes) > 1: |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
136 |
self._cw.set_message(self._cw._('entities deleted')) |
0 | 137 |
else: |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
138 |
self._cw.set_message(self._cw._('entity deleted')) |
1433 | 139 |
|
0 | 140 |
def delete_relations(self, rdefs): |
141 |
"""delete relations from the repository""" |
|
142 |
# FIXME convert to using the syntax subject:relation:eids |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
143 |
execute = self._cw.execute |
0 | 144 |
for subj, rtype, obj in rdefs: |
145 |
rql = 'DELETE X %s Y where X eid %%(x)s, Y eid %%(y)s' % rtype |
|
146 |
execute(rql, {'x': subj, 'y': obj}, ('x', 'y')) |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
147 |
self._cw.set_message(self._cw._('relations deleted')) |
1433 | 148 |
|
0 | 149 |
def insert_relations(self, rdefs): |
150 |
"""insert relations into the repository""" |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
151 |
execute = self._cw.execute |
0 | 152 |
for subj, rtype, obj in rdefs: |
153 |
rql = 'SET X %s Y where X eid %%(x)s, Y eid %%(y)s' % rtype |
|
154 |
execute(rql, {'x': subj, 'y': obj}, ('x', 'y')) |
|
155 |
||
1433 | 156 |
|
0 | 157 |
def reset(self): |
158 |
"""reset form parameters and redirect to a view determinated by given |
|
159 |
parameters |
|
160 |
""" |
|
161 |
newparams = {} |
|
162 |
# sets message if needed |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
163 |
if self._cw.message: |
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
164 |
newparams['__message'] = self._cw.message |
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
165 |
if self._cw.form.has_key('__action_apply'): |
0 | 166 |
self._return_to_edition_view(newparams) |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
167 |
if self._cw.form.has_key('__action_cancel'): |
0 | 168 |
self._return_to_lastpage(newparams) |
169 |
else: |
|
170 |
self._return_to_original_view(newparams) |
|
171 |
||
172 |
||
173 |
def _return_to_original_view(self, newparams): |
|
174 |
"""validate-button case""" |
|
175 |
# transforms __redirect[*] parameters into regular form parameters |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
176 |
newparams.update(redirect_params(self._cw.form)) |
0 | 177 |
# find out if we have some explicit `rql` needs |
178 |
rql = newparams.pop('rql', None) |
|
179 |
# if rql is needed (explicit __redirectrql or multiple deletions for |
|
180 |
# instance), we have to use the old `view?rql=...` form |
|
181 |
if rql: |
|
182 |
path = 'view' |
|
183 |
newparams['rql'] = rql |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
184 |
elif '__redirectpath' in self._cw.form: |
0 | 185 |
# 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
|
186 |
path = self._cw.form['__redirectpath'] |
2314
d03429ad82b2
fix #344457
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2202
diff
changeset
|
187 |
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
|
188 |
# 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
|
189 |
# 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
|
190 |
newparams['__createdpath'] = self._edited_entity.rest_path() |
0 | 191 |
elif self._after_deletion_path: |
192 |
# else it should have been set during form processing |
|
193 |
path, params = self._after_deletion_path |
|
194 |
params = dict(params) # params given as tuple |
|
195 |
params.update(newparams) |
|
196 |
newparams = params |
|
197 |
elif self._edited_entity: |
|
198 |
path = self._edited_entity.rest_path() |
|
199 |
else: |
|
200 |
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
|
201 |
url = self._cw.build_url(path, **newparams) |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
202 |
url = append_url_params(url, self._cw.form.get('__redirectparams')) |
0 | 203 |
raise Redirect(url) |
1433 | 204 |
|
0 | 205 |
|
206 |
def _return_to_edition_view(self, newparams): |
|
207 |
"""apply-button case""" |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
208 |
form = self._cw.form |
0 | 209 |
if self._edited_entity: |
210 |
path = self._edited_entity.rest_path() |
|
211 |
newparams.pop('rql', None) |
|
212 |
# 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
|
213 |
elif 'rql' in self._cw.form: |
0 | 214 |
path = 'view' |
215 |
newparams['rql'] = form['rql'] |
|
216 |
else: |
|
217 |
self.warning("the edited data seems inconsistent") |
|
218 |
path = 'view' |
|
219 |
# pick up the correction edition view |
|
220 |
if form.get('__form_id'): |
|
221 |
newparams['vid'] = form['__form_id'] |
|
222 |
# re-insert copy redirection parameters |
|
223 |
for redirectparam in NAV_FORM_PARAMETERS: |
|
224 |
if redirectparam in form: |
|
225 |
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
|
226 |
raise Redirect(self._cw.build_url(path, **newparams)) |
0 | 227 |
|
228 |
||
229 |
def _return_to_lastpage(self, newparams): |
|
230 |
"""cancel-button case: in this case we are always expecting to go back |
|
231 |
where we came from, and this is not easy. Currently we suppose that |
|
232 |
__redirectpath is specifying that place if found, else we look in the |
|
233 |
request breadcrumbs for the last visited page. |
|
234 |
""" |
|
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
235 |
if '__redirectpath' in self._cw.form: |
0 | 236 |
# 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
|
237 |
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
|
238 |
url = self._cw.build_url(path, **newparams) |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
239 |
url = append_url_params(url, self._cw.form.get('__redirectparams')) |
0 | 240 |
else: |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3460
diff
changeset
|
241 |
url = self._cw.last_visited_page() |
0 | 242 |
raise Redirect(url) |
243 |
||
244 |
||
245 |
from cubicweb import set_log_methods |
|
246 |
set_log_methods(Controller, LOGGER) |
|
247 |