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