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