author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 12 Apr 2010 15:15:00 +0200 | |
branch | stable |
changeset 5217 | 08e7fa906cdb |
parent 5200 | 2b454c6ab7ef |
child 5201 | 2b4267157f85 |
child 5366 | 5f116a4d8a54 |
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 |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3816
diff
changeset
|
7 |
:copyright: 2001-2010 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 |
|
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4436
diff
changeset
|
18 |
from logilab.common.date import strptime |
0 | 19 |
|
4911
898c35be5873
#750055: make it easier to change post logout url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4741
diff
changeset
|
20 |
from cubicweb import (NoSelectableObject, ValidationError, ObjectNotFound, |
898c35be5873
#750055: make it easier to change post logout url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4741
diff
changeset
|
21 |
typed_eid) |
4466
8b0ca7904820
moved generic datetime manipulation function to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4436
diff
changeset
|
22 |
from cubicweb.utils import CubicWebJsonEncoder |
4894
41c28ddca055
[cleanup selectors] use authenticated_user, check for form params instead of handling potential key error on missing params
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4741
diff
changeset
|
23 |
from cubicweb.selectors import authenticated_user, match_form_params |
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4003
diff
changeset
|
24 |
from cubicweb.mail import format_mail |
1635
866563e2d0fc
don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
25 |
from cubicweb.web import ExplicitLogin, Redirect, RemoteCallFailed, json_dumps |
0 | 26 |
from cubicweb.web.controller import Controller |
27 |
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
|
28 |
from cubicweb.web.views.formrenderers import FormRenderer |
0 | 29 |
try: |
30 |
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
|
31 |
prepare_facets_rqlst) |
0 | 32 |
HAS_SEARCH_RESTRICTION = True |
33 |
except ImportError: # gae |
|
34 |
HAS_SEARCH_RESTRICTION = False |
|
1419 | 35 |
|
36 |
def jsonize(func): |
|
37 |
"""decorator to sets correct content_type and calls `simplejson.dumps` on |
|
38 |
results |
|
39 |
""" |
|
40 |
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
|
41 |
self._cw.set_content_type('application/json') |
1635
866563e2d0fc
don't depends on simplejson outside web/
sylvain.thenault@logilab.fr
parents:
1560
diff
changeset
|
42 |
return json_dumps(func(self, *args, **kwargs)) |
1527
c8ca1782e252
controller fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1467
diff
changeset
|
43 |
wrapper.__name__ = func.__name__ |
1419 | 44 |
return wrapper |
45 |
||
46 |
def xhtmlize(func): |
|
47 |
"""decorator to sets correct content_type and calls `xmlize` on results""" |
|
48 |
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
|
49 |
self._cw.set_content_type(self._cw.html_content_type()) |
1419 | 50 |
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
|
51 |
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
|
52 |
u'</div>')) |
1527
c8ca1782e252
controller fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1467
diff
changeset
|
53 |
wrapper.__name__ = func.__name__ |
1419 | 54 |
return wrapper |
55 |
||
56 |
def check_pageid(func): |
|
57 |
"""decorator which checks the given pageid is found in the |
|
58 |
user's session data |
|
59 |
""" |
|
60 |
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
|
61 |
data = self._cw.get_session_data(self._cw.pageid) |
1419 | 62 |
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
|
63 |
raise RemoteCallFailed(self._cw._('pageid-not-found')) |
1419 | 64 |
return func(self, *args, **kwargs) |
65 |
return wrapper |
|
66 |
||
67 |
||
0 | 68 |
class LoginController(Controller): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
69 |
__regid__ = 'login' |
0 | 70 |
|
71 |
def publish(self, rset=None): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
72 |
"""log in the instance""" |
4072
ead446e70c28
some api update
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4023
diff
changeset
|
73 |
if self._cw.vreg.config['auth-mode'] == 'http': |
0 | 74 |
# HTTP authentication |
75 |
raise ExplicitLogin() |
|
76 |
else: |
|
77 |
# Cookie authentication |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
78 |
return self.appli.need_login_content(self._cw) |
0 | 79 |
|
1419 | 80 |
|
0 | 81 |
class LogoutController(Controller): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
82 |
__regid__ = 'logout' |
1419 | 83 |
|
0 | 84 |
def publish(self, rset=None): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
85 |
"""logout from the instance""" |
4911
898c35be5873
#750055: make it easier to change post logout url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4741
diff
changeset
|
86 |
return self.appli.session_handler.logout(self._cw, self.goto_url()) |
0 | 87 |
|
4911
898c35be5873
#750055: make it easier to change post logout url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4741
diff
changeset
|
88 |
def goto_url(self): |
898c35be5873
#750055: make it easier to change post logout url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4741
diff
changeset
|
89 |
# * in http auth mode, url will be ignored |
898c35be5873
#750055: make it easier to change post logout url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4741
diff
changeset
|
90 |
# * in cookie mode redirecting to the index view is enough : either |
898c35be5873
#750055: make it easier to change post logout url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4741
diff
changeset
|
91 |
# anonymous connection is allowed and the page will be displayed or |
898c35be5873
#750055: make it easier to change post logout url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4741
diff
changeset
|
92 |
# we'll be redirected to the login form |
898c35be5873
#750055: make it easier to change post logout url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4741
diff
changeset
|
93 |
msg = self._cw._('you have been logged out') |
5200
2b454c6ab7ef
[web] on logout, use the base_url argument of build_url instead of hacking the request to get proper url. This fix a bug on site with http/https versions: the session cookie is badly removed on logout
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5198
diff
changeset
|
94 |
# force base_url so on dual http/https configuration, we generate an url |
2b454c6ab7ef
[web] on logout, use the base_url argument of build_url instead of hacking the request to get proper url. This fix a bug on site with http/https versions: the session cookie is badly removed on logout
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5198
diff
changeset
|
95 |
# on the http version of the site |
2b454c6ab7ef
[web] on logout, use the base_url argument of build_url instead of hacking the request to get proper url. This fix a bug on site with http/https versions: the session cookie is badly removed on logout
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5198
diff
changeset
|
96 |
return self._cw.build_url('view', vid='index', __message=msg, |
2b454c6ab7ef
[web] on logout, use the base_url argument of build_url instead of hacking the request to get proper url. This fix a bug on site with http/https versions: the session cookie is badly removed on logout
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5198
diff
changeset
|
97 |
base_url=self._cw.vreg.config['base-url']) |
2b454c6ab7ef
[web] on logout, use the base_url argument of build_url instead of hacking the request to get proper url. This fix a bug on site with http/https versions: the session cookie is badly removed on logout
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5198
diff
changeset
|
98 |
|
0 | 99 |
|
100 |
class ViewController(Controller): |
|
823
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
101 |
"""standard entry point : |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
102 |
- build result set |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
103 |
- select and call main template |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
104 |
""" |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
105 |
__regid__ = 'view' |
823
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
106 |
template = 'main-template' |
1419 | 107 |
|
0 | 108 |
def publish(self, rset=None): |
109 |
"""publish a request, returning an encoded string""" |
|
823
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
110 |
view, rset = self._select_view_and_rset(rset) |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
111 |
self.add_to_breadcrumbs(view) |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
112 |
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
|
113 |
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
|
114 |
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
|
115 |
rset=rset, view=view) |
823
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
116 |
|
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
117 |
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
|
118 |
req = self._cw |
823
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
119 |
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
|
120 |
req._rql_processed = True |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
121 |
rset = self.process_rql(req.form.get('rql')) |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
122 |
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
|
123 |
entity = rset.get_entity(0, 0) |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
124 |
try: |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
125 |
method = getattr(entity, req.form.pop('__method')) |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
126 |
method() |
1827
93840d187f26
allow the __method() hook to raise a Redirect exception
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1798
diff
changeset
|
127 |
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
|
128 |
raise |
823
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
129 |
except Exception, ex: |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
130 |
self.exception('while handling __method') |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
131 |
req.set_message(req._("error while handling __method: %s") % req._(ex)) |
3659 | 132 |
vid = req.form.get('vid') or vid_from_rset(req, rset, self._cw.vreg.schema) |
823
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
133 |
try: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
134 |
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
|
135 |
except ObjectNotFound: |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
136 |
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
|
137 |
req.set_message(req._("The view %s could not be found") % vid) |
3659 | 138 |
vid = vid_from_rset(req, rset, self._cw.vreg.schema) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
139 |
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
|
140 |
except NoSelectableObject: |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
141 |
if rset: |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
142 |
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
|
143 |
else: |
3144
a5deac822a13
Bugfix: message was not written in english
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2870
diff
changeset
|
144 |
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
|
145 |
"be used to display the current data.")) |
823
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
146 |
self.warning("the view %s can not be applied to this query", vid) |
3890
d7a270f50f54
backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
147 |
vid = req.form.get('fallbackvid') or vid_from_rset(req, rset, req.vreg.schema) |
d7a270f50f54
backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
148 |
view = req.vreg['views'].select(vid, req, rset=rset) |
823
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
149 |
return view, rset |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
150 |
|
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
151 |
def add_to_breadcrumbs(self, view): |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
152 |
# update breadcrumps **before** validating cache, unless the view |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
153 |
# 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
|
154 |
# view is a binary view |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
155 |
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
|
156 |
self._cw.update_breadcrumbs() |
823
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
157 |
|
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
158 |
def validate_cache(self, view): |
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
159 |
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
|
160 |
self._cw.validate_cache() |
823
cb8ccbef8fa5
main template refactoring
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
808
diff
changeset
|
161 |
|
0 | 162 |
def execute_linkto(self, eid=None): |
163 |
"""XXX __linkto parameter may cause security issue |
|
164 |
||
165 |
defined here since custom application controller inheriting from this |
|
166 |
one use this method? |
|
167 |
""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
168 |
req = self._cw |
0 | 169 |
if not '__linkto' in req.form: |
170 |
return |
|
171 |
if eid is None: |
|
172 |
eid = typed_eid(req.form['eid']) |
|
173 |
for linkto in req.list_form_param('__linkto', pop=True): |
|
174 |
rtype, eids, target = linkto.split(':') |
|
175 |
assert target in ('subject', 'object') |
|
176 |
eids = eids.split('_') |
|
177 |
if target == 'subject': |
|
178 |
rql = 'SET X %s Y WHERE X eid %%(x)s, Y eid %%(y)s' % rtype |
|
179 |
else: |
|
180 |
rql = 'SET Y %s X WHERE X eid %%(x)s, Y eid %%(y)s' % rtype |
|
181 |
for teid in eids: |
|
1419 | 182 |
req.execute(rql, {'x': eid, 'y': typed_eid(teid)}, ('x', 'y')) |
0 | 183 |
|
184 |
||
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
|
185 |
def _validation_error(req, ex): |
2293 | 186 |
req.cnx.rollback() |
4719
aaed3f813ef8
kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4638
diff
changeset
|
187 |
# XXX necessary to remove existant validation error? |
aaed3f813ef8
kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4638
diff
changeset
|
188 |
# imo (syt), it's not necessary |
aaed3f813ef8
kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4638
diff
changeset
|
189 |
req.get_session_data(req.form.get('__errorurl'), pop=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
|
190 |
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
|
191 |
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
|
192 |
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
|
193 |
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
|
194 |
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
|
195 |
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
|
196 |
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
|
197 |
|
3232
eccb7380dc3b
[controllers] allow onsuccess / onfailure callback to be passed to validateform
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3144
diff
changeset
|
198 |
|
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
|
199 |
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
|
200 |
# 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
|
201 |
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
|
202 |
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
|
203 |
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
|
204 |
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
|
205 |
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
|
206 |
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
|
207 |
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
|
208 |
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
|
209 |
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
|
210 |
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
|
211 |
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
|
212 |
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
|
213 |
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
|
214 |
except Exception, ex: |
54743fc6172c
[json validation] catch everything here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3518
diff
changeset
|
215 |
req.cnx.rollback() |
54743fc6172c
[json validation] catch everything here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3518
diff
changeset
|
216 |
req.exception('unexpected error while validating form') |
54743fc6172c
[json validation] catch everything here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3518
diff
changeset
|
217 |
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
|
218 |
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
|
219 |
# 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
|
220 |
# 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
|
221 |
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
|
222 |
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
|
223 |
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
|
224 |
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
|
225 |
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
|
226 |
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
|
227 |
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
|
228 |
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
|
229 |
|
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
|
230 |
|
0 | 231 |
class FormValidatorController(Controller): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
232 |
__regid__ = 'validateform' |
0 | 233 |
|
3232
eccb7380dc3b
[controllers] allow onsuccess / onfailure callback to be passed to validateform
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3144
diff
changeset
|
234 |
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
|
235 |
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
|
236 |
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
|
237 |
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
|
238 |
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
|
239 |
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
|
240 |
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
|
241 |
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
|
242 |
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
|
243 |
</script>""" % (domid, callback, errback, jsargs, cbargs) |
2557
200985d3258d
make it easy to change response of FormValidatorController
Florent <florent@secondweb.fr>
parents:
2555
diff
changeset
|
244 |
|
0 | 245 |
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
|
246 |
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
|
247 |
# 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
|
248 |
# 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
|
249 |
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
|
250 |
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
|
251 |
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
|
252 |
return self.response(domid, status, args, entity) |
0 | 253 |
|
254 |
||
255 |
class JSonController(Controller): |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
256 |
__regid__ = 'json' |
0 | 257 |
|
258 |
def publish(self, rset=None): |
|
1419 | 259 |
"""call js_* methods. Expected form keys: |
260 |
||
261 |
:fname: the method name without the js_ prefix |
|
262 |
:args: arguments list (json) |
|
263 |
||
264 |
note: it's the responsability of js_* methods to set the correct |
|
265 |
response content type |
|
266 |
""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
267 |
self._cw.json_request = True |
1419 | 268 |
try: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
269 |
fname = self._cw.form['fname'] |
1419 | 270 |
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
|
271 |
except KeyError: |
aff0950c54c4
proper error when fname isn't specified
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2045
diff
changeset
|
272 |
raise RemoteCallFailed('no method specified') |
1419 | 273 |
except AttributeError: |
274 |
raise RemoteCallFailed('no %s method' % fname) |
|
275 |
# 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
|
276 |
args = self._cw.form.get('arg', ()) |
1419 | 277 |
if not isinstance(args, (list, tuple)): |
278 |
args = (args,) |
|
5139
61b607c8571b
catch and log errors caused by bad json data
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5113
diff
changeset
|
279 |
try: |
61b607c8571b
catch and log errors caused by bad json data
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5113
diff
changeset
|
280 |
args = [simplejson.loads(arg) for arg in args] |
61b607c8571b
catch and log errors caused by bad json data
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5113
diff
changeset
|
281 |
except ValueError, exc: |
61b607c8571b
catch and log errors caused by bad json data
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5113
diff
changeset
|
282 |
self.exception('error while decoding json arguments for js_%s: %s', fname, args, exc) |
5141 | 283 |
raise RemoteCallFailed(repr(exc)) |
0 | 284 |
try: |
1419 | 285 |
result = func(*args) |
286 |
except RemoteCallFailed: |
|
287 |
raise |
|
288 |
except Exception, ex: |
|
289 |
self.exception('an exception occured while calling js_%s(%s): %s', |
|
290 |
fname, args, ex) |
|
291 |
raise RemoteCallFailed(repr(ex)) |
|
292 |
if result is None: |
|
293 |
return '' |
|
294 |
# get unicode on @htmlize methods, encoded string on @jsonize methods |
|
295 |
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
|
296 |
return result.encode(self._cw.encoding) |
1419 | 297 |
return result |
298 |
||
299 |
def _rebuild_posted_form(self, names, values, action=None): |
|
300 |
form = {} |
|
301 |
for name, value in zip(names, values): |
|
302 |
# remove possible __action_xxx inputs |
|
303 |
if name.startswith('__action'): |
|
304 |
continue |
|
305 |
# form.setdefault(name, []).append(value) |
|
306 |
if name in form: |
|
307 |
curvalue = form[name] |
|
308 |
if isinstance(curvalue, list): |
|
309 |
curvalue.append(value) |
|
310 |
else: |
|
311 |
form[name] = [curvalue, value] |
|
312 |
else: |
|
313 |
form[name] = value |
|
314 |
# simulate click on __action_%s button to help the controller |
|
315 |
if action: |
|
316 |
form['__action_%s' % action] = u'whatever' |
|
317 |
return form |
|
0 | 318 |
|
319 |
def _exec(self, rql, args=None, eidkey=None, rocheck=True): |
|
320 |
"""json mode: execute RQL and return resultset as json""" |
|
321 |
if rocheck: |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
322 |
self._cw.ensure_ro_rql(rql) |
0 | 323 |
try: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
324 |
return self._cw.execute(rql, args, eidkey) |
0 | 325 |
except Exception, ex: |
326 |
self.exception("error in _exec(rql=%s): %s", rql, ex) |
|
327 |
return None |
|
328 |
return None |
|
329 |
||
2852
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
330 |
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
|
331 |
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
|
332 |
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
|
333 |
# 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
|
334 |
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
|
335 |
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
|
336 |
if divid == 'pageContent': |
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
337 |
# 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
|
338 |
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
|
339 |
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
|
340 |
if vtitle: |
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
341 |
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
|
342 |
view.paginate() |
2852
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
343 |
if divid == 'pageContent': |
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
344 |
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
|
345 |
view.render(**kwargs) |
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
346 |
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
|
347 |
if extresources: |
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
348 |
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
|
349 |
stream.write(extresources) |
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
350 |
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
|
351 |
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
|
352 |
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
|
353 |
return stream.getvalue() |
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
354 |
|
1419 | 355 |
@xhtmlize |
356 |
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
|
357 |
# 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
|
358 |
req = self._cw |
0 | 359 |
rql = req.form.get('rql') |
1419 | 360 |
if rql: |
0 | 361 |
rset = self._exec(rql) |
1419 | 362 |
else: |
363 |
rset = None |
|
3659 | 364 |
vid = req.form.get('vid') or vid_from_rset(req, rset, self._cw.vreg.schema) |
0 | 365 |
try: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
366 |
view = self._cw.vreg['views'].select(vid, req, rset=rset) |
0 | 367 |
except NoSelectableObject: |
368 |
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
|
369 |
view = self._cw.vreg['views'].select(vid, req, rset=rset) |
4638
c7d2145abf61
grmmbl, bad resolve
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4637
diff
changeset
|
370 |
self.validate_cache(view) |
2852
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
371 |
return self._call_view(view) |
0 | 372 |
|
1419 | 373 |
@xhtmlize |
374 |
def js_prop_widget(self, propkey, varname, tabindex=None): |
|
375 |
"""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
|
376 |
entity = self._cw.vreg['etypes'].etype_class('CWProperty')(self._cw) |
1419 | 377 |
entity.eid = varname |
378 |
entity['pkey'] = propkey |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
379 |
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
|
380 |
form.build_context() |
1419 | 381 |
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
|
382 |
renderer = FormRenderer(self._cw) |
1419 | 383 |
return vfield.render(form, renderer, tabindex=tabindex) \ |
384 |
+ renderer.render_help(form, vfield) |
|
0 | 385 |
|
1419 | 386 |
@xhtmlize |
387 |
def js_component(self, compid, rql, registry='components', extraargs=None): |
|
388 |
if rql: |
|
389 |
rset = self._exec(rql) |
|
390 |
else: |
|
391 |
rset = None |
|
392 |
if extraargs is None: |
|
393 |
extraargs = {} |
|
394 |
else: # we receive unicode keys which is not supported by the **syntax |
|
395 |
extraargs = dict((str(key), value) |
|
396 |
for key, value in extraargs.items()) |
|
5113
f8cbdb51e6d4
[cleanup] tb already printed by self.exception; add note
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4984
diff
changeset
|
397 |
# XXX while it sounds good, addition of the try/except below cause pb: |
f8cbdb51e6d4
[cleanup] tb already printed by self.exception; add note
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4984
diff
changeset
|
398 |
# when filtering using facets return an empty rset, the edition box |
f8cbdb51e6d4
[cleanup] tb already printed by self.exception; add note
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4984
diff
changeset
|
399 |
# isn't anymore selectable, as expected. The pb is that with the |
f8cbdb51e6d4
[cleanup] tb already printed by self.exception; add note
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4984
diff
changeset
|
400 |
# try/except below, we see a "an error occured" message in the ui, while |
f8cbdb51e6d4
[cleanup] tb already printed by self.exception; add note
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4984
diff
changeset
|
401 |
# we don't see it without it. Proper fix would probably be to deal with |
f8cbdb51e6d4
[cleanup] tb already printed by self.exception; add note
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4984
diff
changeset
|
402 |
# this by allowing facet handling code to tell to js_component that such |
f8cbdb51e6d4
[cleanup] tb already printed by self.exception; add note
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4984
diff
changeset
|
403 |
# error is expected and should'nt be reported. |
f8cbdb51e6d4
[cleanup] tb already printed by self.exception; add note
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4984
diff
changeset
|
404 |
#try: |
f8cbdb51e6d4
[cleanup] tb already printed by self.exception; add note
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4984
diff
changeset
|
405 |
comp = self._cw.vreg[registry].select(compid, self._cw, rset=rset, |
f8cbdb51e6d4
[cleanup] tb already printed by self.exception; add note
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4984
diff
changeset
|
406 |
**extraargs) |
f8cbdb51e6d4
[cleanup] tb already printed by self.exception; add note
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4984
diff
changeset
|
407 |
#except NoSelectableObject: |
f8cbdb51e6d4
[cleanup] tb already printed by self.exception; add note
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4984
diff
changeset
|
408 |
# raise RemoteCallFailed('unselectable') |
1419 | 409 |
extraargs = extraargs or {} |
4976
34fd21f65702
ticket #636927, add ajaxhtmlhead in controller js_component
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4911
diff
changeset
|
410 |
stream = comp.set_stream() |
34fd21f65702
ticket #636927, add ajaxhtmlhead in controller js_component
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4911
diff
changeset
|
411 |
comp.render(**extraargs) |
5157
1202e6565aff
[doc/book] talk about reloadComponent, misc tweaks and notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5151
diff
changeset
|
412 |
# XXX why not _call_view ? |
4976
34fd21f65702
ticket #636927, add ajaxhtmlhead in controller js_component
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4911
diff
changeset
|
413 |
extresources = self._cw.html_headers.getvalue(skiphead=True) |
34fd21f65702
ticket #636927, add ajaxhtmlhead in controller js_component
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4911
diff
changeset
|
414 |
if extresources: |
34fd21f65702
ticket #636927, add ajaxhtmlhead in controller js_component
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4911
diff
changeset
|
415 |
stream.write(u'<div class="ajaxHtmlHead">\n') |
34fd21f65702
ticket #636927, add ajaxhtmlhead in controller js_component
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4911
diff
changeset
|
416 |
stream.write(extresources) |
34fd21f65702
ticket #636927, add ajaxhtmlhead in controller js_component
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4911
diff
changeset
|
417 |
stream.write(u'</div>\n') |
34fd21f65702
ticket #636927, add ajaxhtmlhead in controller js_component
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4911
diff
changeset
|
418 |
return stream.getvalue() |
1419 | 419 |
|
420 |
@check_pageid |
|
421 |
@xhtmlize |
|
4629
1eeef3cbf528
fix 4626:c26b4df9fc90 (#703911): use can't rely on peid since it's not an actual eid when we're creating the parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
422 |
def js_inline_creation_form(self, peid, petype, 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
|
423 |
view = self._cw.vreg['views'].select('inline-creation', self._cw, |
4629
1eeef3cbf528
fix 4626:c26b4df9fc90 (#703911): use can't rely on peid since it's not an actual eid when we're creating the parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
424 |
etype=ttype, rtype=rtype, role=role, |
1eeef3cbf528
fix 4626:c26b4df9fc90 (#703911): use can't rely on peid since it's not an actual eid when we're creating the parent entity
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4466
diff
changeset
|
425 |
peid=peid, petype=petype) |
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
|
426 |
return self._call_view(view, i18nctx=i18nctx) |
1419 | 427 |
|
428 |
@jsonize |
|
0 | 429 |
def js_validate_form(self, action, names, values): |
1527
c8ca1782e252
controller fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1467
diff
changeset
|
430 |
return self.validate_form(action, names, values) |
c8ca1782e252
controller fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1467
diff
changeset
|
431 |
|
c8ca1782e252
controller fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1467
diff
changeset
|
432 |
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
|
433 |
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
|
434 |
return _validate_form(self._cw, self._cw.vreg) |
0 | 435 |
|
3742
20f429eb5f46
kill separate attribute client-side handling #473636
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3608
diff
changeset
|
436 |
@xhtmlize |
20f429eb5f46
kill separate attribute client-side handling #473636
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3608
diff
changeset
|
437 |
def js_reledit_form(self): |
4739
64806b0f7431
[reledit] add ajax html head div, which allows to get additional css/js up to the browser #620569
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4721
diff
changeset
|
438 |
req = self._cw |
4721
8f63691ccb7f
pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
439 |
args = dict((x, self._cw.form[x]) |
3742
20f429eb5f46
kill separate attribute client-side handling #473636
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3608
diff
changeset
|
440 |
for x in frozenset(('rtype', 'role', 'reload', 'landing_zone'))) |
3777 | 441 |
entity = self._cw.entity_from_eid(int(self._cw.form['eid'])) |
3742
20f429eb5f46
kill separate attribute client-side handling #473636
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3608
diff
changeset
|
442 |
# note: default is reserved in js land |
3777 | 443 |
args['default'] = self._cw.form['default_value'] |
3742
20f429eb5f46
kill separate attribute client-side handling #473636
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3608
diff
changeset
|
444 |
args['reload'] = simplejson.loads(args['reload']) |
4739
64806b0f7431
[reledit] add ajax html head div, which allows to get additional css/js up to the browser #620569
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4721
diff
changeset
|
445 |
rset = req.eid_rset(int(self._cw.form['eid'])) |
64806b0f7431
[reledit] add ajax html head div, which allows to get additional css/js up to the browser #620569
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4721
diff
changeset
|
446 |
view = req.vreg['views'].select('doreledit', req, rset=rset, rtype=args['rtype']) |
64806b0f7431
[reledit] add ajax html head div, which allows to get additional css/js up to the browser #620569
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4721
diff
changeset
|
447 |
stream = view.set_stream() |
64806b0f7431
[reledit] add ajax html head div, which allows to get additional css/js up to the browser #620569
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4721
diff
changeset
|
448 |
view.render(**args) |
5151
2f70fa8b6854
[doc/book] expand the js chapter with an ajax story
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
5141
diff
changeset
|
449 |
# XXX why not _call_view ? |
4739
64806b0f7431
[reledit] add ajax html head div, which allows to get additional css/js up to the browser #620569
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4721
diff
changeset
|
450 |
extresources = req.html_headers.getvalue(skiphead=True) |
64806b0f7431
[reledit] add ajax html head div, which allows to get additional css/js up to the browser #620569
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4721
diff
changeset
|
451 |
if extresources: |
64806b0f7431
[reledit] add ajax html head div, which allows to get additional css/js up to the browser #620569
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4721
diff
changeset
|
452 |
stream.write(u'<div class="ajaxHtmlHead">\n') |
64806b0f7431
[reledit] add ajax html head div, which allows to get additional css/js up to the browser #620569
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4721
diff
changeset
|
453 |
stream.write(extresources) |
64806b0f7431
[reledit] add ajax html head div, which allows to get additional css/js up to the browser #620569
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4721
diff
changeset
|
454 |
stream.write(u'</div>\n') |
64806b0f7431
[reledit] add ajax html head div, which allows to get additional css/js up to the browser #620569
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4721
diff
changeset
|
455 |
return stream.getvalue() |
1759
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1736
diff
changeset
|
456 |
|
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1736
diff
changeset
|
457 |
@jsonize |
0 | 458 |
def js_i18n(self, msgids): |
459 |
"""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
|
460 |
return [self._cw._(msgid) for msgid in msgids] |
0 | 461 |
|
1419 | 462 |
@jsonize |
0 | 463 |
def js_format_date(self, strdate): |
464 |
"""returns the formatted date for `msgid`""" |
|
1380 | 465 |
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
|
466 |
return self._cw.format_date(date) |
0 | 467 |
|
1419 | 468 |
@jsonize |
0 | 469 |
def js_external_resource(self, resource): |
470 |
"""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
|
471 |
return self._cw.external_resource(resource) |
0 | 472 |
|
473 |
@check_pageid |
|
1419 | 474 |
@jsonize |
0 | 475 |
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
|
476 |
page_data = self._cw.get_session_data(self._cw.pageid, {}) |
0 | 477 |
try: |
478 |
cb = page_data[cbname] |
|
479 |
except KeyError: |
|
480 |
return None |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
481 |
return cb(self._cw) |
0 | 482 |
|
483 |
if HAS_SEARCH_RESTRICTION: |
|
1419 | 484 |
@jsonize |
0 | 485 |
def js_filter_build_rql(self, names, values): |
486 |
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
|
487 |
self._cw.form = form |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
488 |
builder = FilterRQLBuilder(self._cw) |
0 | 489 |
return builder.build_rql() |
490 |
||
1419 | 491 |
@jsonize |
0 | 492 |
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
|
493 |
rqlst = self._cw.vreg.parse(self._cw, rql) # XXX Union unsupported yet |
0 | 494 |
mainvar = prepare_facets_rqlst(rqlst)[0] |
495 |
update_map = {} |
|
496 |
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
|
497 |
facet = get_facet(self._cw, facetid, rqlst.children[0], mainvar) |
0 | 498 |
update_map[facetid] = facet.possible_values() |
499 |
return update_map |
|
500 |
||
1419 | 501 |
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
|
502 |
self._cw.unregister_callback(self._cw.pageid, cbname) |
1419 | 503 |
|
504 |
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
|
505 |
self._cw.del_session_data(self._cw.pageid) |
1419 | 506 |
|
507 |
def js_cancel_edition(self, errorurl): |
|
508 |
"""cancelling edition from javascript |
|
509 |
||
510 |
We need to clear associated req's data : |
|
511 |
- errorurl |
|
512 |
- pending insertions / deletions |
|
513 |
""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
514 |
self._cw.cancel_edition(errorurl) |
1419 | 515 |
|
0 | 516 |
def js_delete_bookmark(self, beid): |
1419 | 517 |
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
|
518 |
self._cw.execute(rql, {'b': typed_eid(beid), 'u' : self._cw.user.eid}) |
1419 | 519 |
|
1844
ec51bf1b8be3
avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1839
diff
changeset
|
520 |
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
|
521 |
"""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
|
522 |
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
|
523 |
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
|
524 |
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
|
525 |
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
|
526 |
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
|
527 |
cookies[statename] = nodeeid |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
528 |
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
|
529 |
else: |
ec51bf1b8be3
avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1839
diff
changeset
|
530 |
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
|
531 |
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
|
532 |
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
|
533 |
else: |
ec51bf1b8be3
avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1839
diff
changeset
|
534 |
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
|
535 |
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
|
536 |
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
|
537 |
|
4420
a9a8628a1a87
missing jsonize, avoid spurious error w/ ff
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
538 |
@jsonize |
1419 | 539 |
def js_set_cookie(self, cookiename, cookievalue): |
540 |
# XXX we should consider jQuery.Cookie |
|
541 |
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
|
542 |
cookies = self._cw.get_cookie() |
1419 | 543 |
cookies[cookiename] = cookievalue |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
544 |
self._cw.set_cookie(cookies, cookiename) |
1419 | 545 |
|
546 |
# relations edition stuff ################################################## |
|
0 | 547 |
|
548 |
def _add_pending(self, eidfrom, rel, eidto, kind): |
|
549 |
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
|
550 |
pendings = self._cw.get_session_data(key, set()) |
0 | 551 |
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
|
552 |
self._cw.set_session_data(key, pendings) |
0 | 553 |
|
554 |
def _remove_pending(self, eidfrom, rel, eidto, kind): |
|
1419 | 555 |
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
|
556 |
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
|
557 |
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
|
558 |
self._cw.set_session_data(key, pendings) |
0 | 559 |
|
1419 | 560 |
def js_remove_pending_insert(self, (eidfrom, rel, eidto)): |
561 |
self._remove_pending(eidfrom, rel, eidto, 'insert') |
|
562 |
||
563 |
def js_add_pending_inserts(self, tripletlist): |
|
564 |
for eidfrom, rel, eidto in tripletlist: |
|
565 |
self._add_pending(eidfrom, rel, eidto, 'insert') |
|
566 |
||
567 |
def js_remove_pending_delete(self, (eidfrom, rel, eidto)): |
|
568 |
self._remove_pending(eidfrom, rel, eidto, 'delete') |
|
569 |
||
570 |
def js_add_pending_delete(self, (eidfrom, rel, eidto)): |
|
571 |
self._add_pending(eidfrom, rel, eidto, 'delete') |
|
572 |
||
573 |
# XXX specific code. Kill me and my AddComboBox friend |
|
574 |
@jsonize |
|
0 | 575 |
def js_add_and_link_new_entity(self, etype_to, rel, eid_to, etype_from, value_from): |
576 |
# 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
|
577 |
eid_from = self._cw.execute('INSERT %s T : T name "%s"' % ( etype_from, value_from ))[0][0] |
0 | 578 |
# link the new entity to the main entity |
579 |
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} |
|
580 |
return eid_from |
|
603
18c6c31bbaf4
[controllers] a set_cookie method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
581
diff
changeset
|
581 |
|
18c6c31bbaf4
[controllers] a set_cookie method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
581
diff
changeset
|
582 |
|
0 | 583 |
class SendMailController(Controller): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
584 |
__regid__ = 'sendmail' |
4894
41c28ddca055
[cleanup selectors] use authenticated_user, check for form params instead of handling potential key error on missing params
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4741
diff
changeset
|
585 |
__select__ = authenticated_user() & match_form_params('recipient', 'mailbody', 'subject') |
0 | 586 |
|
587 |
def recipients(self): |
|
588 |
"""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
|
589 |
eids = self._cw.form['recipient'] |
0 | 590 |
# make sure we have a list even though only one recipient was specified |
591 |
if isinstance(eids, basestring): |
|
592 |
eids = (eids,) |
|
593 |
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
|
594 |
rset = self._cw.execute(rql) |
0 | 595 |
for entity in rset.entities(): |
596 |
yield entity |
|
597 |
||
598 |
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
|
599 |
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
|
600 |
'name' : self._cw.user.dc_title(),}, |
0 | 601 |
[recipient], body, subject) |
5198
cf8292f80384
[controller] refactor send mail controller to use cwconfig.sendmails
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5157
diff
changeset
|
602 |
if not self._cw.vreg.config.sendmails([(msg, [recipient])]): |
cf8292f80384
[controller] refactor send mail controller to use cwconfig.sendmails
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5157
diff
changeset
|
603 |
msg = self._cw._('could not connect to the SMTP server') |
cf8292f80384
[controller] refactor send mail controller to use cwconfig.sendmails
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5157
diff
changeset
|
604 |
url = self._cw.build_url(__message=msg) |
cf8292f80384
[controller] refactor send mail controller to use cwconfig.sendmails
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5157
diff
changeset
|
605 |
raise Redirect(url) |
0 | 606 |
|
607 |
def publish(self, rset=None): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
608 |
# 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
|
609 |
# a mail relay |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
610 |
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
|
611 |
subject = self._cw.form['subject'] |
0 | 612 |
for recipient in self.recipients(): |
613 |
text = body % recipient.as_email_context() |
|
614 |
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
|
615 |
# 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
|
616 |
url = self._cw.build_url(__message=self._cw._('emails successfully sent')) |
0 | 617 |
raise Redirect(url) |
618 |
||
619 |
||
620 |
class MailBugReportController(SendMailController): |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
621 |
__regid__ = 'reportbug' |
4894
41c28ddca055
[cleanup selectors] use authenticated_user, check for form params instead of handling potential key error on missing params
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4741
diff
changeset
|
622 |
__select__ = match_form_params('description') |
0 | 623 |
|
624 |
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
|
625 |
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
|
626 |
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
|
627 |
url = self._cw.build_url(__message=self._cw._('bug report sent')) |
0 | 628 |
raise Redirect(url) |
1419 | 629 |
|
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
630 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
631 |
class UndoController(SendMailController): |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
632 |
__regid__ = 'undo' |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
633 |
__select__ = authenticated_user() & match_form_params('txuuid') |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
634 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
635 |
def publish(self, rset=None): |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
636 |
txuuid = self._cw.form['txuuid'] |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
637 |
errors = self._cw.cnx.undo_transaction(txuuid) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
638 |
if errors: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
639 |
self.w(self._cw._('some errors occured:')) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
640 |
self.wview('pyvalist', pyvalue=errors) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
641 |
else: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
642 |
self.redirect() |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
643 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
644 |
def redirect(self): |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
645 |
req = self._cw |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
646 |
breadcrumbs = req.get_session_data('breadcrumbs', None) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
647 |
if breadcrumbs is not None and len(breadcrumbs) > 1: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
648 |
url = req.rebuild_url(breadcrumbs[-2], |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
649 |
__message=req._('transaction undoed')) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
650 |
else: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
651 |
url = req.build_url(__message=req._('transaction undoed')) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
652 |
raise Redirect(url) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
653 |