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