author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Wed, 29 Apr 2009 19:50:04 +0200 | |
branch | tls-sprint |
changeset 1560 | 7dd2a81b8bc8 |
parent 1527 | c8ca1782e252 |
child 1635 | 866563e2d0fc |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: utf-8 -*- |
2 |
"""Set of base controllers, which are directly plugged into the application |
|
3 |
object to handle publication. |
|
4 |
||
5 |
||
6 |
:organization: Logilab |
|
408
a8814ff6824e
reactivate tests and fix bug triggering removal of undesired relation (eg type restriction) in some cases
sylvain.thenault@logilab.fr
parents:
353
diff
changeset
|
7 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 8 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
9 |
""" |
|
10 |
__docformat__ = "restructuredtext en" |
|
11 |
||
12 |
from smtplib import SMTP |
|
13 |
||
14 |
import simplejson |
|
15 |
||
16 |
from logilab.common.decorators import cached |
|
17 |
||
945
912b604f0e42
missing import
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
882
diff
changeset
|
18 |
from cubicweb import NoSelectableObject, ValidationError, ObjectNotFound, typed_eid |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
945
diff
changeset
|
19 |
from cubicweb.utils import strptime |
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
644
diff
changeset
|
20 |
from cubicweb.selectors import yes, match_user_groups |
1419 | 21 |
from cubicweb.view import STRICT_DOCTYPE |
0 | 22 |
from cubicweb.common.mail import format_mail |
23 |
from cubicweb.web import ExplicitLogin, Redirect, RemoteCallFailed |
|
1309 | 24 |
from cubicweb.web.formrenderers import FormRenderer |
0 | 25 |
from cubicweb.web.controller import Controller |
26 |
from cubicweb.web.views import vid_from_rset |
|
27 |
try: |
|
28 |
from cubicweb.web.facet import (FilterRQLBuilder, get_facet, |
|
408
a8814ff6824e
reactivate tests and fix bug triggering removal of undesired relation (eg type restriction) in some cases
sylvain.thenault@logilab.fr
parents:
353
diff
changeset
|
29 |
prepare_facets_rqlst) |
0 | 30 |
HAS_SEARCH_RESTRICTION = True |
31 |
except ImportError: # gae |
|
32 |
HAS_SEARCH_RESTRICTION = False |
|
1419 | 33 |
|
34 |
||
35 |
def xhtml_wrap(source): |
|
36 |
head = u'<?xml version="1.0"?>\n' + STRICT_DOCTYPE |
|
37 |
return head + u'<div xmlns="http://www.w3.org/1999/xhtml" xmlns:cubicweb="http://www.logilab.org/2008/cubicweb">%s</div>' % source.strip() |
|
38 |
||
39 |
def jsonize(func): |
|
40 |
"""decorator to sets correct content_type and calls `simplejson.dumps` on |
|
41 |
results |
|
42 |
""" |
|
43 |
def wrapper(self, *args, **kwargs): |
|
44 |
self.req.set_content_type('application/json') |
|
45 |
result = func(self, *args, **kwargs) |
|
46 |
return simplejson.dumps(result) |
|
1527
c8ca1782e252
controller fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1467
diff
changeset
|
47 |
wrapper.__name__ = func.__name__ |
1419 | 48 |
return wrapper |
49 |
||
50 |
def xhtmlize(func): |
|
51 |
"""decorator to sets correct content_type and calls `xmlize` on results""" |
|
52 |
def wrapper(self, *args, **kwargs): |
|
53 |
self.req.set_content_type(self.req.html_content_type()) |
|
54 |
result = func(self, *args, **kwargs) |
|
55 |
return xhtml_wrap(result) |
|
1527
c8ca1782e252
controller fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1467
diff
changeset
|
56 |
wrapper.__name__ = func.__name__ |
1419 | 57 |
return wrapper |
58 |
||
59 |
def check_pageid(func): |
|
60 |
"""decorator which checks the given pageid is found in the |
|
61 |
user's session data |
|
62 |
""" |
|
63 |
def wrapper(self, *args, **kwargs): |
|
64 |
data = self.req.get_session_data(self.req.pageid) |
|
65 |
if data is None: |
|
66 |
raise RemoteCallFailed(self.req._('pageid-not-found')) |
|
67 |
return func(self, *args, **kwargs) |
|
68 |
return wrapper |
|
69 |
||
70 |
||
0 | 71 |
class LoginController(Controller): |
72 |
id = 'login' |
|
73 |
||
74 |
def publish(self, rset=None): |
|
75 |
"""log in the application""" |
|
76 |
if self.config['auth-mode'] == 'http': |
|
77 |
# HTTP authentication |
|
78 |
raise ExplicitLogin() |
|
79 |
else: |
|
80 |
# Cookie authentication |
|
81 |
return self.appli.need_login_content(self.req) |
|
82 |
||
1419 | 83 |
|
0 | 84 |
class LogoutController(Controller): |
85 |
id = 'logout' |
|
1419 | 86 |
|
0 | 87 |
def publish(self, rset=None): |
88 |
"""logout from the application""" |
|
89 |
return self.appli.session_handler.logout(self.req) |
|
90 |
||
91 |
||
92 |
class ViewController(Controller): |
|
823
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
93 |
"""standard entry point : |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
94 |
- build result set |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
95 |
- select and call main template |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
96 |
""" |
0 | 97 |
id = 'view' |
823
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
98 |
template = 'main-template' |
1419 | 99 |
|
0 | 100 |
def publish(self, rset=None): |
101 |
"""publish a request, returning an encoded string""" |
|
823
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
102 |
view, rset = self._select_view_and_rset(rset) |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
103 |
self.add_to_breadcrumbs(view) |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
104 |
self.validate_cache(view) |
882
75488a2a875e
fix ui.main-template property handling
sylvain.thenault@logilab.fr
parents:
823
diff
changeset
|
105 |
template = self.appli.main_template_id(self.req) |
823
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
106 |
return self.vreg.main_template(self.req, template, rset=rset, view=view) |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
107 |
|
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
108 |
def _select_view_and_rset(self, rset): |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
109 |
req = self.req |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
110 |
if rset is None and not hasattr(req, '_rql_processed'): |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
111 |
req._rql_processed = True |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
112 |
rset = self.process_rql(req.form.get('rql')) |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
113 |
if rset and rset.rowcount == 1 and '__method' in req.form: |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
114 |
entity = rset.get_entity(0, 0) |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
115 |
try: |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
116 |
method = getattr(entity, req.form.pop('__method')) |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
117 |
method() |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
118 |
except Exception, ex: |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
119 |
self.exception('while handling __method') |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
120 |
req.set_message(req._("error while handling __method: %s") % req._(ex)) |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
121 |
vid = req.form.get('vid') or vid_from_rset(req, rset, self.schema) |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
122 |
try: |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
123 |
view = self.vreg.select_view(vid, req, rset) |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
124 |
except ObjectNotFound: |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
125 |
self.warning("the view %s could not be found", vid) |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
126 |
req.set_message(req._("The view %s could not be found") % vid) |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
127 |
vid = vid_from_rset(req, rset, self.schema) |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
128 |
view = self.vreg.select_view(vid, req, rset) |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
129 |
except NoSelectableObject: |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
130 |
if rset: |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
131 |
req.set_message(req._("The view %s can not be applied to this query") % vid) |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
132 |
else: |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
133 |
req.set_message(req._("You have no access to this view or it's not applyable to current data")) |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
134 |
self.warning("the view %s can not be applied to this query", vid) |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
135 |
vid = vid_from_rset(req, rset, self.schema) |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
136 |
view = self.vreg.select_view(vid, req, rset) |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
137 |
return view, rset |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
138 |
|
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
139 |
def add_to_breadcrumbs(self, view): |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
140 |
# update breadcrumps **before** validating cache, unless the view |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
141 |
# specifies explicitly it should not be added to breadcrumb or the |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
142 |
# view is a binary view |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
143 |
if view.add_to_breadcrumbs and not view.binary: |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
144 |
self.req.update_breadcrumbs() |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
145 |
|
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
146 |
def validate_cache(self, view): |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
147 |
view.set_http_cache_headers() |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
148 |
self.req.validate_cache() |
0 | 149 |
|
150 |
def execute_linkto(self, eid=None): |
|
151 |
"""XXX __linkto parameter may cause security issue |
|
152 |
||
153 |
defined here since custom application controller inheriting from this |
|
154 |
one use this method? |
|
155 |
""" |
|
156 |
req = self.req |
|
157 |
if not '__linkto' in req.form: |
|
158 |
return |
|
159 |
if eid is None: |
|
160 |
eid = typed_eid(req.form['eid']) |
|
161 |
for linkto in req.list_form_param('__linkto', pop=True): |
|
162 |
rtype, eids, target = linkto.split(':') |
|
163 |
assert target in ('subject', 'object') |
|
164 |
eids = eids.split('_') |
|
165 |
if target == 'subject': |
|
166 |
rql = 'SET X %s Y WHERE X eid %%(x)s, Y eid %%(y)s' % rtype |
|
167 |
else: |
|
168 |
rql = 'SET Y %s X WHERE X eid %%(x)s, Y eid %%(y)s' % rtype |
|
169 |
for teid in eids: |
|
1419 | 170 |
req.execute(rql, {'x': eid, 'y': typed_eid(teid)}, ('x', 'y')) |
0 | 171 |
|
172 |
||
173 |
class FormValidatorController(Controller): |
|
174 |
id = 'validateform' |
|
175 |
||
176 |
def publish(self, rset=None): |
|
177 |
vreg = self.vreg |
|
178 |
try: |
|
179 |
ctrl = vreg.select(vreg.registry_objects('controllers', 'edit'), |
|
180 |
req=self.req, appli=self.appli) |
|
181 |
except NoSelectableObject: |
|
182 |
status, args = (False, {None: self.req._('not authorized')}) |
|
183 |
else: |
|
184 |
try: |
|
185 |
ctrl.publish(None, fromjson=True) |
|
186 |
except ValidationError, err: |
|
187 |
status, args = self.validation_error(err) |
|
188 |
except Redirect, err: |
|
189 |
try: |
|
190 |
self.req.cnx.commit() # ValidationError may be raise on commit |
|
191 |
except ValidationError, err: |
|
192 |
status, args = self.validation_error(err) |
|
193 |
else: |
|
194 |
status, args = (True, err.location) |
|
195 |
except Exception, err: |
|
196 |
self.req.cnx.rollback() |
|
197 |
self.exception('unexpected error in validateform') |
|
198 |
try: |
|
199 |
status, args = (False, self.req._(unicode(err))) |
|
200 |
except UnicodeError: |
|
201 |
status, args = (False, repr(err)) |
|
202 |
else: |
|
203 |
status, args = (False, '???') |
|
204 |
self.req.set_content_type('text/html') |
|
205 |
jsarg = simplejson.dumps( (status, args) ) |
|
206 |
return """<script type="text/javascript"> |
|
207 |
window.parent.handleFormValidationResponse('entityForm', null, %s); |
|
208 |
</script>""" % simplejson.dumps( (status, args) ) |
|
209 |
||
210 |
def validation_error(self, err): |
|
211 |
self.req.cnx.rollback() |
|
212 |
try: |
|
213 |
eid = err.entity.eid |
|
214 |
except AttributeError: |
|
215 |
eid = err.entity |
|
216 |
return (False, (eid, err.errors)) |
|
217 |
||
218 |
||
219 |
class JSonController(Controller): |
|
220 |
id = 'json' |
|
221 |
||
222 |
def publish(self, rset=None): |
|
1419 | 223 |
"""call js_* methods. Expected form keys: |
224 |
||
225 |
:fname: the method name without the js_ prefix |
|
226 |
:args: arguments list (json) |
|
227 |
||
228 |
note: it's the responsability of js_* methods to set the correct |
|
229 |
response content type |
|
230 |
""" |
|
0 | 231 |
self.req.pageid = self.req.form.get('pageid') |
1419 | 232 |
fname = self.req.form['fname'] |
233 |
try: |
|
234 |
func = getattr(self, 'js_%s' % fname) |
|
235 |
except AttributeError: |
|
236 |
raise RemoteCallFailed('no %s method' % fname) |
|
237 |
# no <arg> attribute means the callback takes no argument |
|
238 |
args = self.req.form.get('arg', ()) |
|
239 |
if not isinstance(args, (list, tuple)): |
|
240 |
args = (args,) |
|
241 |
args = [simplejson.loads(arg) for arg in args] |
|
0 | 242 |
try: |
1419 | 243 |
result = func(*args) |
244 |
except RemoteCallFailed: |
|
245 |
raise |
|
246 |
except Exception, ex: |
|
247 |
self.exception('an exception occured while calling js_%s(%s): %s', |
|
248 |
fname, args, ex) |
|
249 |
raise RemoteCallFailed(repr(ex)) |
|
250 |
if result is None: |
|
251 |
return '' |
|
252 |
# get unicode on @htmlize methods, encoded string on @jsonize methods |
|
253 |
elif isinstance(result, unicode): |
|
254 |
return result.encode(self.req.encoding) |
|
255 |
return result |
|
256 |
||
257 |
def _rebuild_posted_form(self, names, values, action=None): |
|
258 |
form = {} |
|
259 |
for name, value in zip(names, values): |
|
260 |
# remove possible __action_xxx inputs |
|
261 |
if name.startswith('__action'): |
|
262 |
continue |
|
263 |
# form.setdefault(name, []).append(value) |
|
264 |
if name in form: |
|
265 |
curvalue = form[name] |
|
266 |
if isinstance(curvalue, list): |
|
267 |
curvalue.append(value) |
|
268 |
else: |
|
269 |
form[name] = [curvalue, value] |
|
270 |
else: |
|
271 |
form[name] = value |
|
272 |
# simulate click on __action_%s button to help the controller |
|
273 |
if action: |
|
274 |
form['__action_%s' % action] = u'whatever' |
|
275 |
return form |
|
0 | 276 |
|
277 |
def _exec(self, rql, args=None, eidkey=None, rocheck=True): |
|
278 |
"""json mode: execute RQL and return resultset as json""" |
|
279 |
if rocheck: |
|
280 |
self.ensure_ro_rql(rql) |
|
281 |
try: |
|
282 |
return self.req.execute(rql, args, eidkey) |
|
283 |
except Exception, ex: |
|
284 |
self.exception("error in _exec(rql=%s): %s", rql, ex) |
|
285 |
return None |
|
286 |
return None |
|
287 |
||
1419 | 288 |
@xhtmlize |
289 |
def js_view(self): |
|
643
616191014b8b
[jsoncontroller] reorganize _html_exec (used by replacePageChunk) to output required css and js scripts
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
603
diff
changeset
|
290 |
# XXX try to use the page-content template |
0 | 291 |
req = self.req |
292 |
rql = req.form.get('rql') |
|
1419 | 293 |
if rql: |
0 | 294 |
rset = self._exec(rql) |
1419 | 295 |
else: |
296 |
rset = None |
|
0 | 297 |
vid = req.form.get('vid') or vid_from_rset(req, rset, self.schema) |
298 |
try: |
|
299 |
view = self.vreg.select_view(vid, req, rset) |
|
300 |
except NoSelectableObject: |
|
301 |
vid = req.form.get('fallbackvid', 'noresult') |
|
302 |
view = self.vreg.select_view(vid, req, rset) |
|
303 |
divid = req.form.get('divid', 'pageContent') |
|
304 |
# we need to call pagination before with the stream set |
|
305 |
stream = view.set_stream() |
|
306 |
if req.form.get('paginate'): |
|
307 |
if divid == 'pageContent': |
|
308 |
# mimick main template behaviour |
|
309 |
stream.write(u'<div id="pageContent">') |
|
310 |
vtitle = self.req.form.get('vtitle') |
|
311 |
if vtitle: |
|
447 | 312 |
stream.write(u'<h1 class="vtitle">%s</h1>\n' % vtitle) |
0 | 313 |
view.pagination(req, rset, view.w, not view.need_navigation) |
314 |
if divid == 'pageContent': |
|
315 |
stream.write(u'<div id="contentmain">') |
|
316 |
view.dispatch() |
|
643
616191014b8b
[jsoncontroller] reorganize _html_exec (used by replacePageChunk) to output required css and js scripts
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
603
diff
changeset
|
317 |
extresources = req.html_headers.getvalue(skiphead=True) |
808
8d739f6e8ef5
JsonController: only return an ajaxHtmlHead div if extra resources are needed
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
762
diff
changeset
|
318 |
if extresources: |
8d739f6e8ef5
JsonController: only return an ajaxHtmlHead div if extra resources are needed
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
762
diff
changeset
|
319 |
stream.write(u'<div class="ajaxHtmlHead">\n') # XXX use a widget ? |
8d739f6e8ef5
JsonController: only return an ajaxHtmlHead div if extra resources are needed
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
762
diff
changeset
|
320 |
stream.write(extresources) |
8d739f6e8ef5
JsonController: only return an ajaxHtmlHead div if extra resources are needed
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
762
diff
changeset
|
321 |
stream.write(u'</div>\n') |
0 | 322 |
if req.form.get('paginate') and divid == 'pageContent': |
323 |
stream.write(u'</div></div>') |
|
1419 | 324 |
return stream.getvalue() |
0 | 325 |
|
1419 | 326 |
@xhtmlize |
327 |
def js_prop_widget(self, propkey, varname, tabindex=None): |
|
328 |
"""specific method for CWProperty handling""" |
|
329 |
entity = self.vreg.etype_class('CWProperty')(self.req, None, None) |
|
330 |
entity.eid = varname |
|
331 |
entity['pkey'] = propkey |
|
332 |
form = self.vreg.select_object('forms', 'edition', self.req, None, |
|
333 |
entity=entity) |
|
334 |
form.form_build_context() |
|
335 |
vfield = form.field_by_name('value') |
|
336 |
renderer = FormRenderer() |
|
337 |
return vfield.render(form, renderer, tabindex=tabindex) \ |
|
338 |
+ renderer.render_help(form, vfield) |
|
0 | 339 |
|
1419 | 340 |
@xhtmlize |
341 |
def js_component(self, compid, rql, registry='components', extraargs=None): |
|
342 |
if rql: |
|
343 |
rset = self._exec(rql) |
|
344 |
else: |
|
345 |
rset = None |
|
346 |
comp = self.vreg.select_object(registry, compid, self.req, rset) |
|
347 |
if extraargs is None: |
|
348 |
extraargs = {} |
|
349 |
else: # we receive unicode keys which is not supported by the **syntax |
|
350 |
extraargs = dict((str(key), value) |
|
351 |
for key, value in extraargs.items()) |
|
352 |
extraargs = extraargs or {} |
|
353 |
return comp.dispatch(**extraargs) |
|
354 |
||
355 |
@check_pageid |
|
356 |
@xhtmlize |
|
357 |
def js_inline_creation_form(self, peid, ttype, rtype, role): |
|
358 |
view = self.vreg.select_view('inline-creation', self.req, None, |
|
359 |
etype=ttype, peid=peid, rtype=rtype, |
|
360 |
role=role) |
|
361 |
return view.dispatch(etype=ttype, peid=peid, rtype=rtype, role=role) |
|
362 |
||
363 |
@jsonize |
|
0 | 364 |
def js_validate_form(self, action, names, values): |
1527
c8ca1782e252
controller fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1467
diff
changeset
|
365 |
return self.validate_form(action, names, values) |
c8ca1782e252
controller fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1467
diff
changeset
|
366 |
|
c8ca1782e252
controller fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1467
diff
changeset
|
367 |
def validate_form(self, action, names, values): |
0 | 368 |
# XXX this method (and correspoding js calls) should use the new |
369 |
# `RemoteCallFailed` mechansim |
|
370 |
self.req.form = self._rebuild_posted_form(names, values, action) |
|
371 |
vreg = self.vreg |
|
372 |
try: |
|
373 |
ctrl = vreg.select(vreg.registry_objects('controllers', 'edit'), |
|
374 |
req=self.req) |
|
375 |
except NoSelectableObject: |
|
376 |
return (False, {None: self.req._('not authorized')}) |
|
377 |
try: |
|
378 |
ctrl.publish(None, fromjson=True) |
|
379 |
except ValidationError, err: |
|
380 |
self.req.cnx.rollback() |
|
381 |
if not err.entity or isinstance(err.entity, (long, int)): |
|
382 |
eid = err.entity |
|
383 |
else: |
|
384 |
eid = err.entity.eid |
|
385 |
return (False, (eid, err.errors)) |
|
386 |
except Redirect, err: |
|
387 |
return (True, err.location) |
|
388 |
except Exception, err: |
|
389 |
self.req.cnx.rollback() |
|
390 |
self.exception('unexpected error in js_validateform') |
|
391 |
return (False, self.req._(str(err))) |
|
392 |
return (False, '???') |
|
393 |
||
1419 | 394 |
@jsonize |
0 | 395 |
def js_edit_field(self, action, names, values, rtype, eid): |
1527
c8ca1782e252
controller fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1467
diff
changeset
|
396 |
success, args = self.validate_form(action, names, values) |
0 | 397 |
if success: |
1560
7dd2a81b8bc8
[basecontrollers] add edit_relation next to edit_field, misc notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1527
diff
changeset
|
398 |
# Any X,N where we don't seem to use N is an optimisation |
7dd2a81b8bc8
[basecontrollers] add edit_relation next to edit_field, misc notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1527
diff
changeset
|
399 |
# printable_value won't need to query N again |
0 | 400 |
rset = self.req.execute('Any X,N WHERE X eid %%(x)s, X %s N' % rtype, |
401 |
{'x': eid}, 'x') |
|
402 |
entity = rset.get_entity(0, 0) |
|
403 |
return (success, args, entity.printable_value(rtype)) |
|
404 |
else: |
|
405 |
return (success, args, None) |
|
1419 | 406 |
|
1560
7dd2a81b8bc8
[basecontrollers] add edit_relation next to edit_field, misc notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1527
diff
changeset
|
407 |
@jsonize |
7dd2a81b8bc8
[basecontrollers] add edit_relation next to edit_field, misc notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1527
diff
changeset
|
408 |
def js_edit_relation(self, action, names, values, |
7dd2a81b8bc8
[basecontrollers] add edit_relation next to edit_field, misc notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1527
diff
changeset
|
409 |
rtype, eid, role='subject', vid='list'): |
7dd2a81b8bc8
[basecontrollers] add edit_relation next to edit_field, misc notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1527
diff
changeset
|
410 |
# XXX validate_form |
7dd2a81b8bc8
[basecontrollers] add edit_relation next to edit_field, misc notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1527
diff
changeset
|
411 |
success, args = self.validate_form(action, names, values) |
7dd2a81b8bc8
[basecontrollers] add edit_relation next to edit_field, misc notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1527
diff
changeset
|
412 |
if success: |
7dd2a81b8bc8
[basecontrollers] add edit_relation next to edit_field, misc notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1527
diff
changeset
|
413 |
entity = self.req.eid_rset(eid).get_entity(0, 0) |
7dd2a81b8bc8
[basecontrollers] add edit_relation next to edit_field, misc notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1527
diff
changeset
|
414 |
rset = entity.related('person_in_charge', role) |
7dd2a81b8bc8
[basecontrollers] add edit_relation next to edit_field, misc notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1527
diff
changeset
|
415 |
return (success, args, self.view(vid, rset)) |
7dd2a81b8bc8
[basecontrollers] add edit_relation next to edit_field, misc notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1527
diff
changeset
|
416 |
else: |
7dd2a81b8bc8
[basecontrollers] add edit_relation next to edit_field, misc notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1527
diff
changeset
|
417 |
return (success, args, None) |
1419 | 418 |
|
419 |
@jsonize |
|
0 | 420 |
def js_i18n(self, msgids): |
421 |
"""returns the translation of `msgid`""" |
|
422 |
return [self.req._(msgid) for msgid in msgids] |
|
423 |
||
1419 | 424 |
@jsonize |
0 | 425 |
def js_format_date(self, strdate): |
426 |
"""returns the formatted date for `msgid`""" |
|
1380 | 427 |
date = strptime(strdate, '%Y-%m-%d %H:%M:%S') |
0 | 428 |
return self.format_date(date) |
429 |
||
1419 | 430 |
@jsonize |
0 | 431 |
def js_external_resource(self, resource): |
432 |
"""returns the URL of the external resource named `resource`""" |
|
433 |
return self.req.external_resource(resource) |
|
434 |
||
435 |
@check_pageid |
|
1419 | 436 |
@jsonize |
0 | 437 |
def js_user_callback(self, cbname): |
438 |
page_data = self.req.get_session_data(self.req.pageid, {}) |
|
439 |
try: |
|
440 |
cb = page_data[cbname] |
|
441 |
except KeyError: |
|
442 |
return None |
|
443 |
return cb(self.req) |
|
444 |
||
445 |
if HAS_SEARCH_RESTRICTION: |
|
1419 | 446 |
@jsonize |
0 | 447 |
def js_filter_build_rql(self, names, values): |
448 |
form = self._rebuild_posted_form(names, values) |
|
449 |
self.req.form = form |
|
450 |
builder = FilterRQLBuilder(self.req) |
|
451 |
return builder.build_rql() |
|
452 |
||
1419 | 453 |
@jsonize |
0 | 454 |
def js_filter_select_content(self, facetids, rql): |
455 |
rqlst = self.vreg.parse(self.req, rql) # XXX Union unsupported yet |
|
456 |
mainvar = prepare_facets_rqlst(rqlst)[0] |
|
457 |
update_map = {} |
|
458 |
for facetid in facetids: |
|
459 |
facet = get_facet(self.req, facetid, rqlst.children[0], mainvar) |
|
460 |
update_map[facetid] = facet.possible_values() |
|
461 |
return update_map |
|
462 |
||
1419 | 463 |
def js_unregister_user_callback(self, cbname): |
464 |
self.req.unregister_callback(self.req.pageid, cbname) |
|
465 |
||
466 |
def js_unload_page_data(self): |
|
467 |
self.req.del_session_data(self.req.pageid) |
|
468 |
||
469 |
def js_cancel_edition(self, errorurl): |
|
470 |
"""cancelling edition from javascript |
|
471 |
||
472 |
We need to clear associated req's data : |
|
473 |
- errorurl |
|
474 |
- pending insertions / deletions |
|
475 |
""" |
|
476 |
self.req.cancel_edition(errorurl) |
|
477 |
||
0 | 478 |
def js_delete_bookmark(self, beid): |
1419 | 479 |
rql = 'DELETE B bookmarked_by U WHERE B eid %(b)s, U eid %(u)s' |
480 |
self.req.execute(rql, {'b': typed_eid(beid), 'u' : self.req.user.eid}) |
|
481 |
||
482 |
def js_set_cookie(self, cookiename, cookievalue): |
|
483 |
# XXX we should consider jQuery.Cookie |
|
484 |
cookiename, cookievalue = str(cookiename), str(cookievalue) |
|
485 |
cookies = self.req.get_cookie() |
|
486 |
cookies[cookiename] = cookievalue |
|
487 |
self.req.set_cookie(cookies, cookiename) |
|
488 |
||
489 |
# relations edition stuff ################################################## |
|
0 | 490 |
|
491 |
def _add_pending(self, eidfrom, rel, eidto, kind): |
|
492 |
key = 'pending_%s' % kind |
|
493 |
pendings = self.req.get_session_data(key, set()) |
|
494 |
pendings.add( (typed_eid(eidfrom), rel, typed_eid(eidto)) ) |
|
495 |
self.req.set_session_data(key, pendings) |
|
496 |
||
497 |
def _remove_pending(self, eidfrom, rel, eidto, kind): |
|
1419 | 498 |
key = 'pending_%s' % kind |
0 | 499 |
try: |
500 |
pendings = self.req.get_session_data(key) |
|
501 |
pendings.remove( (typed_eid(eidfrom), rel, typed_eid(eidto)) ) |
|
502 |
except: |
|
503 |
self.exception('while removing pending eids') |
|
504 |
else: |
|
505 |
self.req.set_session_data(key, pendings) |
|
506 |
||
1419 | 507 |
def js_remove_pending_insert(self, (eidfrom, rel, eidto)): |
508 |
self._remove_pending(eidfrom, rel, eidto, 'insert') |
|
509 |
||
510 |
def js_add_pending_inserts(self, tripletlist): |
|
511 |
for eidfrom, rel, eidto in tripletlist: |
|
512 |
self._add_pending(eidfrom, rel, eidto, 'insert') |
|
513 |
||
514 |
def js_remove_pending_delete(self, (eidfrom, rel, eidto)): |
|
515 |
self._remove_pending(eidfrom, rel, eidto, 'delete') |
|
516 |
||
517 |
def js_add_pending_delete(self, (eidfrom, rel, eidto)): |
|
518 |
self._add_pending(eidfrom, rel, eidto, 'delete') |
|
519 |
||
520 |
# XXX specific code. Kill me and my AddComboBox friend |
|
521 |
@jsonize |
|
0 | 522 |
def js_add_and_link_new_entity(self, etype_to, rel, eid_to, etype_from, value_from): |
523 |
# create a new entity |
|
524 |
eid_from = self.req.execute('INSERT %s T : T name "%s"' % ( etype_from, value_from ))[0][0] |
|
525 |
# link the new entity to the main entity |
|
526 |
rql = 'SET F %(rel)s T WHERE F eid %(eid_to)s, T eid %(eid_from)s' % {'rel' : rel, 'eid_to' : eid_to, 'eid_from' : eid_from} |
|
527 |
return eid_from |
|
603
18c6c31bbaf4
[controllers] a set_cookie method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
581
diff
changeset
|
528 |
|
18c6c31bbaf4
[controllers] a set_cookie method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
581
diff
changeset
|
529 |
|
0 | 530 |
class SendMailController(Controller): |
531 |
id = 'sendmail' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
532 |
__select__ = match_user_groups('managers', 'users') |
0 | 533 |
|
534 |
def recipients(self): |
|
535 |
"""returns an iterator on email's recipients as entities""" |
|
536 |
eids = self.req.form['recipient'] |
|
537 |
# make sure we have a list even though only one recipient was specified |
|
538 |
if isinstance(eids, basestring): |
|
539 |
eids = (eids,) |
|
540 |
rql = 'Any X WHERE X eid in (%s)' % (','.join(eids)) |
|
541 |
rset = self.req.execute(rql) |
|
542 |
for entity in rset.entities(): |
|
543 |
entity.complete() # XXX really? |
|
544 |
yield entity |
|
545 |
||
546 |
@property |
|
547 |
@cached |
|
548 |
def smtp(self): |
|
549 |
mailhost, port = self.config['smtp-host'], self.config['smtp-port'] |
|
550 |
try: |
|
551 |
return SMTP(mailhost, port) |
|
552 |
except Exception, ex: |
|
553 |
self.exception("can't connect to smtp server %s:%s (%s)", |
|
554 |
mailhost, port, ex) |
|
555 |
url = self.build_url(__message=self.req._('could not connect to the SMTP server')) |
|
556 |
raise Redirect(url) |
|
557 |
||
558 |
def sendmail(self, recipient, subject, body): |
|
559 |
helo_addr = '%s <%s>' % (self.config['sender-name'], |
|
560 |
self.config['sender-addr']) |
|
561 |
msg = format_mail({'email' : self.req.user.get_email(), |
|
562 |
'name' : self.req.user.dc_title(),}, |
|
563 |
[recipient], body, subject) |
|
1419 | 564 |
self.smtp.sendmail(helo_addr, [recipient], msg.as_string()) |
0 | 565 |
|
566 |
def publish(self, rset=None): |
|
567 |
# XXX this allow anybody with access to an cubicweb application to use it as a mail relay |
|
568 |
body = self.req.form['mailbody'] |
|
1467
972517be96dc
sendmail form should now work as before
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1419
diff
changeset
|
569 |
subject = self.req.form['subject'] |
0 | 570 |
for recipient in self.recipients(): |
571 |
text = body % recipient.as_email_context() |
|
572 |
self.sendmail(recipient.get_email(), subject, text) |
|
573 |
# breadcrumbs = self.req.get_session_data('breadcrumbs', None) |
|
574 |
url = self.build_url(__message=self.req._('emails successfully sent')) |
|
575 |
raise Redirect(url) |
|
576 |
||
577 |
||
578 |
class MailBugReportController(SendMailController): |
|
579 |
id = 'reportbug' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
580 |
__select__ = yes() |
0 | 581 |
|
582 |
def publish(self, rset=None): |
|
583 |
body = self.req.form['description'] |
|
584 |
self.sendmail(self.config['submit-mail'], _('%s error report') % self.config.appid, body) |
|
585 |
url = self.build_url(__message=self.req._('bug report sent')) |
|
586 |
raise Redirect(url) |
|
1419 | 587 |