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