author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Tue, 22 Dec 2009 13:43:24 +0100 | |
branch | stable |
changeset 4179 | 3a0b2f14e17b |
parent 2680 | 66472d85d548 |
child 2770 | 356e9d7c356d |
child 4212 | ab6573088b4a |
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 |
|
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
635
diff
changeset
|
14 |
from cubicweb.selectors import yes, require_group_compat |
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() |
757
01740274e774
remove explicit access to .im_func when overriding `registered`
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
742
diff
changeset
|
71 |
registered = require_group_compat(AppObject.registered) |
0 | 72 |
|
73 |
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
|
74 |
self.appli = kwargs.pop('appli', None) |
0 | 75 |
super(Controller, self).__init__(*args, **kwargs) |
76 |
# attributes use to control after edition redirection |
|
77 |
self._after_deletion_path = None |
|
78 |
self._edited_entity = None |
|
1433 | 79 |
|
0 | 80 |
def publish(self, rset=None): |
81 |
"""publish the current request, with an option input rql string |
|
82 |
(already processed if necessary) |
|
83 |
""" |
|
84 |
raise NotImplementedError |
|
85 |
||
86 |
# generic methods useful for concret implementations ###################### |
|
1092
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
87 |
|
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
88 |
def process_rql(self, rql): |
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
89 |
"""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
|
90 |
# XXX assigning to self really necessary? |
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
|
91 |
self.rset = None |
1092
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
92 |
if rql: |
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
93 |
self.ensure_ro_rql(rql) |
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
94 |
if not isinstance(rql, unicode): |
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
95 |
rql = unicode(rql, self.req.encoding) |
2650
18aec79ec3a3
R [vreg] important refactoring of the vregistry, moving behaviour to end dictionnary (and so leaving room for more flexibility ; keep bw compat ; update api usage in cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2381
diff
changeset
|
96 |
pp = self.vreg['components'].select_object('magicsearch', self.req) |
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
|
97 |
if pp is not None: |
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
|
98 |
self.rset = pp.process_query(rql, self.req) |
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
|
99 |
return self.rset |
1433 | 100 |
|
0 | 101 |
def check_expected_params(self, params): |
102 |
"""check that the given list of parameters are specified in the form |
|
103 |
dictionary |
|
104 |
""" |
|
105 |
missing = [] |
|
106 |
for param in params: |
|
107 |
if not self.req.form.get(param): |
|
108 |
missing.append(param) |
|
109 |
if missing: |
|
110 |
raise RequestError('missing required parameter(s): %s' |
|
111 |
% ','.join(missing)) |
|
1433 | 112 |
|
0 | 113 |
|
114 |
def notify_edited(self, entity): |
|
115 |
"""called by edit_entity() to notify which entity is edited""" |
|
116 |
# NOTE: we can't use entity.rest_path() at this point because |
|
117 |
# rest_path() could rely on schema constraints (such as a required |
|
118 |
# relation) that might not be satisfied yet (in case of creations) |
|
119 |
if not self._edited_entity: |
|
120 |
self._edited_entity = entity |
|
1433 | 121 |
|
0 | 122 |
def delete_entities(self, eidtypes): |
123 |
"""delete entities from the repository""" |
|
124 |
redirect_info = set() |
|
125 |
eidtypes = tuple(eidtypes) |
|
126 |
for eid, etype in eidtypes: |
|
2680
66472d85d548
[R] use req.entity_from_eid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2663
diff
changeset
|
127 |
entity = self.req.entity_from_eid(eid, etype) |
0 | 128 |
path, params = entity.after_deletion_path() |
129 |
redirect_info.add( (path, tuple(params.iteritems())) ) |
|
130 |
entity.delete() |
|
131 |
if len(redirect_info) > 1: |
|
132 |
# In the face of ambiguity, refuse the temptation to guess. |
|
133 |
self._after_deletion_path = 'view', () |
|
134 |
else: |
|
135 |
self._after_deletion_path = iter(redirect_info).next() |
|
136 |
if len(eidtypes) > 1: |
|
137 |
self.req.set_message(self.req._('entities deleted')) |
|
138 |
else: |
|
139 |
self.req.set_message(self.req._('entity deleted')) |
|
1433 | 140 |
|
0 | 141 |
def delete_relations(self, rdefs): |
142 |
"""delete relations from the repository""" |
|
143 |
# FIXME convert to using the syntax subject:relation:eids |
|
144 |
execute = self.req.execute |
|
145 |
for subj, rtype, obj in rdefs: |
|
146 |
rql = 'DELETE X %s Y where X eid %%(x)s, Y eid %%(y)s' % rtype |
|
147 |
execute(rql, {'x': subj, 'y': obj}, ('x', 'y')) |
|
148 |
self.req.set_message(self.req._('relations deleted')) |
|
1433 | 149 |
|
0 | 150 |
def insert_relations(self, rdefs): |
151 |
"""insert relations into the repository""" |
|
152 |
execute = self.req.execute |
|
153 |
for subj, rtype, obj in rdefs: |
|
154 |
rql = 'SET X %s Y where X eid %%(x)s, Y eid %%(y)s' % rtype |
|
155 |
execute(rql, {'x': subj, 'y': obj}, ('x', 'y')) |
|
156 |
||
1433 | 157 |
|
0 | 158 |
def reset(self): |
159 |
"""reset form parameters and redirect to a view determinated by given |
|
160 |
parameters |
|
161 |
""" |
|
162 |
newparams = {} |
|
163 |
# sets message if needed |
|
164 |
if self.req.message: |
|
165 |
newparams['__message'] = self.req.message |
|
166 |
if self.req.form.has_key('__action_apply'): |
|
167 |
self._return_to_edition_view(newparams) |
|
168 |
if self.req.form.has_key('__action_cancel'): |
|
169 |
self._return_to_lastpage(newparams) |
|
170 |
else: |
|
171 |
self._return_to_original_view(newparams) |
|
172 |
||
173 |
||
174 |
def _return_to_original_view(self, newparams): |
|
175 |
"""validate-button case""" |
|
176 |
# transforms __redirect[*] parameters into regular form parameters |
|
177 |
newparams.update(redirect_params(self.req.form)) |
|
178 |
# find out if we have some explicit `rql` needs |
|
179 |
rql = newparams.pop('rql', None) |
|
180 |
# if rql is needed (explicit __redirectrql or multiple deletions for |
|
181 |
# instance), we have to use the old `view?rql=...` form |
|
182 |
if rql: |
|
183 |
path = 'view' |
|
184 |
newparams['rql'] = rql |
|
185 |
elif '__redirectpath' in self.req.form: |
|
186 |
# if redirect path was explicitly specified in the form, use it |
|
187 |
path = self.req.form['__redirectpath'] |
|
2314
d03429ad82b2
fix #344457
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2202
diff
changeset
|
188 |
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
|
189 |
# 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
|
190 |
# 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
|
191 |
newparams['__createdpath'] = self._edited_entity.rest_path() |
0 | 192 |
elif self._after_deletion_path: |
193 |
# else it should have been set during form processing |
|
194 |
path, params = self._after_deletion_path |
|
195 |
params = dict(params) # params given as tuple |
|
196 |
params.update(newparams) |
|
197 |
newparams = params |
|
198 |
elif self._edited_entity: |
|
199 |
path = self._edited_entity.rest_path() |
|
200 |
else: |
|
201 |
path = 'view' |
|
202 |
url = self.build_url(path, **newparams) |
|
203 |
url = append_url_params(url, self.req.form.get('__redirectparams')) |
|
204 |
raise Redirect(url) |
|
1433 | 205 |
|
0 | 206 |
|
207 |
def _return_to_edition_view(self, newparams): |
|
208 |
"""apply-button case""" |
|
209 |
form = self.req.form |
|
210 |
if self._edited_entity: |
|
211 |
path = self._edited_entity.rest_path() |
|
212 |
newparams.pop('rql', None) |
|
213 |
# else, fallback on the old `view?rql=...` url form |
|
214 |
elif 'rql' in self.req.form: |
|
215 |
path = 'view' |
|
216 |
newparams['rql'] = form['rql'] |
|
217 |
else: |
|
218 |
self.warning("the edited data seems inconsistent") |
|
219 |
path = 'view' |
|
220 |
# pick up the correction edition view |
|
221 |
if form.get('__form_id'): |
|
222 |
newparams['vid'] = form['__form_id'] |
|
223 |
# re-insert copy redirection parameters |
|
224 |
for redirectparam in NAV_FORM_PARAMETERS: |
|
225 |
if redirectparam in form: |
|
226 |
newparams[redirectparam] = form[redirectparam] |
|
227 |
raise Redirect(self.build_url(path, **newparams)) |
|
228 |
||
229 |
||
230 |
def _return_to_lastpage(self, newparams): |
|
231 |
"""cancel-button case: in this case we are always expecting to go back |
|
232 |
where we came from, and this is not easy. Currently we suppose that |
|
233 |
__redirectpath is specifying that place if found, else we look in the |
|
234 |
request breadcrumbs for the last visited page. |
|
235 |
""" |
|
236 |
if '__redirectpath' in self.req.form: |
|
237 |
# if redirect path was explicitly specified in the form, use it |
|
238 |
path = self.req.form['__redirectpath'] |
|
239 |
url = self.build_url(path, **newparams) |
|
240 |
url = append_url_params(url, self.req.form.get('__redirectparams')) |
|
241 |
else: |
|
242 |
url = self.req.last_visited_page() |
|
243 |
raise Redirect(url) |
|
244 |
||
245 |
||
246 |
from cubicweb import set_log_methods |
|
247 |
set_log_methods(Controller, LOGGER) |
|
248 |