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