author | sylvain.thenault@logilab.fr |
Thu, 14 May 2009 12:48:11 +0200 | |
changeset 1808 | aa09e20dd8c0 |
parent 1649 | ab3c662b6ebe |
child 1977 | 606923dff11b |
permissions | -rw-r--r-- |
0 | 1 |
"""abstract controler classe for CubicWeb web client |
2 |
||
3 |
||
4 |
:organization: Logilab |
|
635
305da8d6aa2d
require_group/match_user_group -> match_user_groups
sylvain.thenault@logilab.fr
parents:
431
diff
changeset
|
5 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
7 |
""" |
|
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
1478
674fa3eb01d1
parse_datetime call with Date etype now returns a datetime.date instance
Florent <florent@secondweb.fr>
parents:
1103
diff
changeset
|
10 |
import datetime |
0 | 11 |
|
12 |
from cubicweb import typed_eid |
|
1649 | 13 |
from cubicweb.utils import strptime, todate, todatetime |
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): |
|
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""" |
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
89 |
if rql: |
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
90 |
self.ensure_ro_rql(rql) |
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
91 |
if not isinstance(rql, unicode): |
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
92 |
rql = unicode(rql, self.req.encoding) |
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
93 |
pp = self.vreg.select_component('magicsearch', self.req) |
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
94 |
self.rset = pp.process_query(rql, self.req) |
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
95 |
return self.rset |
b8fbb95dc0eb
process_rql now done in the controller
sylvain.thenault@logilab.fr
parents:
1016
diff
changeset
|
96 |
return None |
1433 | 97 |
|
0 | 98 |
def check_expected_params(self, params): |
99 |
"""check that the given list of parameters are specified in the form |
|
100 |
dictionary |
|
101 |
""" |
|
102 |
missing = [] |
|
103 |
for param in params: |
|
104 |
if not self.req.form.get(param): |
|
105 |
missing.append(param) |
|
106 |
if missing: |
|
107 |
raise RequestError('missing required parameter(s): %s' |
|
108 |
% ','.join(missing)) |
|
1433 | 109 |
|
0 | 110 |
def parse_datetime(self, value, etype='Datetime'): |
111 |
"""get a datetime or time from a string (according to etype) |
|
112 |
Datetime formatted as Date are accepted |
|
113 |
""" |
|
114 |
assert etype in ('Datetime', 'Date', 'Time'), etype |
|
115 |
# XXX raise proper validation error |
|
116 |
if etype == 'Datetime': |
|
117 |
format = self.req.property_value('ui.datetime-format') |
|
118 |
try: |
|
1649 | 119 |
return todatetime(strptime(value, format)) |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
757
diff
changeset
|
120 |
except: |
0 | 121 |
pass |
122 |
elif etype == 'Time': |
|
123 |
format = self.req.property_value('ui.time-format') |
|
124 |
try: |
|
125 |
# (adim) I can't find a way to parse a Time with a custom format |
|
126 |
date = strptime(value, format) # this returns a DateTime |
|
1649 | 127 |
return datetime.time(date.hour, date.minute, date.second) |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
757
diff
changeset
|
128 |
except: |
0 | 129 |
raise ValueError('can\'t parse %r (expected %s)' % (value, format)) |
130 |
try: |
|
131 |
format = self.req.property_value('ui.date-format') |
|
1478
674fa3eb01d1
parse_datetime call with Date etype now returns a datetime.date instance
Florent <florent@secondweb.fr>
parents:
1103
diff
changeset
|
132 |
dt = strptime(value, format) |
1649 | 133 |
if etype == 'Datetime': |
134 |
return todatetime(dt) |
|
135 |
return todate(dt) |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
757
diff
changeset
|
136 |
except: |
0 | 137 |
raise ValueError('can\'t parse %r (expected %s)' % (value, format)) |
138 |
||
139 |
||
140 |
def notify_edited(self, entity): |
|
141 |
"""called by edit_entity() to notify which entity is edited""" |
|
142 |
# NOTE: we can't use entity.rest_path() at this point because |
|
143 |
# rest_path() could rely on schema constraints (such as a required |
|
144 |
# relation) that might not be satisfied yet (in case of creations) |
|
145 |
if not self._edited_entity: |
|
146 |
self._edited_entity = entity |
|
1433 | 147 |
|
0 | 148 |
def delete_entities(self, eidtypes): |
149 |
"""delete entities from the repository""" |
|
150 |
redirect_info = set() |
|
151 |
eidtypes = tuple(eidtypes) |
|
152 |
for eid, etype in eidtypes: |
|
153 |
entity = self.req.eid_rset(eid, etype).get_entity(0, 0) |
|
154 |
path, params = entity.after_deletion_path() |
|
155 |
redirect_info.add( (path, tuple(params.iteritems())) ) |
|
156 |
entity.delete() |
|
157 |
if len(redirect_info) > 1: |
|
158 |
# In the face of ambiguity, refuse the temptation to guess. |
|
159 |
self._after_deletion_path = 'view', () |
|
160 |
else: |
|
161 |
self._after_deletion_path = iter(redirect_info).next() |
|
162 |
if len(eidtypes) > 1: |
|
163 |
self.req.set_message(self.req._('entities deleted')) |
|
164 |
else: |
|
165 |
self.req.set_message(self.req._('entity deleted')) |
|
1433 | 166 |
|
0 | 167 |
def delete_relations(self, rdefs): |
168 |
"""delete relations from the repository""" |
|
169 |
# FIXME convert to using the syntax subject:relation:eids |
|
170 |
execute = self.req.execute |
|
171 |
for subj, rtype, obj in rdefs: |
|
172 |
rql = 'DELETE X %s Y where X eid %%(x)s, Y eid %%(y)s' % rtype |
|
173 |
execute(rql, {'x': subj, 'y': obj}, ('x', 'y')) |
|
174 |
self.req.set_message(self.req._('relations deleted')) |
|
1433 | 175 |
|
0 | 176 |
def insert_relations(self, rdefs): |
177 |
"""insert relations into the repository""" |
|
178 |
execute = self.req.execute |
|
179 |
for subj, rtype, obj in rdefs: |
|
180 |
rql = 'SET X %s Y where X eid %%(x)s, Y eid %%(y)s' % rtype |
|
181 |
execute(rql, {'x': subj, 'y': obj}, ('x', 'y')) |
|
182 |
||
1433 | 183 |
|
0 | 184 |
def reset(self): |
185 |
"""reset form parameters and redirect to a view determinated by given |
|
186 |
parameters |
|
187 |
""" |
|
188 |
newparams = {} |
|
189 |
# sets message if needed |
|
190 |
if self.req.message: |
|
191 |
newparams['__message'] = self.req.message |
|
192 |
if self.req.form.has_key('__action_apply'): |
|
193 |
self._return_to_edition_view(newparams) |
|
194 |
if self.req.form.has_key('__action_cancel'): |
|
195 |
self._return_to_lastpage(newparams) |
|
196 |
else: |
|
197 |
self._return_to_original_view(newparams) |
|
198 |
||
199 |
||
200 |
def _return_to_original_view(self, newparams): |
|
201 |
"""validate-button case""" |
|
202 |
# transforms __redirect[*] parameters into regular form parameters |
|
203 |
newparams.update(redirect_params(self.req.form)) |
|
204 |
# find out if we have some explicit `rql` needs |
|
205 |
rql = newparams.pop('rql', None) |
|
206 |
# if rql is needed (explicit __redirectrql or multiple deletions for |
|
207 |
# instance), we have to use the old `view?rql=...` form |
|
208 |
if rql: |
|
209 |
path = 'view' |
|
210 |
newparams['rql'] = rql |
|
211 |
elif '__redirectpath' in self.req.form: |
|
212 |
# if redirect path was explicitly specified in the form, use it |
|
213 |
path = self.req.form['__redirectpath'] |
|
214 |
elif self._after_deletion_path: |
|
215 |
# else it should have been set during form processing |
|
216 |
path, params = self._after_deletion_path |
|
217 |
params = dict(params) # params given as tuple |
|
218 |
params.update(newparams) |
|
219 |
newparams = params |
|
220 |
elif self._edited_entity: |
|
221 |
path = self._edited_entity.rest_path() |
|
222 |
else: |
|
223 |
path = 'view' |
|
224 |
url = self.build_url(path, **newparams) |
|
225 |
url = append_url_params(url, self.req.form.get('__redirectparams')) |
|
226 |
raise Redirect(url) |
|
1433 | 227 |
|
0 | 228 |
|
229 |
def _return_to_edition_view(self, newparams): |
|
230 |
"""apply-button case""" |
|
231 |
form = self.req.form |
|
232 |
if self._edited_entity: |
|
233 |
path = self._edited_entity.rest_path() |
|
234 |
newparams.pop('rql', None) |
|
235 |
# else, fallback on the old `view?rql=...` url form |
|
236 |
elif 'rql' in self.req.form: |
|
237 |
path = 'view' |
|
238 |
newparams['rql'] = form['rql'] |
|
239 |
else: |
|
240 |
self.warning("the edited data seems inconsistent") |
|
241 |
path = 'view' |
|
242 |
# pick up the correction edition view |
|
243 |
if form.get('__form_id'): |
|
244 |
newparams['vid'] = form['__form_id'] |
|
245 |
# re-insert copy redirection parameters |
|
246 |
for redirectparam in NAV_FORM_PARAMETERS: |
|
247 |
if redirectparam in form: |
|
248 |
newparams[redirectparam] = form[redirectparam] |
|
249 |
raise Redirect(self.build_url(path, **newparams)) |
|
250 |
||
251 |
||
252 |
def _return_to_lastpage(self, newparams): |
|
253 |
"""cancel-button case: in this case we are always expecting to go back |
|
254 |
where we came from, and this is not easy. Currently we suppose that |
|
255 |
__redirectpath is specifying that place if found, else we look in the |
|
256 |
request breadcrumbs for the last visited page. |
|
257 |
""" |
|
258 |
if '__redirectpath' in self.req.form: |
|
259 |
# if redirect path was explicitly specified in the form, use it |
|
260 |
path = self.req.form['__redirectpath'] |
|
261 |
url = self.build_url(path, **newparams) |
|
262 |
url = append_url_params(url, self.req.form.get('__redirectparams')) |
|
263 |
else: |
|
264 |
url = self.req.last_visited_page() |
|
265 |
raise Redirect(url) |
|
266 |
||
267 |
||
268 |
from cubicweb import set_log_methods |
|
269 |
set_log_methods(Controller, LOGGER) |
|
270 |