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