author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Mon, 16 Feb 2009 16:24:24 +0100 | |
branch | tls-sprint |
changeset 630 | 66ff0b2f7d03 |
parent 607 | 47f2ee781d6d |
child 631 | 99f5852f8604 |
child 1333 | 85d1bc9da0ad |
permissions | -rw-r--r-- |
0 | 1 |
"""management and error screens |
2 |
||
3 |
||
4 |
:organization: Logilab |
|
5 |
:copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
7 |
""" |
|
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
from logilab.mtconverter import html_escape |
|
11 |
||
12 |
from logilab.common.decorators import cached |
|
13 |
||
14 |
from cubicweb.common.utils import UStringIO |
|
15 |
from cubicweb.common.view import AnyRsetView, StartupView, EntityView |
|
604
e85042d18b48
[preferences] preferences sections will now remember their collapsed/open status
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
602
diff
changeset
|
16 |
from cubicweb.common.uilib import html_traceback, rest_traceback |
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
174
diff
changeset
|
17 |
from cubicweb.common.selectors import (yes, one_line_rset, |
604
e85042d18b48
[preferences] preferences sections will now remember their collapsed/open status
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
602
diff
changeset
|
18 |
accept_rset, none_rset, |
e85042d18b48
[preferences] preferences sections will now remember their collapsed/open status
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
602
diff
changeset
|
19 |
chainfirst, chainall) |
0 | 20 |
from cubicweb.web import INTERNAL_FIELD_VALUE, eid_param, stdmsgs |
21 |
from cubicweb.web.widgets import StaticComboBoxWidget |
|
22 |
from cubicweb.web.form import FormMixIn |
|
23 |
||
24 |
_ = unicode |
|
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
25 |
|
0 | 26 |
def begin_form(w, entity, redirectvid, redirectpath=None, msg=None): |
27 |
w(u'<form method="post" action="%s">\n' % entity.req.build_url('edit')) |
|
28 |
w(u'<fieldset>\n') |
|
29 |
w(u'<input type="hidden" name="__redirectvid" value="%s"/>\n' % redirectvid) |
|
30 |
w(u'<input type="hidden" name="__redirectpath" value="%s"/>\n' % ( |
|
31 |
html_escape(redirectpath or entity.rest_path()))) |
|
32 |
w(u'<input type="hidden" name="eid" value="%s"/>\n' % entity.eid) |
|
33 |
w(u'<input type="hidden" name="%s" value="%s"/>\n' % ( |
|
34 |
eid_param('__type', entity.eid), entity.e_schema)) |
|
35 |
if msg: |
|
36 |
w(u'<input type="hidden" name="__message" value="%s"/>\n' |
|
37 |
% html_escape(msg)) |
|
38 |
||
39 |
||
40 |
class SecurityManagementView(EntityView): |
|
41 |
"""display security information for a given entity""" |
|
42 |
id = 'security' |
|
43 |
title = _('security') |
|
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
44 |
|
0 | 45 |
def cell_call(self, row, col): |
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
46 |
self.req.add_js('cubicweb.edition.js') |
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
47 |
self.req.add_css('cubicweb.acl.css') |
0 | 48 |
entity = self.entity(row, col) |
49 |
w = self.w |
|
50 |
_ = self.req._ |
|
51 |
w(u'<h1><span class="etype">%s</span> <a href="%s">%s</a></h1>' |
|
52 |
% (entity.dc_type().capitalize(), |
|
53 |
html_escape(entity.absolute_url()), |
|
54 |
html_escape(entity.dc_title()))) |
|
55 |
# first show permissions defined by the schema |
|
56 |
self.w('<h2>%s</h2>' % _('schema\'s permissions definitions')) |
|
57 |
self.schema_definition(entity) |
|
58 |
self.w('<h2>%s</h2>' % _('manage security')) |
|
59 |
# ownership information |
|
60 |
if self.schema.rschema('owned_by').has_perm(self.req, 'add', |
|
61 |
fromeid=entity.eid): |
|
62 |
self.owned_by_edit_form(entity) |
|
63 |
else: |
|
64 |
self.owned_by_information(entity) |
|
65 |
# epermissions |
|
66 |
if 'require_permission' in entity.e_schema.subject_relations(): |
|
67 |
w('<h3>%s</h3>' % _('permissions for this entity')) |
|
68 |
reqpermschema = self.schema.rschema('require_permission') |
|
69 |
self.require_permission_information(entity, reqpermschema) |
|
70 |
if reqpermschema.has_perm(self.req, 'add', fromeid=entity.eid): |
|
71 |
self.require_permission_edit_form(entity) |
|
72 |
||
73 |
def schema_definition(self, entity): |
|
74 |
w = self.w |
|
75 |
_ = self.req._ |
|
76 |
w(u'<table class="schemaInfo">') |
|
77 |
w(u'<tr><th>%s</th><th>%s</th><th>%s</th></tr>' % ( |
|
78 |
_("access type"), _('granted to groups'), _('rql expressions'))) |
|
79 |
for access_type in ('read', 'add', 'update', 'delete'): |
|
80 |
w(u'<tr>') |
|
81 |
w(u'<th>%s</th>' % self.req.__('%s_permission' % access_type)) |
|
82 |
groups = entity.e_schema.get_groups(access_type) |
|
83 |
l = [] |
|
84 |
for group in groups: |
|
85 |
l.append(u'<a href="%s">%s</a>' % ( |
|
86 |
self.build_url('egroup/%s' % group), _(group))) |
|
87 |
w(u'<td>%s</td>' % u', '.join(l)) |
|
88 |
rqlexprs = entity.e_schema.get_rqlexprs(access_type) |
|
89 |
w(u'<td>%s</td>' % u'<br/>'.join(expr.expression for expr in rqlexprs)) |
|
90 |
w(u'</tr>\n') |
|
91 |
w(u'</table>') |
|
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
92 |
|
0 | 93 |
def owned_by_edit_form(self, entity): |
94 |
self.w('<h3>%s</h3>' % self.req._('ownership')) |
|
95 |
begin_form(self.w, entity, 'security', msg= _('ownerships have been changed')) |
|
96 |
self.w(u'<table border="0">\n') |
|
97 |
self.w(u'<tr><td>\n') |
|
98 |
wdg = entity.get_widget('owned_by') |
|
99 |
self.w(wdg.edit_render(entity)) |
|
100 |
self.w(u'</td><td>\n') |
|
101 |
self.w(self.button_ok()) |
|
102 |
self.w(u'</td></tr>\n</table>\n') |
|
103 |
self.w(u'</fieldset></form>\n') |
|
104 |
||
105 |
def owned_by_information(self, entity): |
|
106 |
ownersrset = entity.related('owned_by') |
|
107 |
if ownersrset: |
|
108 |
self.w('<h3>%s</h3>' % self.req._('ownership')) |
|
109 |
self.w(u'<div class="ownerInfo">') |
|
110 |
self.w(self.req._('this entity is currently owned by') + ' ') |
|
111 |
self.wview('csv', entity.related('owned_by'), 'null') |
|
112 |
self.w(u'</div>') |
|
113 |
# else we don't know if this is because entity has no owner or becayse |
|
114 |
# user as no access to owner users entities |
|
115 |
||
116 |
def require_permission_information(self, entity, reqpermschema): |
|
117 |
if entity.require_permission: |
|
118 |
w = self.w |
|
119 |
_ = self.req._ |
|
120 |
if reqpermschema.has_perm(self.req, 'delete', fromeid=entity.eid): |
|
121 |
delurl = self.build_url('edit', __redirectvid='security', |
|
122 |
__redirectpath=entity.rest_path()) |
|
123 |
delurl = delurl.replace('%', '%%') |
|
124 |
# don't give __delete value to build_url else it will be urlquoted |
|
125 |
# and this will replace %s by %25s |
|
126 |
delurl += '&__delete=%s:require_permission:%%s' % entity.eid |
|
127 |
dellinktempl = u'[<a href="%s" title="%s">-</a>] ' % ( |
|
128 |
html_escape(delurl), _('delete this permission')) |
|
129 |
else: |
|
130 |
dellinktempl = None |
|
131 |
w(u'<table class="schemaInfo">') |
|
132 |
w(u'<tr><th>%s</th><th>%s</th></tr>' % (_("permission"), |
|
133 |
_('granted to groups'))) |
|
134 |
for eperm in entity.require_permission: |
|
135 |
w(u'<tr>') |
|
136 |
if dellinktempl: |
|
137 |
w(u'<td>%s%s</td>' % (dellinktempl % eperm.eid, |
|
138 |
eperm.view('oneline'))) |
|
139 |
else: |
|
140 |
w(u'<td>%s</td>' % eperm.view('oneline')) |
|
141 |
w(u'<td>%s</td>' % self.view('csv', eperm.related('require_group'), 'null')) |
|
142 |
w(u'</tr>\n') |
|
143 |
w(u'</table>') |
|
144 |
else: |
|
145 |
self.w(self.req._('no associated epermissions')) |
|
146 |
||
147 |
def require_permission_edit_form(self, entity): |
|
148 |
w = self.w |
|
149 |
_ = self.req._ |
|
150 |
newperm = self.vreg.etype_class('EPermission')(self.req, None) |
|
151 |
newperm.eid = self.req.varmaker.next() |
|
152 |
w(u'<p>%s</p>' % _('add a new permission')) |
|
153 |
begin_form(w, newperm, 'security', entity.rest_path()) |
|
154 |
w(u'<input type="hidden" name="%s" value="__cubicweb_internal_field__"/>' |
|
155 |
% eid_param('edito-require_permission', newperm.eid)) |
|
156 |
w(u'<input type="hidden" name="%s" value="%s"/>' |
|
157 |
% (eid_param('require_permission', newperm.eid), entity.eid)) |
|
158 |
w(u'<table border="0">\n') |
|
159 |
w(u'<tr><th>%s</th><th>%s</th><th>%s</th><th> </th></tr>\n' |
|
160 |
% (_("name"), _("label"), _('granted to groups'))) |
|
161 |
if getattr(entity, '__permissions__', None): |
|
162 |
wdg = StaticComboBoxWidget(self.vreg, self.schema['EPermission'], |
|
163 |
self.schema['name'], self.schema['String'], |
|
164 |
vocabfunc=lambda x: entity.__permissions__) |
|
165 |
else: |
|
166 |
wdg = newperm.get_widget('name') |
|
167 |
w(u'<tr><td>%s</td>\n' % wdg.edit_render(newperm)) |
|
168 |
wdg = newperm.get_widget('label') |
|
169 |
w(u'<td>%s</td>\n' % wdg.edit_render(newperm)) |
|
170 |
wdg = newperm.get_widget('require_group') |
|
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
171 |
w(u'<td>%s</td>\n' % wdg.edit_render(newperm)) |
0 | 172 |
w(u'<td>%s</td></tr>\n' % self.button_ok()) |
173 |
w(u'</table>') |
|
174 |
w(u'</fieldset></form>\n') |
|
175 |
||
176 |
def button_ok(self): |
|
177 |
return (u'<input class="validateButton" type="submit" name="submit" value="%s"/>' |
|
178 |
% self.req._(stdmsgs.BUTTON_OK)) |
|
179 |
||
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
180 |
|
0 | 181 |
class ErrorView(AnyRsetView): |
182 |
"""default view when no result has been found""" |
|
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
174
diff
changeset
|
183 |
__selectors__ = (yes,) |
0 | 184 |
id = 'error' |
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
185 |
|
0 | 186 |
def page_title(self): |
187 |
"""returns a title according to the result set - used for the |
|
188 |
title in the HTML header |
|
189 |
""" |
|
190 |
return self.req._('an error occured') |
|
191 |
||
192 |
def call(self): |
|
193 |
req = self.req.reset_headers() |
|
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
194 |
_ = req._; w = self.w |
0 | 195 |
ex = req.data.get('ex')#_("unable to find exception information")) |
196 |
excinfo = req.data.get('excinfo') |
|
197 |
title = _('an error occured') |
|
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
198 |
w(u'<h2>%s</h2>' % title) |
0 | 199 |
if 'errmsg' in req.data: |
200 |
ex = req.data['errmsg'] |
|
174
a2966960d550
get actual exception class instead of 'unicode' in the error view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
201 |
exclass = None |
0 | 202 |
else: |
174
a2966960d550
get actual exception class instead of 'unicode' in the error view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
203 |
exclass = ex.__class__.__name__ |
0 | 204 |
ex = exc_message(ex, req.encoding) |
205 |
if excinfo is not None and self.config['print-traceback']: |
|
174
a2966960d550
get actual exception class instead of 'unicode' in the error view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
206 |
if exclass is None: |
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
207 |
w(u'<div class="tb">%s</div>' |
174
a2966960d550
get actual exception class instead of 'unicode' in the error view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
208 |
% html_escape(ex).replace("\n","<br />")) |
a2966960d550
get actual exception class instead of 'unicode' in the error view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
209 |
else: |
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
210 |
w(u'<div class="tb">%s: %s</div>' |
174
a2966960d550
get actual exception class instead of 'unicode' in the error view
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
211 |
% (exclass, html_escape(ex).replace("\n","<br />"))) |
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
212 |
w(u'<hr />') |
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
213 |
w(u'<div class="tb">%s</div>' % html_traceback(excinfo, ex, '')) |
0 | 214 |
else: |
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
215 |
w(u'<div class="tb">%s</div>' % (html_escape(ex).replace("\n","<br />"))) |
0 | 216 |
# if excinfo is not None, it's probably not a bug |
217 |
if excinfo is None: |
|
218 |
return |
|
219 |
vcconf = self.config.vc_config() |
|
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
220 |
w(u"<div>") |
0 | 221 |
eversion = vcconf.get('cubicweb', _('no version information')) |
222 |
# NOTE: tuple wrapping needed since eversion is itself a tuple |
|
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
223 |
w(u"<b>CubicWeb version:</b> %s<br/>\n" % (eversion,)) |
0 | 224 |
for pkg in self.config.cubes(): |
225 |
pkgversion = vcconf.get(pkg, _('no version information')) |
|
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
226 |
w(u"<b>Package %s version:</b> %s<br/>\n" % (pkg, pkgversion)) |
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
227 |
w(u"</div>") |
0 | 228 |
# creates a bug submission link if SUBMIT_URL is set |
229 |
submiturl = self.config['submit-url'] |
|
230 |
if submiturl: |
|
231 |
binfo = text_error_description(ex, excinfo, req, eversion, |
|
232 |
[(pkg, vcconf.get(pkg, _('no version information'))) |
|
233 |
for pkg in self.config.cubes()]) |
|
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
234 |
w(u'<form action="%s" method="post">\n' % html_escape(submiturl)) |
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
235 |
w(u'<fieldset>\n') |
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
236 |
w(u'<textarea class="hidden" name="description">%s</textarea>' % html_escape(binfo)) |
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
237 |
w(u'<input type="hidden" name="description_format" value="text/rest"/>') |
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
238 |
w(u'<input type="hidden" name="__bugreporting" value="1"/>') |
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
239 |
w(u'<input type="submit" value="%s"/>' % _('Submit bug report')) |
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
240 |
w(u'</fieldset>\n') |
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
241 |
w(u'</form>\n') |
0 | 242 |
submitmail = self.config['submit-mail'] |
243 |
if submitmail: |
|
244 |
binfo = text_error_description(ex, excinfo, req, eversion, |
|
245 |
[(pkg, vcconf.get(pkg, _('no version information'))) |
|
246 |
for pkg in self.config.cubes()]) |
|
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
247 |
w(u'<form action="%s" method="post">\n' % req.build_url('reportbug')) |
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
248 |
w(u'<fieldset>\n') |
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
249 |
w(u'<input type="hidden" name="description" value="%s"/>' % html_escape(binfo)) |
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
250 |
w(u'<input type="hidden" name="__bugreporting" value="1"/>') |
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
251 |
w(u'<input type="submit" value="%s"/>' % _('Submit bug report by mail')) |
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
252 |
w(u'</fieldset>\n') |
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
253 |
w(u'</form>\n') |
0 | 254 |
|
255 |
||
256 |
def exc_message(ex, encoding): |
|
257 |
try: |
|
258 |
return unicode(ex) |
|
259 |
except: |
|
260 |
try: |
|
261 |
return unicode(str(ex), encoding, 'replace') |
|
262 |
except: |
|
263 |
return unicode(repr(ex), encoding, 'replace') |
|
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
264 |
|
0 | 265 |
def text_error_description(ex, excinfo, req, eversion, cubes): |
266 |
binfo = rest_traceback(excinfo, html_escape(ex)) |
|
267 |
binfo += u'\n\n:URL: %s\n' % req.url() |
|
268 |
if not '__bugreporting' in req.form: |
|
269 |
binfo += u'\n:form params:\n' |
|
270 |
binfo += u'\n'.join(u' * %s = %s' % (k, v) for k, v in req.form.iteritems()) |
|
271 |
binfo += u'\n\n:CubicWeb version: %s\n' % (eversion,) |
|
272 |
for pkg, pkgversion in cubes: |
|
273 |
binfo += u":Package %s version: %s\n" % (pkg, pkgversion) |
|
274 |
binfo += '\n' |
|
275 |
return binfo |
|
276 |
||
277 |
# some string we want to be internationalizable for nicer display of eproperty |
|
278 |
# groups |
|
279 |
_('navigation') |
|
280 |
_('ui') |
|
281 |
_('actions') |
|
282 |
_('boxes') |
|
283 |
_('components') |
|
284 |
_('contentnavigation') |
|
285 |
||
604
e85042d18b48
[preferences] preferences sections will now remember their collapsed/open status
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
602
diff
changeset
|
286 |
|
607
47f2ee781d6d
[preferences] de-uglify a bit, remove useless parameter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
604
diff
changeset
|
287 |
def make_togglable_link(nodeid, label, cookiename): |
604
e85042d18b48
[preferences] preferences sections will now remember their collapsed/open status
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
602
diff
changeset
|
288 |
"""builds a HTML link that switches the visibility & remembers it""" |
607
47f2ee781d6d
[preferences] de-uglify a bit, remove useless parameter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
604
diff
changeset
|
289 |
action = u"javascript: toggle_and_remember_visibility('%s', '%s')" % \ |
47f2ee781d6d
[preferences] de-uglify a bit, remove useless parameter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
604
diff
changeset
|
290 |
(nodeid, cookiename) |
604
e85042d18b48
[preferences] preferences sections will now remember their collapsed/open status
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
602
diff
changeset
|
291 |
return u'<a href="%s">%s</a>' % (action, label) |
e85042d18b48
[preferences] preferences sections will now remember their collapsed/open status
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
602
diff
changeset
|
292 |
|
607
47f2ee781d6d
[preferences] de-uglify a bit, remove useless parameter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
604
diff
changeset
|
293 |
def css_class(someclass): |
47f2ee781d6d
[preferences] de-uglify a bit, remove useless parameter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
604
diff
changeset
|
294 |
return someclass and 'class="%s"' % someclass or '' |
47f2ee781d6d
[preferences] de-uglify a bit, remove useless parameter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
604
diff
changeset
|
295 |
|
0 | 296 |
class SystemEpropertiesForm(FormMixIn, StartupView): |
297 |
controller = 'edit' |
|
298 |
id = 'systemepropertiesform' |
|
299 |
title = _('site configuration') |
|
300 |
require_groups = ('managers',) |
|
301 |
category = 'startupview' |
|
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
302 |
|
0 | 303 |
def linkable(self): |
304 |
return True |
|
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
305 |
|
0 | 306 |
def url(self): |
307 |
"""return the url associated with this view. We can omit rql here""" |
|
308 |
return self.build_url('view', vid=self.id) |
|
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
309 |
|
607
47f2ee781d6d
[preferences] de-uglify a bit, remove useless parameter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
604
diff
changeset
|
310 |
def _cookie_name(self, somestr): |
47f2ee781d6d
[preferences] de-uglify a bit, remove useless parameter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
604
diff
changeset
|
311 |
return str('%s_property_%s' % (self.config.appid, somestr)) |
604
e85042d18b48
[preferences] preferences sections will now remember their collapsed/open status
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
602
diff
changeset
|
312 |
|
e85042d18b48
[preferences] preferences sections will now remember their collapsed/open status
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
602
diff
changeset
|
313 |
def _group_status(self, group, default=u'hidden'): |
e85042d18b48
[preferences] preferences sections will now remember their collapsed/open status
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
602
diff
changeset
|
314 |
cookies = self.req.get_cookie() |
607
47f2ee781d6d
[preferences] de-uglify a bit, remove useless parameter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
604
diff
changeset
|
315 |
cookiename = self._cookie_name(group) |
604
e85042d18b48
[preferences] preferences sections will now remember their collapsed/open status
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
602
diff
changeset
|
316 |
cookie = cookies.get(cookiename) |
e85042d18b48
[preferences] preferences sections will now remember their collapsed/open status
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
602
diff
changeset
|
317 |
if cookie is None: |
e85042d18b48
[preferences] preferences sections will now remember their collapsed/open status
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
602
diff
changeset
|
318 |
cookies[cookiename] = default |
e85042d18b48
[preferences] preferences sections will now remember their collapsed/open status
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
602
diff
changeset
|
319 |
self.req.set_cookie(cookies, cookiename, maxage=None) |
e85042d18b48
[preferences] preferences sections will now remember their collapsed/open status
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
602
diff
changeset
|
320 |
status = default |
e85042d18b48
[preferences] preferences sections will now remember their collapsed/open status
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
602
diff
changeset
|
321 |
else: |
e85042d18b48
[preferences] preferences sections will now remember their collapsed/open status
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
602
diff
changeset
|
322 |
status = cookie.value |
e85042d18b48
[preferences] preferences sections will now remember their collapsed/open status
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
602
diff
changeset
|
323 |
return status |
e85042d18b48
[preferences] preferences sections will now remember their collapsed/open status
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
602
diff
changeset
|
324 |
|
0 | 325 |
def call(self, **kwargs): |
326 |
"""The default view representing the application's index""" |
|
604
e85042d18b48
[preferences] preferences sections will now remember their collapsed/open status
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
602
diff
changeset
|
327 |
self.req.add_js(('cubicweb.edition.js', 'cubicweb.preferences.js')) |
0 | 328 |
self.req.add_css('cubicweb.preferences.css') |
329 |
vreg = self.vreg |
|
330 |
values = self.defined_keys |
|
331 |
groupedopts = {} |
|
332 |
mainopts = {} |
|
333 |
# "self.id=='systemepropertiesform'" to skip site wide properties on |
|
334 |
# user's preference but not site's configuration |
|
335 |
for key in vreg.user_property_keys(self.id=='systemepropertiesform'): |
|
336 |
parts = key.split('.') |
|
337 |
if parts[0] in vreg: |
|
338 |
# appobject configuration |
|
339 |
reg, oid, propid = parts |
|
340 |
groupedopts.setdefault(reg, {}).setdefault(oid, []).append(key) |
|
341 |
else: |
|
342 |
mainopts.setdefault(parts[0], []).append(key) |
|
343 |
# precompute form to consume error message |
|
344 |
for group, keys in mainopts.items(): |
|
345 |
mainopts[group] = self.form(keys, False) |
|
346 |
for group, objects in groupedopts.items(): |
|
347 |
for oid, keys in objects.items(): |
|
348 |
groupedopts[group][oid] = self.form(keys, True) |
|
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
349 |
|
0 | 350 |
w = self.w |
351 |
req = self.req |
|
352 |
_ = req._ |
|
353 |
w(u'<h1>%s</h1>\n' % _(self.title)) |
|
354 |
w(self.error_message()) |
|
355 |
for label, group, form in sorted((_(g), g, f) |
|
356 |
for g, f in mainopts.iteritems()): |
|
607
47f2ee781d6d
[preferences] de-uglify a bit, remove useless parameter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
604
diff
changeset
|
357 |
status = css_class(self._group_status(group)) #'hidden' (collapsed), or '' (open) ? |
0 | 358 |
w(u'<h2 class="propertiesform">%s</h2>\n' % |
604
e85042d18b48
[preferences] preferences sections will now remember their collapsed/open status
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
602
diff
changeset
|
359 |
(make_togglable_link('fieldset_' + group, label, |
607
47f2ee781d6d
[preferences] de-uglify a bit, remove useless parameter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
604
diff
changeset
|
360 |
self._cookie_name(group)))) |
47f2ee781d6d
[preferences] de-uglify a bit, remove useless parameter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
604
diff
changeset
|
361 |
w(u'<div id="fieldset_%s" %s>' % (group, status)) |
0 | 362 |
w(u'<fieldset class="subentity">') |
363 |
w(form) |
|
364 |
w(u'</fieldset></div>') |
|
365 |
for label, group, objects in sorted((_(g), g, o) |
|
366 |
for g, o in groupedopts.iteritems()): |
|
607
47f2ee781d6d
[preferences] de-uglify a bit, remove useless parameter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
604
diff
changeset
|
367 |
status = css_class(self._group_status(group)) |
0 | 368 |
w(u'<h2 class="propertiesform">%s</h2>\n' % |
604
e85042d18b48
[preferences] preferences sections will now remember their collapsed/open status
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
602
diff
changeset
|
369 |
(make_togglable_link('fieldset_' + group, label, |
607
47f2ee781d6d
[preferences] de-uglify a bit, remove useless parameter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
604
diff
changeset
|
370 |
self._cookie_name(group)))) |
47f2ee781d6d
[preferences] de-uglify a bit, remove useless parameter
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
604
diff
changeset
|
371 |
w(u'<div id="fieldset_%s" %s>' % (group, status)) |
0 | 372 |
for label, oid, form in sorted((self.req.__('%s_%s' % (group, o)), o, f) |
373 |
for o, f in objects.iteritems()): |
|
374 |
w(u'<fieldset class="subentity">') |
|
375 |
w(u'<legend class="componentTitle">%s</legend>\n' % label) |
|
376 |
docmsgid = '%s_%s_description' % (group, oid) |
|
377 |
doc = _(docmsgid) |
|
378 |
if doc != docmsgid: |
|
379 |
w(u'<p class="description">%s</p>' % html_escape(doc)) |
|
380 |
w(form) |
|
381 |
w(u'</fieldset>') |
|
382 |
w(u'</div>') |
|
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
383 |
|
0 | 384 |
@property |
385 |
@cached |
|
386 |
def eprops_rset(self): |
|
387 |
return self.req.execute('Any P,K,V WHERE P is EProperty, P pkey K, P value V, NOT P for_user U') |
|
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
388 |
|
0 | 389 |
@property |
390 |
def defined_keys(self): |
|
391 |
values = {} |
|
392 |
for i, entity in enumerate(self.eprops_rset.entities()): |
|
393 |
values[entity.pkey] = i |
|
394 |
return values |
|
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
395 |
|
0 | 396 |
def entity_for_key(self, key): |
397 |
values = self.defined_keys |
|
398 |
if key in values: |
|
399 |
entity = self.eprops_rset.get_entity(values[key], 0) |
|
400 |
else: |
|
401 |
entity = self.vreg.etype_class('EProperty')(self.req, None, None) |
|
402 |
entity.eid = self.req.varmaker.next() |
|
403 |
entity['value'] = self.vreg.property_value(key) |
|
404 |
return entity |
|
405 |
||
406 |
def form(self, keys, splitlabel=False): |
|
407 |
stream = UStringIO() |
|
408 |
w = stream.write |
|
409 |
w(u'<form action="%s" method="post">\n' % self.build_url()) |
|
410 |
w(u'<fieldset>\n') |
|
411 |
w(u'<input type="hidden" name="__errorurl" value="%s"/>\n' |
|
412 |
% html_escape(self.req.url())) |
|
413 |
w(u'<input type="hidden" name="__form_id" value="%s"/>\n' % self.id) |
|
414 |
path = self.req.relative_path() |
|
415 |
if '?' in path: |
|
416 |
path, params = path.split('?', 1) |
|
417 |
w(u'<input type="hidden" name="__redirectparams" value="%s"/>\n' |
|
418 |
% html_escape(params)) |
|
419 |
w(u'<input type="hidden" name="__redirectpath" value="%s"/>\n' % path) |
|
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
420 |
#w(u'<input type="hidden" name="__redirectrql" value=""/>\n') |
0 | 421 |
w(u'<input type="hidden" name="__message" value="%s"/>\n' |
422 |
% self.req._('changes applied')) |
|
423 |
w(u'<table><tr><td>\n') |
|
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
424 |
|
0 | 425 |
w(u'<table>\n') |
426 |
for key in keys: |
|
427 |
w(u'<tr>\n') |
|
428 |
self.form_row(w, key, splitlabel) |
|
429 |
w(u'</tr>\n') |
|
430 |
w(u'</table>\n') |
|
431 |
w(u'</td></tr><tr><td>\n') |
|
432 |
w(self.button_ok()) |
|
433 |
w(self.button_cancel()) |
|
434 |
w(u'</td></tr></table>\n') |
|
435 |
w(u'</fieldset>\n') |
|
436 |
w(u'</form>\n') |
|
437 |
return stream.getvalue() |
|
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
438 |
|
0 | 439 |
def form_row(self, w, key, splitlabel): |
440 |
entity = self.entity_for_key(key) |
|
441 |
eid = entity.eid |
|
442 |
if splitlabel: |
|
443 |
w(u'<td class="label">%s</td>' % self.req._(key.split('.')[-1])) |
|
444 |
else: |
|
445 |
w(u'<td class="label">%s</td>' % self.req._(key)) |
|
446 |
wdg = self.vreg.property_value_widget(key, req=self.req) |
|
447 |
error = wdg.render_error(entity) |
|
448 |
w(u'<td class="%s">' % (error and 'error' or '')) |
|
449 |
w(error) |
|
450 |
self.form_row_hiddens(w, entity, key) |
|
451 |
w(wdg.edit_render(entity)) |
|
452 |
w(u'</td>\n') |
|
453 |
w(u'<td>%s</td>' % wdg.render_help(entity)) |
|
454 |
return entity |
|
455 |
||
456 |
def form_row_hiddens(self, w, entity, key): |
|
457 |
eid = entity.eid |
|
458 |
w(u'<input type="hidden" name="eid" value="%s"/>' % eid) |
|
459 |
w(u'<input type="hidden" name="%s" value="EProperty"/>' % eid_param('__type', eid)) |
|
460 |
w(u'<input type="hidden" name="%s" value="%s"/>' % (eid_param('pkey', eid), key)) |
|
461 |
w(u'<input type="hidden" name="%s" value="%s"/>' % (eid_param('edits-pkey', eid), '')) |
|
462 |
||
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
463 |
|
0 | 464 |
class EpropertiesForm(SystemEpropertiesForm): |
465 |
id = 'epropertiesform' |
|
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
466 |
title = _('preferences') |
0 | 467 |
require_groups = ('users', 'managers') # we don't want guests to be able to come here |
237
3df2e0ae2eba
begin selector renaming (work in progress)
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
174
diff
changeset
|
468 |
__selectors__ = chainfirst(none_rset, |
431 | 469 |
chainall(one_line_rset, accept_rset)), |
0 | 470 |
accepts = ('EUser',) |
471 |
||
472 |
@classmethod |
|
473 |
def accept_rset(cls, req, rset, row, col): |
|
474 |
if row is None: |
|
475 |
row = 0 |
|
476 |
score = super(EpropertiesForm, cls).accept_rset(req, rset, row, col) |
|
477 |
# check current user is the rset user or he is in the managers group |
|
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
478 |
if score and (req.user.eid == rset[row][col or 0] |
0 | 479 |
or req.user.matching_groups('managers')): |
480 |
return score |
|
481 |
return 0 |
|
482 |
||
483 |
@property |
|
484 |
def user(self): |
|
485 |
if self.rset is None: |
|
486 |
return self.req.user |
|
487 |
return self.rset.get_entity(self.row or 0, self.col or 0) |
|
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
488 |
|
0 | 489 |
@property |
490 |
@cached |
|
491 |
def eprops_rset(self): |
|
492 |
return self.req.execute('Any P,K,V WHERE P is EProperty, P pkey K, P value V,' |
|
493 |
'P for_user U, U eid %(x)s', {'x': self.user.eid}) |
|
494 |
||
495 |
def form_row_hiddens(self, w, entity, key): |
|
496 |
super(EpropertiesForm, self).form_row_hiddens(w, entity, key) |
|
497 |
# if user is in the managers group and the property is being created, |
|
498 |
# we have to set for_user explicitly |
|
499 |
if not entity.has_eid() and self.user.matching_groups('managers'): |
|
500 |
eid = entity.eid |
|
501 |
w(u'<input type="hidden" name="%s" value="%s"/>' |
|
502 |
% (eid_param('edits-for_user', eid), INTERNAL_FIELD_VALUE)) |
|
503 |
w(u'<input type="hidden" name="%s" value="%s"/>' |
|
504 |
% (eid_param('for_user', eid), self.user.eid)) |
|
602
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
505 |
|
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
506 |
|
1454282a8b45
[views] cleanup, be less heavy with self.w ...
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
431
diff
changeset
|
507 |
|
0 | 508 |
|
509 |
class ProcessInformationView(StartupView): |
|
510 |
id = 'info' |
|
511 |
title = _('server information') |
|
512 |
require_groups = ('managers',) |
|
513 |
||
514 |
def call(self, **kwargs): |
|
515 |
"""display server information""" |
|
516 |
vcconf = self.config.vc_config() |
|
517 |
req = self.req |
|
518 |
_ = req._ |
|
519 |
# display main information |
|
520 |
self.w(u'<h3>%s</h3>' % _('Application')) |
|
521 |
self.w(u'<table border="1">') |
|
522 |
self.w(u'<tr><th align="left">%s</th><td>%s</td></tr>' % ( |
|
523 |
'CubicWeb', vcconf.get('cubicweb', _('no version information')))) |
|
524 |
for pkg in self.config.cubes(): |
|
525 |
pkgversion = vcconf.get(pkg, _('no version information')) |
|
526 |
self.w(u'<tr><th align="left">%s</th><td>%s</td></tr>' % ( |
|
527 |
pkg, pkgversion)) |
|
528 |
self.w(u'<tr><th align="left">%s</th><td>%s</td></tr>' % ( |
|
529 |
_('home'), self.config.apphome)) |
|
530 |
self.w(u'<tr><th align="left">%s</th><td>%s</td></tr>' % ( |
|
531 |
_('base url'), req.base_url())) |
|
532 |
self.w(u'<tr><th align="left">%s</th><td>%s</td></tr>' % ( |
|
533 |
_('data directory url'), req.datadir_url)) |
|
534 |
self.w(u'</table>') |
|
535 |
self.w(u'<br/>') |
|
536 |
# environment and request and server information |
|
537 |
try: |
|
538 |
# need to remove our adapter and then modpython-apache wrapper... |
|
539 |
env = req._areq._req.subprocess_env |
|
540 |
except AttributeError: |
|
541 |
return |
|
542 |
self.w(u'<h3>%s</h3>' % _('Environment')) |
|
543 |
self.w(u'<table border="1">') |
|
544 |
for attr in env.keys(): |
|
545 |
self.w(u'<tr><th align="left">%s</th><td>%s</td></tr>' |
|
546 |
% (attr, html_escape(env[attr]))) |
|
547 |
self.w(u'</table>') |
|
548 |
self.w(u'<h3>%s</h3>' % _('Request')) |
|
549 |
self.w(u'<table border="1">') |
|
550 |
for attr in ('filename', 'form', 'hostname', 'main', 'method', |
|
551 |
'path_info', 'protocol', |
|
552 |
'search_state', 'the_request', 'unparsed_uri', 'uri'): |
|
553 |
val = getattr(req, attr) |
|
554 |
self.w(u'<tr><th align="left">%s</th><td>%s</td></tr>' |
|
555 |
% (attr, html_escape(val))) |
|
556 |
self.w(u'</table>') |
|
557 |
server = req.server |
|
558 |
self.w(u'<h3>%s</h3>' % _('Server')) |
|
559 |
self.w(u'<table border="1">') |
|
560 |
for attr in dir(server): |
|
561 |
val = getattr(server, attr) |
|
562 |
if attr.startswith('_') or callable(val): |
|
563 |
continue |
|
564 |
self.w(u'<tr><th align="left">%s</th><td>%s</td></tr>' |
|
565 |
% (attr, html_escape(val))) |
|
566 |
self.w(u'</table>') |
|
567 |