author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 13 Oct 2009 08:47:41 +0200 | |
changeset 3645 | 7272b793d7c9 |
parent 3629 | 559cad62c786 |
child 3659 | 993997b4b41d |
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): |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
39 |
self._cw.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): |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
47 |
self._cw.set_content_type(self._cw.html_content_type()) |
1419 | 48 |
result = func(self, *args, **kwargs) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
49 |
return ''.join((self._cw.document_surrounding_div(), result.strip(), |
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
|
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): |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
59 |
data = self._cw.get_session_data(self._cw.pageid) |
1419 | 60 |
if data is None: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
61 |
raise RemoteCallFailed(self._cw._('pageid-not-found')) |
1419 | 62 |
return func(self, *args, **kwargs) |
63 |
return wrapper |
|
64 |
||
65 |
||
0 | 66 |
class LoginController(Controller): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
67 |
__regid__ = 'login' |
0 | 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""" |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
71 |
if self._cw.config['auth-mode'] == 'http': |
0 | 72 |
# HTTP authentication |
73 |
raise ExplicitLogin() |
|
74 |
else: |
|
75 |
# Cookie authentication |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
76 |
return self.appli.need_login_content(self._cw) |
0 | 77 |
|
1419 | 78 |
|
0 | 79 |
class LogoutController(Controller): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
80 |
__regid__ = '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""" |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
84 |
return self.appli.session_handler.logout(self._cw) |
0 | 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 |
""" |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
92 |
__regid__ = '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) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
100 |
template = self.appli.main_template_id(self._cw) |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
101 |
return self._cw.vreg['views'].main_template(self._cw, template, |
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
|
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): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
105 |
req = self._cw |
823
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)) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
119 |
vid = req.form.get('vid') or vid_from_rset(req, rset, self._cw.schema) |
823
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
120 |
try: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
121 |
view = self._cw.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) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
125 |
vid = vid_from_rset(req, rset, self._cw.schema) |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
126 |
view = self._cw.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) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
134 |
vid = vid_from_rset(req, rset, self._cw.schema) |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
135 |
view = self._cw.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: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
143 |
self._cw.update_breadcrumbs() |
823
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() |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
147 |
self._cw.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 |
""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
155 |
req = self._cw |
0 | 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: |
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
|
195 |
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
|
196 |
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
|
197 |
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
|
198 |
return (False, _validation_error(req, ex), ctrl._edited_entity) |
3571
54743fc6172c
[json validation] catch everything here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3518
diff
changeset
|
199 |
except Exception, ex: |
54743fc6172c
[json validation] catch everything here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3518
diff
changeset
|
200 |
req.cnx.rollback() |
54743fc6172c
[json validation] catch everything here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3518
diff
changeset
|
201 |
req.exception('unexpected error while validating form') |
54743fc6172c
[json validation] catch everything here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3518
diff
changeset
|
202 |
return (False, req._(str(ex).decode('utf-8')), 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 |
else: |
3608
5a46e68c3d3c
[editcontroller] backout (sort of) removal of entity.complete() in validate_form
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3599
diff
changeset
|
204 |
# complete entity: it can be used in js callbacks where we might |
5a46e68c3d3c
[editcontroller] backout (sort of) removal of entity.complete() in validate_form
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3599
diff
changeset
|
205 |
# want every possible information |
5a46e68c3d3c
[editcontroller] backout (sort of) removal of entity.complete() in validate_form
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3599
diff
changeset
|
206 |
if ctrl._edited_entity: |
5a46e68c3d3c
[editcontroller] backout (sort of) removal of entity.complete() in validate_form
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3599
diff
changeset
|
207 |
ctrl._edited_entity.complete() |
3232
eccb7380dc3b
[controllers] allow onsuccess / onfailure callback to be passed to validateform
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3144
diff
changeset
|
208 |
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
|
209 |
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
|
210 |
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
|
211 |
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
|
212 |
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
|
213 |
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
|
214 |
|
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
|
215 |
|
0 | 216 |
class FormValidatorController(Controller): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
217 |
__regid__ = 'validateform' |
0 | 218 |
|
3232
eccb7380dc3b
[controllers] allow onsuccess / onfailure callback to be passed to validateform
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3144
diff
changeset
|
219 |
def response(self, domid, status, args, entity): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
220 |
callback = str(self._cw.form.get('__onsuccess', 'null')) |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
221 |
errback = str(self._cw.form.get('__onfailure', 'null')) |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
222 |
cbargs = str(self._cw.form.get('__cbargs', 'null')) |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
223 |
self._cw.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
|
224 |
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
|
225 |
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
|
226 |
wp = window.parent; |
3343
383b42263bb1
[validatecontroller] allow additional args to be passed to the js callback
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3327
diff
changeset
|
227 |
window.parent.handleFormValidationResponse('%s', %s, %s, %s, %s); |
383b42263bb1
[validatecontroller] allow additional args to be passed to the js callback
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3327
diff
changeset
|
228 |
</script>""" % (domid, callback, errback, jsargs, cbargs) |
2557
200985d3258d
make it easy to change response of FormValidatorController
Florent <florent@secondweb.fr>
parents:
2555
diff
changeset
|
229 |
|
0 | 230 |
def publish(self, rset=None): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
231 |
self._cw.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
|
232 |
# 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
|
233 |
# js_validate_form on the json controller |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
234 |
status, args, entity = _validate_form(self._cw, self._cw.vreg) |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
235 |
domid = self._cw.form.get('__domid', 'entityForm').encode( |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
236 |
self._cw.encoding) |
3232
eccb7380dc3b
[controllers] allow onsuccess / onfailure callback to be passed to validateform
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3144
diff
changeset
|
237 |
return self.response(domid, status, args, entity) |
0 | 238 |
|
239 |
||
240 |
class JSonController(Controller): |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
241 |
__regid__ = 'json' |
0 | 242 |
|
243 |
def publish(self, rset=None): |
|
1419 | 244 |
"""call js_* methods. Expected form keys: |
245 |
||
246 |
:fname: the method name without the js_ prefix |
|
247 |
:args: arguments list (json) |
|
248 |
||
249 |
note: it's the responsability of js_* methods to set the correct |
|
250 |
response content type |
|
251 |
""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
252 |
self._cw.json_request = True |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
253 |
self._cw.pageid = self._cw.form.get('pageid') |
1419 | 254 |
try: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
255 |
fname = self._cw.form['fname'] |
1419 | 256 |
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
|
257 |
except KeyError: |
aff0950c54c4
proper error when fname isn't specified
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2045
diff
changeset
|
258 |
raise RemoteCallFailed('no method specified') |
1419 | 259 |
except AttributeError: |
260 |
raise RemoteCallFailed('no %s method' % fname) |
|
261 |
# no <arg> attribute means the callback takes no argument |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
262 |
args = self._cw.form.get('arg', ()) |
1419 | 263 |
if not isinstance(args, (list, tuple)): |
264 |
args = (args,) |
|
265 |
args = [simplejson.loads(arg) for arg in args] |
|
0 | 266 |
try: |
1419 | 267 |
result = func(*args) |
268 |
except RemoteCallFailed: |
|
269 |
raise |
|
270 |
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
|
271 |
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
|
272 |
traceback.print_exc() |
1419 | 273 |
self.exception('an exception occured while calling js_%s(%s): %s', |
274 |
fname, args, ex) |
|
275 |
raise RemoteCallFailed(repr(ex)) |
|
276 |
if result is None: |
|
277 |
return '' |
|
278 |
# get unicode on @htmlize methods, encoded string on @jsonize methods |
|
279 |
elif isinstance(result, unicode): |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
280 |
return result.encode(self._cw.encoding) |
1419 | 281 |
return result |
282 |
||
283 |
def _rebuild_posted_form(self, names, values, action=None): |
|
284 |
form = {} |
|
285 |
for name, value in zip(names, values): |
|
286 |
# remove possible __action_xxx inputs |
|
287 |
if name.startswith('__action'): |
|
288 |
continue |
|
289 |
# form.setdefault(name, []).append(value) |
|
290 |
if name in form: |
|
291 |
curvalue = form[name] |
|
292 |
if isinstance(curvalue, list): |
|
293 |
curvalue.append(value) |
|
294 |
else: |
|
295 |
form[name] = [curvalue, value] |
|
296 |
else: |
|
297 |
form[name] = value |
|
298 |
# simulate click on __action_%s button to help the controller |
|
299 |
if action: |
|
300 |
form['__action_%s' % action] = u'whatever' |
|
301 |
return form |
|
0 | 302 |
|
303 |
def _exec(self, rql, args=None, eidkey=None, rocheck=True): |
|
304 |
"""json mode: execute RQL and return resultset as json""" |
|
305 |
if rocheck: |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
306 |
self._cw.ensure_ro_rql(rql) |
0 | 307 |
try: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
308 |
return self._cw.execute(rql, args, eidkey) |
0 | 309 |
except Exception, ex: |
310 |
self.exception("error in _exec(rql=%s): %s", rql, ex) |
|
311 |
return None |
|
312 |
return None |
|
313 |
||
2852
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
314 |
def _call_view(self, view, **kwargs): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
315 |
req = self._cw |
2852
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
316 |
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
|
317 |
# 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
|
318 |
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
|
319 |
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
|
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 |
# 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
|
322 |
stream.write(u'<div id="pageContent">') |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
323 |
vtitle = self._cw.form.get('vtitle') |
2852
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
324 |
if vtitle: |
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'<h1 class="vtitle">%s</h1>\n' % vtitle) |
2870
e3a5b7c3f767
use view.paginate
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2862
diff
changeset
|
326 |
view.paginate() |
2852
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
327 |
if divid == 'pageContent': |
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
328 |
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
|
329 |
view.render(**kwargs) |
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
330 |
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
|
331 |
if extresources: |
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
332 |
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
|
333 |
stream.write(extresources) |
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
334 |
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
|
335 |
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
|
336 |
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
|
337 |
return stream.getvalue() |
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
338 |
|
1419 | 339 |
@xhtmlize |
340 |
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
|
341 |
# XXX try to use the page-content template |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
342 |
req = self._cw |
0 | 343 |
rql = req.form.get('rql') |
1419 | 344 |
if rql: |
0 | 345 |
rset = self._exec(rql) |
1419 | 346 |
else: |
347 |
rset = None |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
348 |
vid = req.form.get('vid') or vid_from_rset(req, rset, self._cw.schema) |
0 | 349 |
try: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
350 |
view = self._cw.vreg['views'].select(vid, req, rset=rset) |
0 | 351 |
except NoSelectableObject: |
352 |
vid = req.form.get('fallbackvid', 'noresult') |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
353 |
view = self._cw.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
|
354 |
return self._call_view(view) |
0 | 355 |
|
1419 | 356 |
@xhtmlize |
357 |
def js_prop_widget(self, propkey, varname, tabindex=None): |
|
358 |
"""specific method for CWProperty handling""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
359 |
entity = self._cw.vreg['etypes'].etype_class('CWProperty')(self._cw) |
1419 | 360 |
entity.eid = varname |
361 |
entity['pkey'] = propkey |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
362 |
form = self._cw.vreg['forms'].select('edition', self._cw, entity=entity) |
3514
1531edc6c021
forgotten that one
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3343
diff
changeset
|
363 |
form.build_context() |
1419 | 364 |
vfield = form.field_by_name('value') |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
365 |
renderer = FormRenderer(self._cw) |
1419 | 366 |
return vfield.render(form, renderer, tabindex=tabindex) \ |
367 |
+ renderer.render_help(form, vfield) |
|
0 | 368 |
|
1419 | 369 |
@xhtmlize |
370 |
def js_component(self, compid, rql, registry='components', extraargs=None): |
|
371 |
if rql: |
|
372 |
rset = self._exec(rql) |
|
373 |
else: |
|
374 |
rset = None |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
375 |
comp = self._cw.vreg[registry].select(compid, self._cw, rset=rset) |
1419 | 376 |
if extraargs is None: |
377 |
extraargs = {} |
|
378 |
else: # we receive unicode keys which is not supported by the **syntax |
|
379 |
extraargs = dict((str(key), value) |
|
380 |
for key, value in extraargs.items()) |
|
381 |
extraargs = extraargs or {} |
|
1723 | 382 |
return comp.render(**extraargs) |
1419 | 383 |
|
384 |
@check_pageid |
|
385 |
@xhtmlize |
|
3327
44efba78afac
fix/enhance i18n context usage for inlined forms
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3260
diff
changeset
|
386 |
def js_inline_creation_form(self, peid, ttype, rtype, role, i18nctx): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
387 |
view = self._cw.vreg['views'].select('inline-creation', self._cw, |
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
|
388 |
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
|
389 |
role=role) |
3518
11ce4682187d
[form] important refactoring of inlined forms to get proper separation of form object creation / rendering
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3514
diff
changeset
|
390 |
return self._call_view(view, i18nctx=i18nctx) |
1419 | 391 |
|
392 |
@jsonize |
|
0 | 393 |
def js_validate_form(self, action, names, values): |
1527
c8ca1782e252
controller fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1467
diff
changeset
|
394 |
return self.validate_form(action, names, values) |
c8ca1782e252
controller fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1467
diff
changeset
|
395 |
|
c8ca1782e252
controller fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1467
diff
changeset
|
396 |
def validate_form(self, action, names, values): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
397 |
self._cw.form = self._rebuild_posted_form(names, values, action) |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
398 |
return _validate_form(self._cw, self._cw.vreg) |
0 | 399 |
|
1419 | 400 |
@jsonize |
2345
16e3d0e47ee6
[reledit] there is nothing to escape, also cleanup lzone for attributes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2330
diff
changeset
|
401 |
def js_edit_field(self, action, names, values, rtype, eid, default): |
3260
49728db93b3e
[jsoncontroller] bugfix: update to match new validate_form API
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3237
diff
changeset
|
402 |
success, args, _ = self.validate_form(action, names, values) |
0 | 403 |
if success: |
1560
7dd2a81b8bc8
[basecontrollers] add edit_relation next to edit_field, misc notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1527
diff
changeset
|
404 |
# 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
|
405 |
# printable_value won't need to query N again |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
406 |
rset = self._cw.execute('Any X,N WHERE X eid %%(x)s, X %s N' % rtype, |
0 | 407 |
{'x': eid}, 'x') |
408 |
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
|
409 |
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
|
410 |
return (success, args, value) |
0 | 411 |
else: |
412 |
return (success, args, None) |
|
1419 | 413 |
|
1560
7dd2a81b8bc8
[basecontrollers] add edit_relation next to edit_field, misc notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1527
diff
changeset
|
414 |
@jsonize |
2382
c1dcb5aef4b4
[reledit] simplify a bit more
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2371
diff
changeset
|
415 |
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
|
416 |
"""XXX we should get rid of this and use loadxhtml""" |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
417 |
entity = self._cw.entity_from_eid(eid) |
2371
76bf522c27be
[reledit] simplify, fixing #344545
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
2345
diff
changeset
|
418 |
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
|
419 |
default=default, landing_zone=lzone) |
1759
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1736
diff
changeset
|
420 |
|
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1736
diff
changeset
|
421 |
@jsonize |
0 | 422 |
def js_i18n(self, msgids): |
423 |
"""returns the translation of `msgid`""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
424 |
return [self._cw._(msgid) for msgid in msgids] |
0 | 425 |
|
1419 | 426 |
@jsonize |
0 | 427 |
def js_format_date(self, strdate): |
428 |
"""returns the formatted date for `msgid`""" |
|
1380 | 429 |
date = strptime(strdate, '%Y-%m-%d %H:%M:%S') |
3460
e4843535db25
[api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
430 |
return self._cw.format_date(date) |
0 | 431 |
|
1419 | 432 |
@jsonize |
0 | 433 |
def js_external_resource(self, resource): |
434 |
"""returns the URL of the external resource named `resource`""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
435 |
return self._cw.external_resource(resource) |
0 | 436 |
|
437 |
@check_pageid |
|
1419 | 438 |
@jsonize |
0 | 439 |
def js_user_callback(self, cbname): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
440 |
page_data = self._cw.get_session_data(self._cw.pageid, {}) |
0 | 441 |
try: |
442 |
cb = page_data[cbname] |
|
443 |
except KeyError: |
|
444 |
return None |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
445 |
return cb(self._cw) |
0 | 446 |
|
447 |
if HAS_SEARCH_RESTRICTION: |
|
1419 | 448 |
@jsonize |
0 | 449 |
def js_filter_build_rql(self, names, values): |
450 |
form = self._rebuild_posted_form(names, values) |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
451 |
self._cw.form = form |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
452 |
builder = FilterRQLBuilder(self._cw) |
0 | 453 |
return builder.build_rql() |
454 |
||
1419 | 455 |
@jsonize |
0 | 456 |
def js_filter_select_content(self, facetids, rql): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
457 |
rqlst = self._cw.vreg.parse(self._cw, rql) # XXX Union unsupported yet |
0 | 458 |
mainvar = prepare_facets_rqlst(rqlst)[0] |
459 |
update_map = {} |
|
460 |
for facetid in facetids: |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
461 |
facet = get_facet(self._cw, facetid, rqlst.children[0], mainvar) |
0 | 462 |
update_map[facetid] = facet.possible_values() |
463 |
return update_map |
|
464 |
||
1419 | 465 |
def js_unregister_user_callback(self, cbname): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
466 |
self._cw.unregister_callback(self._cw.pageid, cbname) |
1419 | 467 |
|
468 |
def js_unload_page_data(self): |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
469 |
self._cw.del_session_data(self._cw.pageid) |
1419 | 470 |
|
471 |
def js_cancel_edition(self, errorurl): |
|
472 |
"""cancelling edition from javascript |
|
473 |
||
474 |
We need to clear associated req's data : |
|
475 |
- errorurl |
|
476 |
- pending insertions / deletions |
|
477 |
""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
478 |
self._cw.cancel_edition(errorurl) |
1419 | 479 |
|
0 | 480 |
def js_delete_bookmark(self, beid): |
1419 | 481 |
rql = 'DELETE B bookmarked_by U WHERE B eid %(b)s, U eid %(u)s' |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
482 |
self._cw.execute(rql, {'b': typed_eid(beid), 'u' : self._cw.user.eid}) |
1419 | 483 |
|
1844
ec51bf1b8be3
avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1839
diff
changeset
|
484 |
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
|
485 |
"""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
|
486 |
from cubicweb.web.views.treeview import treecookiename |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
487 |
cookies = self._cw.get_cookie() |
1844
ec51bf1b8be3
avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1839
diff
changeset
|
488 |
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
|
489 |
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
|
490 |
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
|
491 |
cookies[statename] = nodeeid |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
492 |
self._cw.set_cookie(cookies, statename) |
1844
ec51bf1b8be3
avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1839
diff
changeset
|
493 |
else: |
ec51bf1b8be3
avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1839
diff
changeset
|
494 |
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
|
495 |
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
|
496 |
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
|
497 |
else: |
ec51bf1b8be3
avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1839
diff
changeset
|
498 |
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
|
499 |
cookies[statename] = ';'.join(marked) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
500 |
self._cw.set_cookie(cookies, statename) |
1844
ec51bf1b8be3
avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1839
diff
changeset
|
501 |
|
1419 | 502 |
def js_set_cookie(self, cookiename, cookievalue): |
503 |
# XXX we should consider jQuery.Cookie |
|
504 |
cookiename, cookievalue = str(cookiename), str(cookievalue) |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
505 |
cookies = self._cw.get_cookie() |
1419 | 506 |
cookies[cookiename] = cookievalue |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
507 |
self._cw.set_cookie(cookies, cookiename) |
1419 | 508 |
|
509 |
# relations edition stuff ################################################## |
|
0 | 510 |
|
511 |
def _add_pending(self, eidfrom, rel, eidto, kind): |
|
512 |
key = 'pending_%s' % kind |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
513 |
pendings = self._cw.get_session_data(key, set()) |
0 | 514 |
pendings.add( (typed_eid(eidfrom), rel, typed_eid(eidto)) ) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
515 |
self._cw.set_session_data(key, pendings) |
0 | 516 |
|
517 |
def _remove_pending(self, eidfrom, rel, eidto, kind): |
|
1419 | 518 |
key = 'pending_%s' % kind |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
519 |
pendings = self._cw.get_session_data(key) |
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
|
520 |
pendings.remove( (typed_eid(eidfrom), rel, typed_eid(eidto)) ) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
521 |
self._cw.set_session_data(key, pendings) |
0 | 522 |
|
1419 | 523 |
def js_remove_pending_insert(self, (eidfrom, rel, eidto)): |
524 |
self._remove_pending(eidfrom, rel, eidto, 'insert') |
|
525 |
||
526 |
def js_add_pending_inserts(self, tripletlist): |
|
527 |
for eidfrom, rel, eidto in tripletlist: |
|
528 |
self._add_pending(eidfrom, rel, eidto, 'insert') |
|
529 |
||
530 |
def js_remove_pending_delete(self, (eidfrom, rel, eidto)): |
|
531 |
self._remove_pending(eidfrom, rel, eidto, 'delete') |
|
532 |
||
533 |
def js_add_pending_delete(self, (eidfrom, rel, eidto)): |
|
534 |
self._add_pending(eidfrom, rel, eidto, 'delete') |
|
535 |
||
536 |
# XXX specific code. Kill me and my AddComboBox friend |
|
537 |
@jsonize |
|
0 | 538 |
def js_add_and_link_new_entity(self, etype_to, rel, eid_to, etype_from, value_from): |
539 |
# create a new entity |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
540 |
eid_from = self._cw.execute('INSERT %s T : T name "%s"' % ( etype_from, value_from ))[0][0] |
0 | 541 |
# link the new entity to the main entity |
542 |
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} |
|
543 |
return eid_from |
|
603
18c6c31bbaf4
[controllers] a set_cookie method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
581
diff
changeset
|
544 |
|
18c6c31bbaf4
[controllers] a set_cookie method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
581
diff
changeset
|
545 |
|
0 | 546 |
class SendMailController(Controller): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
547 |
__regid__ = 'sendmail' |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
548 |
__select__ = match_user_groups('managers', 'users') |
0 | 549 |
|
550 |
def recipients(self): |
|
551 |
"""returns an iterator on email's recipients as entities""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
552 |
eids = self._cw.form['recipient'] |
0 | 553 |
# make sure we have a list even though only one recipient was specified |
554 |
if isinstance(eids, basestring): |
|
555 |
eids = (eids,) |
|
556 |
rql = 'Any X WHERE X eid in (%s)' % (','.join(eids)) |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
557 |
rset = self._cw.execute(rql) |
0 | 558 |
for entity in rset.entities(): |
559 |
yield entity |
|
560 |
||
561 |
@property |
|
562 |
@cached |
|
563 |
def smtp(self): |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
564 |
mailhost, port = self._cw.config['smtp-host'], self._cw.config['smtp-port'] |
0 | 565 |
try: |
566 |
return SMTP(mailhost, port) |
|
567 |
except Exception, ex: |
|
568 |
self.exception("can't connect to smtp server %s:%s (%s)", |
|
569 |
mailhost, port, ex) |
|
3460
e4843535db25
[api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
570 |
url = self._cw.build_url(__message=self._cw._('could not connect to the SMTP server')) |
0 | 571 |
raise Redirect(url) |
572 |
||
573 |
def sendmail(self, recipient, subject, body): |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
574 |
helo_addr = '%s <%s>' % (self._cw.config['sender-name'], |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
575 |
self._cw.config['sender-addr']) |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
576 |
msg = format_mail({'email' : self._cw.user.get_email(), |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
577 |
'name' : self._cw.user.dc_title(),}, |
0 | 578 |
[recipient], body, subject) |
1419 | 579 |
self.smtp.sendmail(helo_addr, [recipient], msg.as_string()) |
0 | 580 |
|
581 |
def publish(self, rset=None): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
582 |
# 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
|
583 |
# a mail relay |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
584 |
body = self._cw.form['mailbody'] |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
585 |
subject = self._cw.form['subject'] |
0 | 586 |
for recipient in self.recipients(): |
587 |
text = body % recipient.as_email_context() |
|
588 |
self.sendmail(recipient.get_email(), subject, text) |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
589 |
# breadcrumbs = self._cw.get_session_data('breadcrumbs', None) |
3460
e4843535db25
[api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
590 |
url = self._cw.build_url(__message=self._cw._('emails successfully sent')) |
0 | 591 |
raise Redirect(url) |
592 |
||
593 |
||
594 |
class MailBugReportController(SendMailController): |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
595 |
__regid__ = 'reportbug' |
742
99115e029dca
replaced most of __selectors__ assignments with __select__
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
596 |
__select__ = yes() |
0 | 597 |
|
598 |
def publish(self, rset=None): |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
599 |
body = self._cw.form['description'] |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
600 |
self.sendmail(self._cw.config['submit-mail'], _('%s error report') % self._cw.config.appid, body) |
3460
e4843535db25
[api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
601 |
url = self._cw.build_url(__message=self._cw._('bug report sent')) |
0 | 602 |
raise Redirect(url) |
1419 | 603 |