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