author | sylvain.thenault@logilab.fr |
Wed, 13 May 2009 17:00:08 +0200 | |
branch | tls-sprint |
changeset 1788 | d6e6ad70e50a |
parent 1760 | 6b97d286eb5a |
child 1795 | abffc24f93c5 |
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') |
|
1759
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1736
diff
changeset
|
390 |
return (False, self.req._(str(err).decode('utf-8'))) |
0 | 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 |
1759
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1736
diff
changeset
|
407 |
def js_edit_relation(self, action, names, values, |
1760
6b97d286eb5a
[reledit] relation edition : cleanup a bit #343123
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1759
diff
changeset
|
408 |
rtype, eid, vid, role='subject'): |
1759
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1736
diff
changeset
|
409 |
success, args = self.validate_form(action, names, values) |
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1736
diff
changeset
|
410 |
if success: |
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1736
diff
changeset
|
411 |
entity = self.req.eid_rset(eid).get_entity(0, 0) |
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1736
diff
changeset
|
412 |
rset = entity.related(rtype, role) |
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1736
diff
changeset
|
413 |
return (success, args, self.view(vid, rset)) |
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1736
diff
changeset
|
414 |
else: |
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1736
diff
changeset
|
415 |
return (success, args, None) |
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1736
diff
changeset
|
416 |
|
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1736
diff
changeset
|
417 |
@jsonize |
0 | 418 |
def js_i18n(self, msgids): |
419 |
"""returns the translation of `msgid`""" |
|
420 |
return [self.req._(msgid) for msgid in msgids] |
|
421 |
||
1419 | 422 |
@jsonize |
0 | 423 |
def js_format_date(self, strdate): |
424 |
"""returns the formatted date for `msgid`""" |
|
1380 | 425 |
date = strptime(strdate, '%Y-%m-%d %H:%M:%S') |
0 | 426 |
return self.format_date(date) |
427 |
||
1419 | 428 |
@jsonize |
0 | 429 |
def js_external_resource(self, resource): |
430 |
"""returns the URL of the external resource named `resource`""" |
|
431 |
return self.req.external_resource(resource) |
|
432 |
||
433 |
@check_pageid |
|
1419 | 434 |
@jsonize |
0 | 435 |
def js_user_callback(self, cbname): |
436 |
page_data = self.req.get_session_data(self.req.pageid, {}) |
|
437 |
try: |
|
438 |
cb = page_data[cbname] |
|
439 |
except KeyError: |
|
440 |
return None |
|
441 |
return cb(self.req) |
|
442 |
||
443 |
if HAS_SEARCH_RESTRICTION: |
|
1419 | 444 |
@jsonize |
0 | 445 |
def js_filter_build_rql(self, names, values): |
446 |
form = self._rebuild_posted_form(names, values) |
|
447 |
self.req.form = form |
|
448 |
builder = FilterRQLBuilder(self.req) |
|
449 |
return builder.build_rql() |
|
450 |
||
1419 | 451 |
@jsonize |
0 | 452 |
def js_filter_select_content(self, facetids, rql): |
453 |
rqlst = self.vreg.parse(self.req, rql) # XXX Union unsupported yet |
|
454 |
mainvar = prepare_facets_rqlst(rqlst)[0] |
|
455 |
update_map = {} |
|
456 |
for facetid in facetids: |
|
457 |
facet = get_facet(self.req, facetid, rqlst.children[0], mainvar) |
|
458 |
update_map[facetid] = facet.possible_values() |
|
459 |
return update_map |
|
460 |
||
1419 | 461 |
def js_unregister_user_callback(self, cbname): |
462 |
self.req.unregister_callback(self.req.pageid, cbname) |
|
463 |
||
464 |
def js_unload_page_data(self): |
|
465 |
self.req.del_session_data(self.req.pageid) |
|
466 |
||
467 |
def js_cancel_edition(self, errorurl): |
|
468 |
"""cancelling edition from javascript |
|
469 |
||
470 |
We need to clear associated req's data : |
|
471 |
- errorurl |
|
472 |
- pending insertions / deletions |
|
473 |
""" |
|
474 |
self.req.cancel_edition(errorurl) |
|
475 |
||
0 | 476 |
def js_delete_bookmark(self, beid): |
1419 | 477 |
rql = 'DELETE B bookmarked_by U WHERE B eid %(b)s, U eid %(u)s' |
478 |
self.req.execute(rql, {'b': typed_eid(beid), 'u' : self.req.user.eid}) |
|
479 |
||
480 |
def js_set_cookie(self, cookiename, cookievalue): |
|
481 |
# XXX we should consider jQuery.Cookie |
|
482 |
cookiename, cookievalue = str(cookiename), str(cookievalue) |
|
483 |
cookies = self.req.get_cookie() |
|
484 |
cookies[cookiename] = cookievalue |
|
485 |
self.req.set_cookie(cookies, cookiename) |
|
486 |
||
487 |
# relations edition stuff ################################################## |
|
0 | 488 |
|
489 |
def _add_pending(self, eidfrom, rel, eidto, kind): |
|
490 |
key = 'pending_%s' % kind |
|
491 |
pendings = self.req.get_session_data(key, set()) |
|
492 |
pendings.add( (typed_eid(eidfrom), rel, typed_eid(eidto)) ) |
|
493 |
self.req.set_session_data(key, pendings) |
|
494 |
||
495 |
def _remove_pending(self, eidfrom, rel, eidto, kind): |
|
1419 | 496 |
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
|
497 |
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
|
498 |
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
|
499 |
self.req.set_session_data(key, pendings) |
0 | 500 |
|
1419 | 501 |
def js_remove_pending_insert(self, (eidfrom, rel, eidto)): |
502 |
self._remove_pending(eidfrom, rel, eidto, 'insert') |
|
503 |
||
504 |
def js_add_pending_inserts(self, tripletlist): |
|
505 |
for eidfrom, rel, eidto in tripletlist: |
|
506 |
self._add_pending(eidfrom, rel, eidto, 'insert') |
|
507 |
||
508 |
def js_remove_pending_delete(self, (eidfrom, rel, eidto)): |
|
509 |
self._remove_pending(eidfrom, rel, eidto, 'delete') |
|
510 |
||
511 |
def js_add_pending_delete(self, (eidfrom, rel, eidto)): |
|
512 |
self._add_pending(eidfrom, rel, eidto, 'delete') |
|
513 |
||
514 |
# XXX specific code. Kill me and my AddComboBox friend |
|
515 |
@jsonize |
|
0 | 516 |
def js_add_and_link_new_entity(self, etype_to, rel, eid_to, etype_from, value_from): |
517 |
# create a new entity |
|
518 |
eid_from = self.req.execute('INSERT %s T : T name "%s"' % ( etype_from, value_from ))[0][0] |
|
519 |
# link the new entity to the main entity |
|
520 |
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} |
|
521 |
return eid_from |
|
603
18c6c31bbaf4
[controllers] a set_cookie method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
581
diff
changeset
|
522 |
|
18c6c31bbaf4
[controllers] a set_cookie method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
581
diff
changeset
|
523 |
|
0 | 524 |
class SendMailController(Controller): |
525 |
id = 'sendmail' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
526 |
__select__ = match_user_groups('managers', 'users') |
0 | 527 |
|
528 |
def recipients(self): |
|
529 |
"""returns an iterator on email's recipients as entities""" |
|
530 |
eids = self.req.form['recipient'] |
|
531 |
# make sure we have a list even though only one recipient was specified |
|
532 |
if isinstance(eids, basestring): |
|
533 |
eids = (eids,) |
|
534 |
rql = 'Any X WHERE X eid in (%s)' % (','.join(eids)) |
|
535 |
rset = self.req.execute(rql) |
|
536 |
for entity in rset.entities(): |
|
537 |
entity.complete() # XXX really? |
|
538 |
yield entity |
|
539 |
||
540 |
@property |
|
541 |
@cached |
|
542 |
def smtp(self): |
|
543 |
mailhost, port = self.config['smtp-host'], self.config['smtp-port'] |
|
544 |
try: |
|
545 |
return SMTP(mailhost, port) |
|
546 |
except Exception, ex: |
|
547 |
self.exception("can't connect to smtp server %s:%s (%s)", |
|
548 |
mailhost, port, ex) |
|
549 |
url = self.build_url(__message=self.req._('could not connect to the SMTP server')) |
|
550 |
raise Redirect(url) |
|
551 |
||
552 |
def sendmail(self, recipient, subject, body): |
|
553 |
helo_addr = '%s <%s>' % (self.config['sender-name'], |
|
554 |
self.config['sender-addr']) |
|
555 |
msg = format_mail({'email' : self.req.user.get_email(), |
|
556 |
'name' : self.req.user.dc_title(),}, |
|
557 |
[recipient], body, subject) |
|
1419 | 558 |
self.smtp.sendmail(helo_addr, [recipient], msg.as_string()) |
0 | 559 |
|
560 |
def publish(self, rset=None): |
|
561 |
# XXX this allow anybody with access to an cubicweb application to use it as a mail relay |
|
562 |
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
|
563 |
subject = self.req.form['subject'] |
0 | 564 |
for recipient in self.recipients(): |
565 |
text = body % recipient.as_email_context() |
|
566 |
self.sendmail(recipient.get_email(), subject, text) |
|
567 |
# breadcrumbs = self.req.get_session_data('breadcrumbs', None) |
|
568 |
url = self.build_url(__message=self.req._('emails successfully sent')) |
|
569 |
raise Redirect(url) |
|
570 |
||
571 |
||
572 |
class MailBugReportController(SendMailController): |
|
573 |
id = 'reportbug' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
574 |
__select__ = yes() |
0 | 575 |
|
576 |
def publish(self, rset=None): |
|
577 |
body = self.req.form['description'] |
|
578 |
self.sendmail(self.config['submit-mail'], _('%s error report') % self.config.appid, body) |
|
579 |
url = self.build_url(__message=self.req._('bug report sent')) |
|
580 |
raise Redirect(url) |
|
1419 | 581 |