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