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