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