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