author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 29 Mar 2010 10:20:01 +0200 | |
branch | stable |
changeset 5054 | cb066d29166a |
parent 4984 | 6cb91be7707f |
child 5113 | f8cbdb51e6d4 |
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') |
898c35be5873
#750055: make it easier to change post logout url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4741
diff
changeset
|
94 |
if self._cw.https: |
898c35be5873
#750055: make it easier to change post logout url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4741
diff
changeset
|
95 |
# XXX hack to generate an url on the http version of the site |
898c35be5873
#750055: make it easier to change post logout url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4741
diff
changeset
|
96 |
self._cw._base_url = self._cw.vreg.config['base-url'] |
898c35be5873
#750055: make it easier to change post logout url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4741
diff
changeset
|
97 |
self._cw.https = False |
898c35be5873
#750055: make it easier to change post logout url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4741
diff
changeset
|
98 |
return self._cw.build_url('view', vid='index', __message=msg) |
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,) |
|
279 |
args = [simplejson.loads(arg) for arg in args] |
|
0 | 280 |
try: |
1419 | 281 |
result = func(*args) |
282 |
except RemoteCallFailed: |
|
283 |
raise |
|
284 |
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
|
285 |
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
|
286 |
traceback.print_exc() |
1419 | 287 |
self.exception('an exception occured while calling js_%s(%s): %s', |
288 |
fname, args, ex) |
|
289 |
raise RemoteCallFailed(repr(ex)) |
|
290 |
if result is None: |
|
291 |
return '' |
|
292 |
# get unicode on @htmlize methods, encoded string on @jsonize methods |
|
293 |
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
|
294 |
return result.encode(self._cw.encoding) |
1419 | 295 |
return result |
296 |
||
297 |
def _rebuild_posted_form(self, names, values, action=None): |
|
298 |
form = {} |
|
299 |
for name, value in zip(names, values): |
|
300 |
# remove possible __action_xxx inputs |
|
301 |
if name.startswith('__action'): |
|
302 |
continue |
|
303 |
# form.setdefault(name, []).append(value) |
|
304 |
if name in form: |
|
305 |
curvalue = form[name] |
|
306 |
if isinstance(curvalue, list): |
|
307 |
curvalue.append(value) |
|
308 |
else: |
|
309 |
form[name] = [curvalue, value] |
|
310 |
else: |
|
311 |
form[name] = value |
|
312 |
# simulate click on __action_%s button to help the controller |
|
313 |
if action: |
|
314 |
form['__action_%s' % action] = u'whatever' |
|
315 |
return form |
|
0 | 316 |
|
317 |
def _exec(self, rql, args=None, eidkey=None, rocheck=True): |
|
318 |
"""json mode: execute RQL and return resultset as json""" |
|
319 |
if rocheck: |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
320 |
self._cw.ensure_ro_rql(rql) |
0 | 321 |
try: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
322 |
return self._cw.execute(rql, args, eidkey) |
0 | 323 |
except Exception, ex: |
324 |
self.exception("error in _exec(rql=%s): %s", rql, ex) |
|
325 |
return None |
|
326 |
return None |
|
327 |
||
2852
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
328 |
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
|
329 |
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
|
330 |
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
|
331 |
# 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
|
332 |
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
|
333 |
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
|
334 |
if divid == 'pageContent': |
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
335 |
# 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
|
336 |
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
|
337 |
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
|
338 |
if vtitle: |
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
339 |
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
|
340 |
view.paginate() |
2852
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
341 |
if divid == 'pageContent': |
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
342 |
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
|
343 |
view.render(**kwargs) |
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
344 |
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
|
345 |
if extresources: |
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
346 |
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
|
347 |
stream.write(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>\n') |
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
349 |
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
|
350 |
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
|
351 |
return stream.getvalue() |
858b33162e9d
[ajax] allow inlineCreationForms to add their own JS / CSS
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2681
diff
changeset
|
352 |
|
1419 | 353 |
@xhtmlize |
354 |
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
|
355 |
# 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
|
356 |
req = self._cw |
0 | 357 |
rql = req.form.get('rql') |
1419 | 358 |
if rql: |
0 | 359 |
rset = self._exec(rql) |
1419 | 360 |
else: |
361 |
rset = None |
|
3659 | 362 |
vid = req.form.get('vid') or vid_from_rset(req, rset, self._cw.vreg.schema) |
0 | 363 |
try: |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
364 |
view = self._cw.vreg['views'].select(vid, req, rset=rset) |
0 | 365 |
except NoSelectableObject: |
366 |
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
|
367 |
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
|
368 |
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
|
369 |
return self._call_view(view) |
0 | 370 |
|
1419 | 371 |
@xhtmlize |
372 |
def js_prop_widget(self, propkey, varname, tabindex=None): |
|
373 |
"""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
|
374 |
entity = self._cw.vreg['etypes'].etype_class('CWProperty')(self._cw) |
1419 | 375 |
entity.eid = varname |
376 |
entity['pkey'] = propkey |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
377 |
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
|
378 |
form.build_context() |
1419 | 379 |
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
|
380 |
renderer = FormRenderer(self._cw) |
1419 | 381 |
return vfield.render(form, renderer, tabindex=tabindex) \ |
382 |
+ renderer.render_help(form, vfield) |
|
0 | 383 |
|
1419 | 384 |
@xhtmlize |
385 |
def js_component(self, compid, rql, registry='components', extraargs=None): |
|
386 |
if rql: |
|
387 |
rset = self._exec(rql) |
|
388 |
else: |
|
389 |
rset = None |
|
390 |
if extraargs is None: |
|
391 |
extraargs = {} |
|
392 |
else: # we receive unicode keys which is not supported by the **syntax |
|
393 |
extraargs = dict((str(key), value) |
|
394 |
for key, value in extraargs.items()) |
|
4741
f9a176ebe090
[book/controllers] add some content (overview, api super sketch) for the cubicweb controllers
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4739
diff
changeset
|
395 |
comp = self._cw.vreg[registry].select(compid, self._cw, rset=rset, **extraargs) |
1419 | 396 |
extraargs = extraargs or {} |
4976
34fd21f65702
ticket #636927, add ajaxhtmlhead in controller js_component
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4911
diff
changeset
|
397 |
stream = comp.set_stream() |
34fd21f65702
ticket #636927, add ajaxhtmlhead in controller js_component
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4911
diff
changeset
|
398 |
comp.render(**extraargs) |
34fd21f65702
ticket #636927, add ajaxhtmlhead in controller js_component
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4911
diff
changeset
|
399 |
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
|
400 |
if extresources: |
34fd21f65702
ticket #636927, add ajaxhtmlhead in controller js_component
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4911
diff
changeset
|
401 |
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
|
402 |
stream.write(extresources) |
34fd21f65702
ticket #636927, add ajaxhtmlhead in controller js_component
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4911
diff
changeset
|
403 |
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
|
404 |
return stream.getvalue() |
1419 | 405 |
|
406 |
@check_pageid |
|
407 |
@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
|
408 |
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
|
409 |
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
|
410 |
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
|
411 |
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
|
412 |
return self._call_view(view, i18nctx=i18nctx) |
1419 | 413 |
|
414 |
@jsonize |
|
0 | 415 |
def js_validate_form(self, action, names, values): |
1527
c8ca1782e252
controller fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1467
diff
changeset
|
416 |
return self.validate_form(action, names, values) |
c8ca1782e252
controller fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1467
diff
changeset
|
417 |
|
c8ca1782e252
controller fixes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1467
diff
changeset
|
418 |
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
|
419 |
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
|
420 |
return _validate_form(self._cw, self._cw.vreg) |
0 | 421 |
|
3742
20f429eb5f46
kill separate attribute client-side handling #473636
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3608
diff
changeset
|
422 |
@xhtmlize |
20f429eb5f46
kill separate attribute client-side handling #473636
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3608
diff
changeset
|
423 |
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
|
424 |
req = self._cw |
4721
8f63691ccb7f
pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
425 |
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
|
426 |
for x in frozenset(('rtype', 'role', 'reload', 'landing_zone'))) |
3777 | 427 |
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
|
428 |
# note: default is reserved in js land |
3777 | 429 |
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
|
430 |
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
|
431 |
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
|
432 |
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
|
433 |
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
|
434 |
view.render(**args) |
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
|
435 |
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
|
436 |
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
|
437 |
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
|
438 |
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
|
439 |
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
|
440 |
return stream.getvalue() |
1759
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1736
diff
changeset
|
441 |
|
61d026ced19f
preliminary support for inline edition of relations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1736
diff
changeset
|
442 |
@jsonize |
0 | 443 |
def js_i18n(self, msgids): |
444 |
"""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
|
445 |
return [self._cw._(msgid) for msgid in msgids] |
0 | 446 |
|
1419 | 447 |
@jsonize |
0 | 448 |
def js_format_date(self, strdate): |
449 |
"""returns the formatted date for `msgid`""" |
|
1380 | 450 |
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
|
451 |
return self._cw.format_date(date) |
0 | 452 |
|
1419 | 453 |
@jsonize |
0 | 454 |
def js_external_resource(self, resource): |
455 |
"""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
|
456 |
return self._cw.external_resource(resource) |
0 | 457 |
|
458 |
@check_pageid |
|
1419 | 459 |
@jsonize |
0 | 460 |
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
|
461 |
page_data = self._cw.get_session_data(self._cw.pageid, {}) |
0 | 462 |
try: |
463 |
cb = page_data[cbname] |
|
464 |
except KeyError: |
|
465 |
return None |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
466 |
return cb(self._cw) |
0 | 467 |
|
468 |
if HAS_SEARCH_RESTRICTION: |
|
1419 | 469 |
@jsonize |
0 | 470 |
def js_filter_build_rql(self, names, values): |
471 |
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
|
472 |
self._cw.form = form |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
473 |
builder = FilterRQLBuilder(self._cw) |
0 | 474 |
return builder.build_rql() |
475 |
||
1419 | 476 |
@jsonize |
0 | 477 |
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
|
478 |
rqlst = self._cw.vreg.parse(self._cw, rql) # XXX Union unsupported yet |
0 | 479 |
mainvar = prepare_facets_rqlst(rqlst)[0] |
480 |
update_map = {} |
|
481 |
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
|
482 |
facet = get_facet(self._cw, facetid, rqlst.children[0], mainvar) |
0 | 483 |
update_map[facetid] = facet.possible_values() |
484 |
return update_map |
|
485 |
||
1419 | 486 |
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
|
487 |
self._cw.unregister_callback(self._cw.pageid, cbname) |
1419 | 488 |
|
489 |
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
|
490 |
self._cw.del_session_data(self._cw.pageid) |
1419 | 491 |
|
492 |
def js_cancel_edition(self, errorurl): |
|
493 |
"""cancelling edition from javascript |
|
494 |
||
495 |
We need to clear associated req's data : |
|
496 |
- errorurl |
|
497 |
- pending insertions / deletions |
|
498 |
""" |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
499 |
self._cw.cancel_edition(errorurl) |
1419 | 500 |
|
0 | 501 |
def js_delete_bookmark(self, beid): |
1419 | 502 |
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
|
503 |
self._cw.execute(rql, {'b': typed_eid(beid), 'u' : self._cw.user.eid}) |
1419 | 504 |
|
1844
ec51bf1b8be3
avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1839
diff
changeset
|
505 |
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
|
506 |
"""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
|
507 |
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
|
508 |
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
|
509 |
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
|
510 |
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
|
511 |
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
|
512 |
cookies[statename] = nodeeid |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
513 |
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
|
514 |
else: |
ec51bf1b8be3
avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1839
diff
changeset
|
515 |
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
|
516 |
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
|
517 |
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
|
518 |
else: |
ec51bf1b8be3
avoid monkeypatching JsonController in cw, to avoid _potential_ load order problems
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1839
diff
changeset
|
519 |
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
|
520 |
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
|
521 |
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
|
522 |
|
4420
a9a8628a1a87
missing jsonize, avoid spurious error w/ ff
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4212
diff
changeset
|
523 |
@jsonize |
1419 | 524 |
def js_set_cookie(self, cookiename, cookievalue): |
525 |
# XXX we should consider jQuery.Cookie |
|
526 |
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
|
527 |
cookies = self._cw.get_cookie() |
1419 | 528 |
cookies[cookiename] = cookievalue |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
529 |
self._cw.set_cookie(cookies, cookiename) |
1419 | 530 |
|
531 |
# relations edition stuff ################################################## |
|
0 | 532 |
|
533 |
def _add_pending(self, eidfrom, rel, eidto, kind): |
|
534 |
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
|
535 |
pendings = self._cw.get_session_data(key, set()) |
0 | 536 |
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
|
537 |
self._cw.set_session_data(key, pendings) |
0 | 538 |
|
539 |
def _remove_pending(self, eidfrom, rel, eidto, kind): |
|
1419 | 540 |
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
|
541 |
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
|
542 |
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
|
543 |
self._cw.set_session_data(key, pendings) |
0 | 544 |
|
1419 | 545 |
def js_remove_pending_insert(self, (eidfrom, rel, eidto)): |
546 |
self._remove_pending(eidfrom, rel, eidto, 'insert') |
|
547 |
||
548 |
def js_add_pending_inserts(self, tripletlist): |
|
549 |
for eidfrom, rel, eidto in tripletlist: |
|
550 |
self._add_pending(eidfrom, rel, eidto, 'insert') |
|
551 |
||
552 |
def js_remove_pending_delete(self, (eidfrom, rel, eidto)): |
|
553 |
self._remove_pending(eidfrom, rel, eidto, 'delete') |
|
554 |
||
555 |
def js_add_pending_delete(self, (eidfrom, rel, eidto)): |
|
556 |
self._add_pending(eidfrom, rel, eidto, 'delete') |
|
557 |
||
558 |
# XXX specific code. Kill me and my AddComboBox friend |
|
559 |
@jsonize |
|
0 | 560 |
def js_add_and_link_new_entity(self, etype_to, rel, eid_to, etype_from, value_from): |
561 |
# 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
|
562 |
eid_from = self._cw.execute('INSERT %s T : T name "%s"' % ( etype_from, value_from ))[0][0] |
0 | 563 |
# link the new entity to the main entity |
564 |
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} |
|
565 |
return eid_from |
|
603
18c6c31bbaf4
[controllers] a set_cookie method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
581
diff
changeset
|
566 |
|
18c6c31bbaf4
[controllers] a set_cookie method
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
581
diff
changeset
|
567 |
|
0 | 568 |
class SendMailController(Controller): |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
569 |
__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
|
570 |
__select__ = authenticated_user() & match_form_params('recipient', 'mailbody', 'subject') |
0 | 571 |
|
572 |
def recipients(self): |
|
573 |
"""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
|
574 |
eids = self._cw.form['recipient'] |
0 | 575 |
# make sure we have a list even though only one recipient was specified |
576 |
if isinstance(eids, basestring): |
|
577 |
eids = (eids,) |
|
578 |
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
|
579 |
rset = self._cw.execute(rql) |
0 | 580 |
for entity in rset.entities(): |
581 |
yield entity |
|
582 |
||
583 |
@property |
|
584 |
@cached |
|
585 |
def smtp(self): |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
586 |
mailhost, port = self._cw.config['smtp-host'], self._cw.config['smtp-port'] |
0 | 587 |
try: |
588 |
return SMTP(mailhost, port) |
|
589 |
except Exception, ex: |
|
590 |
self.exception("can't connect to smtp server %s:%s (%s)", |
|
591 |
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
|
592 |
url = self._cw.build_url(__message=self._cw._('could not connect to the SMTP server')) |
0 | 593 |
raise Redirect(url) |
594 |
||
595 |
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
|
596 |
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
|
597 |
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
|
598 |
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
|
599 |
'name' : self._cw.user.dc_title(),}, |
0 | 600 |
[recipient], body, subject) |
1419 | 601 |
self.smtp.sendmail(helo_addr, [recipient], msg.as_string()) |
0 | 602 |
|
603 |
def publish(self, rset=None): |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2458
diff
changeset
|
604 |
# 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
|
605 |
# a mail relay |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
606 |
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
|
607 |
subject = self._cw.form['subject'] |
0 | 608 |
for recipient in self.recipients(): |
609 |
text = body % recipient.as_email_context() |
|
610 |
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
|
611 |
# 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
|
612 |
url = self._cw.build_url(__message=self._cw._('emails successfully sent')) |
0 | 613 |
raise Redirect(url) |
614 |
||
615 |
||
616 |
class MailBugReportController(SendMailController): |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3369
diff
changeset
|
617 |
__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
|
618 |
__select__ = match_form_params('description') |
0 | 619 |
|
620 |
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
|
621 |
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
|
622 |
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
|
623 |
url = self._cw.build_url(__message=self._cw._('bug report sent')) |
0 | 624 |
raise Redirect(url) |
1419 | 625 |
|
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
626 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
627 |
class UndoController(SendMailController): |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
628 |
__regid__ = 'undo' |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
629 |
__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
|
630 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
631 |
def publish(self, rset=None): |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
632 |
txuuid = self._cw.form['txuuid'] |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
633 |
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
|
634 |
if errors: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
635 |
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
|
636 |
self.wview('pyvalist', pyvalue=errors) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
637 |
else: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
638 |
self.redirect() |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
639 |
|
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
640 |
def redirect(self): |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
641 |
req = self._cw |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
642 |
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
|
643 |
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
|
644 |
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
|
645 |
__message=req._('transaction undoed')) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
646 |
else: |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
647 |
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
|
648 |
raise Redirect(url) |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4894
diff
changeset
|
649 |