author | Katia Saurfelt <katia.saurfelt@logilab.fr> |
Tue, 19 May 2009 14:31:42 +0200 | |
branch | stable |
changeset 1868 | af1ae863606d |
parent 1857 | 46f975203f4f |
child 1889 | 874a055c373b |
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"> |
|
1868
af1ae863606d
update handleFormValidationResponse calls
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
1857
diff
changeset
|
211 |
window.parent.handleFormValidationResponse('entityForm', null, null, %s); |
0 | 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() |
|
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
|
385 |
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
|
386 |
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
|
387 |
return (True, redir.location) |
0 | 388 |
except Exception, err: |
389 |
self.req.cnx.rollback() |
|
390 |
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
|
391 |
return (False, self.req._(str(err).decode('utf-8'))) |
0 | 392 |
return (False, '???') |
393 |
||
1419 | 394 |
@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
|
395 |
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
|
396 |
success, args = self.validate_form(action, names, values) |
0 | 397 |
if success: |
1560
7dd2a81b8bc8
[basecontrollers] add edit_relation next to edit_field, misc notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1527
diff
changeset
|
398 |
# 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
|
399 |
# printable_value won't need to query N again |
0 | 400 |
rset = self.req.execute('Any X,N WHERE X eid %%(x)s, X %s N' % rtype, |
401 |
{'x': eid}, 'x') |
|
402 |
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
|
403 |
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
|
404 |
return (success, args, value or default) |
0 | 405 |
else: |
406 |
return (success, args, None) |
|
1419 | 407 |
|
1560
7dd2a81b8bc8
[basecontrollers] add edit_relation next to edit_field, misc notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1527
diff
changeset
|
408 |
@jsonize |
1759
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1736
diff
changeset
|
409 |
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
|
410 |
rtype, role, eid, vid, default): |
1759
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1736
diff
changeset
|
411 |
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
|
412 |
if success: |
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1736
diff
changeset
|
413 |
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
|
414 |
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
|
415 |
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
|
416 |
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
|
417 |
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
|
418 |
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
|
419 |
return (success, args, output) |
1759
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1736
diff
changeset
|
420 |
else: |
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1736
diff
changeset
|
421 |
return (success, args, None) |
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1736
diff
changeset
|
422 |
|
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1736
diff
changeset
|
423 |
@jsonize |
0 | 424 |
def js_i18n(self, msgids): |
425 |
"""returns the translation of `msgid`""" |
|
426 |
return [self.req._(msgid) for msgid in msgids] |
|
427 |
||
1419 | 428 |
@jsonize |
0 | 429 |
def js_format_date(self, strdate): |
430 |
"""returns the formatted date for `msgid`""" |
|
1380 | 431 |
date = strptime(strdate, '%Y-%m-%d %H:%M:%S') |
0 | 432 |
return self.format_date(date) |
433 |
||
1419 | 434 |
@jsonize |
0 | 435 |
def js_external_resource(self, resource): |
436 |
"""returns the URL of the external resource named `resource`""" |
|
437 |
return self.req.external_resource(resource) |
|
438 |
||
439 |
@check_pageid |
|
1419 | 440 |
@jsonize |
0 | 441 |
def js_user_callback(self, cbname): |
442 |
page_data = self.req.get_session_data(self.req.pageid, {}) |
|
443 |
try: |
|
444 |
cb = page_data[cbname] |
|
445 |
except KeyError: |
|
446 |
return None |
|
447 |
return cb(self.req) |
|
448 |
||
449 |
if HAS_SEARCH_RESTRICTION: |
|
1419 | 450 |
@jsonize |
0 | 451 |
def js_filter_build_rql(self, names, values): |
452 |
form = self._rebuild_posted_form(names, values) |
|
453 |
self.req.form = form |
|
454 |
builder = FilterRQLBuilder(self.req) |
|
455 |
return builder.build_rql() |
|
456 |
||
1419 | 457 |
@jsonize |
0 | 458 |
def js_filter_select_content(self, facetids, rql): |
459 |
rqlst = self.vreg.parse(self.req, rql) # XXX Union unsupported yet |
|
460 |
mainvar = prepare_facets_rqlst(rqlst)[0] |
|
461 |
update_map = {} |
|
462 |
for facetid in facetids: |
|
463 |
facet = get_facet(self.req, facetid, rqlst.children[0], mainvar) |
|
464 |
update_map[facetid] = facet.possible_values() |
|
465 |
return update_map |
|
466 |
||
1419 | 467 |
def js_unregister_user_callback(self, cbname): |
468 |
self.req.unregister_callback(self.req.pageid, cbname) |
|
469 |
||
470 |
def js_unload_page_data(self): |
|
471 |
self.req.del_session_data(self.req.pageid) |
|
472 |
||
473 |
def js_cancel_edition(self, errorurl): |
|
474 |
"""cancelling edition from javascript |
|
475 |
||
476 |
We need to clear associated req's data : |
|
477 |
- errorurl |
|
478 |
- pending insertions / deletions |
|
479 |
""" |
|
480 |
self.req.cancel_edition(errorurl) |
|
481 |
||
0 | 482 |
def js_delete_bookmark(self, beid): |
1419 | 483 |
rql = 'DELETE B bookmarked_by U WHERE B eid %(b)s, U eid %(u)s' |
484 |
self.req.execute(rql, {'b': typed_eid(beid), 'u' : self.req.user.eid}) |
|
485 |
||
1844
ec51bf1b8be3
avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1839
diff
changeset
|
486 |
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
|
487 |
"""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
|
488 |
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
|
489 |
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
|
490 |
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
|
491 |
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
|
492 |
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
|
493 |
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
|
494 |
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
|
495 |
else: |
ec51bf1b8be3
avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1839
diff
changeset
|
496 |
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
|
497 |
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
|
498 |
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
|
499 |
else: |
ec51bf1b8be3
avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1839
diff
changeset
|
500 |
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
|
501 |
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
|
502 |
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
|
503 |
|
1419 | 504 |
def js_set_cookie(self, cookiename, cookievalue): |
505 |
# XXX we should consider jQuery.Cookie |
|
506 |
cookiename, cookievalue = str(cookiename), str(cookievalue) |
|
507 |
cookies = self.req.get_cookie() |
|
508 |
cookies[cookiename] = cookievalue |
|
509 |
self.req.set_cookie(cookies, cookiename) |
|
510 |
||
511 |
# relations edition stuff ################################################## |
|
0 | 512 |
|
513 |
def _add_pending(self, eidfrom, rel, eidto, kind): |
|
514 |
key = 'pending_%s' % kind |
|
515 |
pendings = self.req.get_session_data(key, set()) |
|
516 |
pendings.add( (typed_eid(eidfrom), rel, typed_eid(eidto)) ) |
|
517 |
self.req.set_session_data(key, pendings) |
|
518 |
||
519 |
def _remove_pending(self, eidfrom, rel, eidto, kind): |
|
1419 | 520 |
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
|
521 |
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
|
522 |
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
|
523 |
self.req.set_session_data(key, pendings) |
0 | 524 |
|
1419 | 525 |
def js_remove_pending_insert(self, (eidfrom, rel, eidto)): |
526 |
self._remove_pending(eidfrom, rel, eidto, 'insert') |
|
527 |
||
528 |
def js_add_pending_inserts(self, tripletlist): |
|
529 |
for eidfrom, rel, eidto in tripletlist: |
|
530 |
self._add_pending(eidfrom, rel, eidto, 'insert') |
|
531 |
||
532 |
def js_remove_pending_delete(self, (eidfrom, rel, eidto)): |
|
533 |
self._remove_pending(eidfrom, rel, eidto, 'delete') |
|
534 |
||
535 |
def js_add_pending_delete(self, (eidfrom, rel, eidto)): |
|
536 |
self._add_pending(eidfrom, rel, eidto, 'delete') |
|
537 |
||
538 |
# XXX specific code. Kill me and my AddComboBox friend |
|
539 |
@jsonize |
|
0 | 540 |
def js_add_and_link_new_entity(self, etype_to, rel, eid_to, etype_from, value_from): |
541 |
# create a new entity |
|
542 |
eid_from = self.req.execute('INSERT %s T : T name "%s"' % ( etype_from, value_from ))[0][0] |
|
543 |
# link the new entity to the main entity |
|
544 |
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} |
|
545 |
return eid_from |
|
603
18c6c31bbaf4
[controllers] a set_cookie method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
581
diff
changeset
|
546 |
|
18c6c31bbaf4
[controllers] a set_cookie method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
581
diff
changeset
|
547 |
|
0 | 548 |
class SendMailController(Controller): |
549 |
id = 'sendmail' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
550 |
__select__ = match_user_groups('managers', 'users') |
0 | 551 |
|
552 |
def recipients(self): |
|
553 |
"""returns an iterator on email's recipients as entities""" |
|
554 |
eids = self.req.form['recipient'] |
|
555 |
# make sure we have a list even though only one recipient was specified |
|
556 |
if isinstance(eids, basestring): |
|
557 |
eids = (eids,) |
|
558 |
rql = 'Any X WHERE X eid in (%s)' % (','.join(eids)) |
|
559 |
rset = self.req.execute(rql) |
|
560 |
for entity in rset.entities(): |
|
561 |
entity.complete() # XXX really? |
|
562 |
yield entity |
|
563 |
||
564 |
@property |
|
565 |
@cached |
|
566 |
def smtp(self): |
|
567 |
mailhost, port = self.config['smtp-host'], self.config['smtp-port'] |
|
568 |
try: |
|
569 |
return SMTP(mailhost, port) |
|
570 |
except Exception, ex: |
|
571 |
self.exception("can't connect to smtp server %s:%s (%s)", |
|
572 |
mailhost, port, ex) |
|
573 |
url = self.build_url(__message=self.req._('could not connect to the SMTP server')) |
|
574 |
raise Redirect(url) |
|
575 |
||
576 |
def sendmail(self, recipient, subject, body): |
|
577 |
helo_addr = '%s <%s>' % (self.config['sender-name'], |
|
578 |
self.config['sender-addr']) |
|
579 |
msg = format_mail({'email' : self.req.user.get_email(), |
|
580 |
'name' : self.req.user.dc_title(),}, |
|
581 |
[recipient], body, subject) |
|
1419 | 582 |
self.smtp.sendmail(helo_addr, [recipient], msg.as_string()) |
0 | 583 |
|
584 |
def publish(self, rset=None): |
|
585 |
# XXX this allow anybody with access to an cubicweb application to use it as a mail relay |
|
586 |
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
|
587 |
subject = self.req.form['subject'] |
0 | 588 |
for recipient in self.recipients(): |
589 |
text = body % recipient.as_email_context() |
|
590 |
self.sendmail(recipient.get_email(), subject, text) |
|
591 |
# breadcrumbs = self.req.get_session_data('breadcrumbs', None) |
|
592 |
url = self.build_url(__message=self.req._('emails successfully sent')) |
|
593 |
raise Redirect(url) |
|
594 |
||
595 |
||
596 |
class MailBugReportController(SendMailController): |
|
597 |
id = 'reportbug' |
|
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
598 |
__select__ = yes() |
0 | 599 |
|
600 |
def publish(self, rset=None): |
|
601 |
body = self.req.form['description'] |
|
602 |
self.sendmail(self.config['submit-mail'], _('%s error report') % self.config.appid, body) |
|
603 |
url = self.build_url(__message=self.req._('bug report sent')) |
|
604 |
raise Redirect(url) |
|
1419 | 605 |